Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / .NET Framework / Interop / November 2003

Tip: Looking for answers? Try searching our database.

Implementing IOleCommandTarget

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Oliver Sturm - 25 Nov 2003 20:49 GMT
Hey,

can I implement IOleCommandTarget in a .net COM server? I had the idea to
define the interface myself using the GUID it usually has, then implement
it... is that the right way to go?

              Oliver Sturm
Signature

omnibus ex nihilo ducendis sufficit unum
Jabber sturm@amessage.de ICQ 27142619 MSN macnapple@hotmail.com Y! macnapple

Mattias Sj?gren - 25 Nov 2003 20:56 GMT
Oliver,

>can I implement IOleCommandTarget in a .net COM server? I had the idea to
>define the interface myself using the GUID it usually has, then implement
>it... is that the right way to go?

Yes and yes.

Mattias

Signature

Mattias Sjögren [MVP]  mattias @ mvps.org
http://www.msjogren.net/dotnet/
Please reply only to the newsgroup.

Oliver Sturm - 26 Nov 2003 08:27 GMT
On Tue, 25 Nov 2003 21:56:55 +0100, Mattias Sjögren wrote:

>>can I implement IOleCommandTarget in a .net COM server? I had the idea to
>>define the interface myself using the GUID it usually has, then implement
>>it... is that the right way to go?
>
> Yes and yes.

Thanks, that sounds fine :-) Has anybody done that already? I mean, is
there a project somewhere that has taken up the task of converting
interface definitions from IDL to .net? Should be possible to write an
automated converter...

              Oliver Sturm
Signature

Dahlhoff IT-Solutions - Buellenkothenweg 37a - 40229 Duesseldorf
Tel.: 0211-22959012   - Fax: 0211-22959016   - http://www.dahlhoff.biz
Jabber sturm@amessage.de ICQ 27142619 MSN macnapple@hotmail.com Y! macnapple

Mattias Sj?gren - 26 Nov 2003 12:08 GMT
Oliver,

>Has anybody done that already?

Try this:

public struct OLECMD
{
 public uint cmdID;
 public uint cmdf;
}

[ComImport, Guid("b722bccb-4e68-101b-a2bc-00aa00404770"),
InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
interface IOleCommandTarget
{
 void QueryStatus(
   ref Guid pguidCmdGroup,
   uint cCmds,
   [In, Out, MarshalAs(UnmanagedType.LPArray)] OLECMD[] prgCmds,
   IntPtr /* OLECMDTEXT* */ CmdText);

 void Exec(
   ref Guid pguidCmdGroup,
   uint nCmdId,
   uint nCmdExecOpt,
   ref object pvaIn,
   ref object pvaOut);
}

Mattias

Signature

Mattias Sjögren [MVP]  mattias @ mvps.org
http://www.msjogren.net/dotnet/
Please reply only to the newsgroup.

Oliver Sturm - 26 Nov 2003 20:03 GMT
Thanks for your help! I'm still having some problems, though... see inline.

On Wed, 26 Nov 2003 13:08:17 +0100, Mattias Sjögren wrote:

> Try this:

>   void QueryStatus(

Shouldn't that have a return value? Normally, it's a HRESULT and I docs
say I need to return S_OK.

>     ref Guid pguidCmdGroup,
>     uint cCmds,
>     [In, Out, MarshalAs(UnmanagedType.LPArray)] OLECMD[] prgCmds,

This doesn't seem to work correctly. I'm trying to set prgCmds[0].cmdf to
something, needed to tell the caller if each of the commands is accepted
by my caller. But the .net compiler tells me that I can't assing anything
to the left side...

Trying to use that thing (I'm attaching it to an addin toolbar button in
IE), I get a System.ExecutionEngineException when hitting the button. I
tried attaching the debugger to the running IE instance and it doesn't
seem like any of my implementation code is called before the exception
occurs. Any hints?

              Oliver Sturm
Signature

omnibus ex nihilo ducendis sufficit unum
Jabber sturm@amessage.de ICQ 27142619 MSN macnapple@hotmail.com Y! macnapple

Mattias Sj?gren - 27 Nov 2003 22:45 GMT
Oliver,

>>   void QueryStatus(
>
>Shouldn't that have a return value? Normally, it's a HRESULT and I docs
>say I need to return S_OK.

The HRESULT is hidden from you. The runtime takes care of interpreting
it and throws an exception if an error HRESULT is returned.

If you want to handle the return value manually, change the return
type to (u)int and apply the [PreserveSig] attribute to the method.

>But the .net compiler tells me that I can't assing anything
>to the left side...

Can you post your code? Are you implementing the interface or are you
using it to call another object?

Mattias

Signature

Mattias Sjögren [MVP]  mattias @ mvps.org
http://www.msjogren.net/dotnet/
Please reply only to the newsgroup.

Oliver Sturm - 28 Nov 2003 20:03 GMT
On Thu, 27 Nov 2003 23:45:04 +0100, Mattias Sjögren wrote:

>>But the .net compiler tells me that I can't assing anything
>>to the left side...
>
> Can you post your code? Are you implementing the interface or are you
> using it to call another object?

Here's my code. I'm trying to implement an object that can be used as a
COM object for a button in IE.

Currently, this doesn't work at all... I have all the entries in the
registry for the button (it works when using an .exe to run instead of the
COM object) and I use regasm to register the dll compiled from the
following code. But the moment I hit the button in IE, it just crashes on
me without showing any of the message boxes from my code.

------------------ cut -------------------------------

using System;
using System.Windows.Forms;
using System.Runtime.InteropServices;

namespace iebuttontest {
    [Guid ("3C7BA736-716C-4346-806D-714778E3F0CD")]
    public interface IIEPluginTest1 {
        int DoYourJob();
    }

    public struct OLECMD {
        public uint cmdID;
        public uint cmdf;
    }

    [ComImport, Guid("b722bccb-4e68-101b-a2bc-00aa00404770"),
        InterfaceType (ComInterfaceType.InterfaceIsIUnknown)]
    public interface IOleCommandTarget {
        void QueryStatus (
            ref Guid pguidCmdGroup,
            uint cCmds,
            [In, Out, MarshalAs(UnmanagedType.LPArray)] OLECMD[] prgCmds,
            IntPtr CmdText);

        void Exec (
            ref Guid pguidCmdGroup,
            uint nCmdId,
            uint nCmdExecOpt,
            ref object pvaIn,
            ref object pvaOut);
    }

    [Guid ("D985E523-358B-4d60-88D8-684474C910F6")]
    public class IEPluginTest1 : IIEPluginTest1, IOleCommandTarget
    {
        public int DoYourJob () {
            MessageBox.Show ("Doing my job");
            return 0;
        }

        public void QueryStatus(ref Guid pguidCmdGroup, uint cCmds, OLECMD[]
        prgCmds, IntPtr CmdText) {
            MessageBox.Show ("Query status");

               
/*            The following doesn't work because it says I can't assign
            to the left hand side.

            foreach (OLECMD cmd in prgCmds) {
                cmd.cmdf = 3;
            }
*/
           
        }

        public void Exec(ref Guid pguidCmdGroup, uint nCmdId, uint nCmdExecOpt,
        ref object pvaIn, ref object pvaOut) {
            MessageBox.Show ("Execing");

        }
    }
}

------------------ cut -------------------------------

              Oliver Sturm
Signature

omnibus ex nihilo ducendis sufficit unum
Jabber sturm@amessage.de ICQ 27142619 MSN macnapple@hotmail.com Y! macnapple


Rate this thread:







Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.