Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Proper way to implement IUnknown?

Status
Not open for further replies.

Jasen

IS-IT--Management
May 1, 2002
435
US
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:
[COLOR=#4E9A06]//the DllImport:[/color]
[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);

[COLOR=#4E9A06]//an example of one of the interface definitions:[/color]
[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);
    };

[COLOR=#4E9A06]// what I need to be able to do:[/color]
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;
[b]hResult = InetCfgObj.QueryInterface(IID_INetCfgLock, tempObj2);[/b]
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:
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?
 
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.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top