Search code examples
comatlvoid-pointersidl

How to declare an arguments of type void* in IDL


I am developing ATL project. Some interfaces contain arguments to pass the various pointers. Here is my IDL file:

import "oaidl.idl";
import "ocidl.idl";
[
    object,
    uuid(618E64F5-676B-4A13-A513-DE3D4097294A),
    dual,
    nonextensible,
    helpstring("IMyObject Interface"),
    pointer_default(unique)
]
interface IMyObject : IDispatch{
    [id(1), helpstring("method Make")] HRESULT Make(DWORD type, LPVOID settings);
    [id(2), helpstring("method Deserialize")] HRESULT Deserialize(LPVOID dataPtr, DWORD dataSize);
};
[
    uuid(E57065B4-498F-4347-9ACC-A2C86A771720),
    version(1.0),
    helpstring("TestComVoid 1.0 Type Library")
]
library TestComVoidLib
{
    importlib("stdole2.tlb");
    [
        uuid(1DC2528B-EA49-4F89-BB56-B1D667379644),
        helpstring("MyObject Class")
    ]
    coclass MyObject
    {
        [default] interface IMyObject;
    };
};

The first method of IMyObject constructs an object based on the type and the corresponding structure. The second method constructs an object based on binary data.

But I get error MIDL2139 : type of the parameter cannot derive from void or void *

In addition, I want to make a C# wrapper with IntPtr arguments to pass pointers. ("Add reference"->COM->"TestComVoid 1.0 Type Library")

I tried to use INT_PTR instead of LPVOID, then I get int instead of IntPtr. I tried to use DWORD_PTR instead of LPVOID, then I get uint instead of IntPtr. I tried to use VARIANT instead of LPVOID, then I get Object instead of IntPtr.

How to correctly pass pointers to these cases?


Solution

  • I found the solution. It need use a local attribute for those methods. Here it is:

    [
        object,
        uuid(618E64F5-676B-4A13-A513-DE3D4097294A),
        dual,
        nonextensible,
        helpstring("IMyObject Interface"),
        pointer_default(unique)
    ]
    interface IMyObject : IDispatch{
        [id(1), helpstring("method Make"), local] HRESULT Make(DWORD type, LPVOID settings);
        [id(2), helpstring("method Deserialize"), local] HRESULT Deserialize(LPVOID dataPtr, DWORD dataSize);
    };