×
INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Contact US

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

Proper way to implement IUnknown?

Proper way to implement IUnknown?

Proper way to implement IUnknown?

(OP)
I'm working on someone else's code, which currently works, but I need to add some functionality.
It uses CoCreateInstance to instantiate some COM objects which seem to implement the IUnknown interface, but QueryInterface is not accessible. I'm not as familiar with C# as other languages, so maybe there's something obvious I'm missing. I'll try to keep this brief and only post what I think are the relevant snippets of code...

CODE --> C#

//the DllImport:
[DllImport("ole32.Dll")]
static public extern int CoCreateInstance(ref Guid clsid,
           [MarshalAs(UnmanagedType.IUnknown)] object inner,
           uint context,
           ref Guid uuid,
           [Out, MarshalAs(UnmanagedType.IUnknown)] out object rReturnedComObject);

//an example of one of the interface definitions:
[Guid("C0E8AE93-306E-11D1-AACF-00805FC1270E"), 
InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
[ComVisible(true)]
public interface INetCfg
    {
        int Initialize(IntPtr pvReserved);
        int Uninitialize();
        int Apply();
        int Cancel();
        int EnumComponents([In]ref Guid pguidClass, [Out, MarshalAs(UnmanagedType.IUnknown)] out object ppenumComponent);
        int FindComponent([In, MarshalAs(UnmanagedType.LPWStr)]  string pszwInfId,
                           [Out, MarshalAs(UnmanagedType.IUnknown)] out object pComponent);
        int QueryNetCfgClass([In]ref Guid pguidClass,
                              ref  Guid riid,
                             [Out, MarshalAs(UnmanagedType.IUnknown)] out object ppvObject);
    };

// what I need to be able to do:
int hReturn = 0;
object tempObj1 = null;
hResult = CoCreateInstance(ref CLSID_CNetCfg, null, CLSCTX_INPROC_SERVER, ref IID_INetCfg, out tempObj1);
INetCfg InetCfgObj = tempObj1 as INetCfg;
object tempObj2 = null;
hResult = InetCfgObj.QueryInterface(IID_INetCfgLock, tempObj2);
INetCfgLock LockObj = tempObj2 as INetCfgLock 

It tells me that the INetCfg interface does not have a QueryInterface method. Iv'e tried to manually add that method to the INetCfg definition, as such:

CODE --> C#

int QueryInterface([In]ref Guid pguidClass, [Out, MarshalAs(UnmanagedType.IUnknown)] out object ppvObject); 

With that added I get a value outside of expected range exception when it tries to call QueryInterface. But I shouldn't have to manually add that method if it's part of the base interface, should I?

RE: Proper way to implement IUnknown?

(OP)
I think part of the problem is I've found multiple definitions of the QueryInterface method, depending what site you look at. MSDN lists one in the documentation for the Notify classes (network device drivers), pinvoke.net has another, and in VS if I do a Marshal.QueryInterface() it shows another (the one I assume it's trying to use).
I think part of the problem is it doesn't like me passing in an unmanaged interface object where it expects an IntPtr, and I haven't found a way to convert it to one yet.

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members! Already a Member? Login

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close