Click here to Skip to main content
15,883,788 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
If a COM interface has a definition in C++:
C++
interface Ixxx {
 ....
HRESULT fn([in]LPWSTR pszName, [in]REFIID riid, [out]void** ppv);

I am declaring it in my COMImport as
C#
[ComImport, Guid(...)]
Interface Ixxx {
 ....
void fn(
    [In, MarshalAs(UnmanagedType.LPWStr)] string pszName,
    [In, MarshalAs(UnmanagedType.LPStruct)] Guid riid,
    [Out, MarshalAs(UnmanagedType.Interface, IidParameterIndex = 1)] out IInterface ppv);

Then, when I try to call the function...
C#
IInterface obj;
Ixxx x = // object implementing the Ixxx interface;
string name="name";
try {
    x.fn(name, typeof(IInterface).GUID, out obj);
}
catch { // Handle the COM exception

...I am getting a protected memory violation. There is something amiss with the way I am declaring the void** parameter, I am sure, although it all seems to work ok when calling an API function with the same sort of signature.

Any ideas on how the marshalling should be declared?

What I have tried:

setting ppv as an IntPtr
passing riid by ref
leaving off the IidParameterIndex parameter

and, of course, google.
Posted
Comments
Bernhard Hiller 18-Oct-16 3:26am    
What are you trying to achieve here? Instantiating a COM class from C#?
Midi_Mick 18-Oct-16 4:25am    
I'm actually trying to work with the Windows Property System. I instantiate an object exposing the IPropertySystem interface with a call to PSGetPropertySystem - that works fine. Then when I try to use that interface to call, say IPropertySystem.GetPropertyDescriptionByName - that error occurs. I've declared the interfaces with the COMImport attribute.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900