You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Hi folks, I'm trying to pass my interface IFoo to an exported function [DllImport("CppProject")] static extern int FooFunction(IFoo foo); but I'm hitting a slight issue. During marshalling, the runtime asks my ComWrapper for the Vtable. I'm able to generate the Vtbale and pass it to the runtime. Problem is, when the exported FooFunction is called, it passes a completely made up pointer to the IFoo instance, but the Vtable is correct. What am I doing wrong?
[ComImport][InterfaceType(ComInterfaceType.InterfaceIsIUnknown)][Guid("C859BA31-821B-4947-A04F-5C30C6A94B82")]publicinterfaceIFoo{[PreserveSig]intBar([In][MarshalAs(UnmanagedType.LPStr)]stringMessage);}publicclassFooComWrapper:IFoo{publicstructIFooVtbl{publicIntPtrBar;}publicIntPtrInstance{get;}publicIFooVtblVtbl{get;}publicdelegateintBarDelegate(IntPtrptrThis,[MarshalAs(UnmanagedType.LPStr)]stringMessage);publicBarDelegateBarFunction{get;set;}publicFooComWrapper(IntPtrptr){Instance=ptr;
unsafe
{Vtbl=Marshal.PtrToStructure<IFooVtbl>(newnint(*(void**)ptr.ToPointer()));BarFunction=Marshal.GetDelegateForFunctionPointer<BarDelegate>(Vtbl.Bar);}}publicintBar(stringMessage){returnBarFunction(Instance,Message);}}publicclassFooComWrappers:ComWrappers{protectedoverrideunsafeComInterfaceEntry*ComputeVtables(objectobj,CreateComInterfaceFlagsflags,outintcount){ComInterfaceEntry*entry=null;count=0;if(objisFooComWrapperfoo){// What do I do here lmao?count=1;entry=(ComInterfaceEntry*)RuntimeHelpers.AllocateTypeAssociatedMemory(typeof(FooComWrappers),sizeof(ComInterfaceEntry));entry->Vtable=*(IntPtr*)foo.Instance.ToPointer();
entry->IID=typeof(IFoo).GUID;}returnentry;}protectedoverrideobject?CreateObject(nintexternalComObject,CreateObjectFlagsflags){returnnewFooComWrapper(externalComObject);}protectedoverridevoidReleaseObjects(IEnumerableobjects){thrownewNotImplementedException();}}publicstaticclassProgram{[DllImport("CppProject")]staticexternintFooFactory(outIntPtrptr);[DllImport("CppProject")]staticexternintFooFunction(IFoofoo);[SupportedOSPlatform("Windows")]publicstaticvoidMain(string[]args){ComWrappers.RegisterForMarshalling(newFooComWrappers());if(FooFactory(outIntPtrptrFoo)==0){if(Marshal.GetObjectForIUnknown(ptrFoo)isIFoofoo){foo.Bar("RCW");_=FooFunction(foo);// <<<<<< Pointer passed here to unmanaged function is incorrect but computed vtable is correct}}}}
reacted with thumbs up emoji reacted with thumbs down emoji reacted with laugh emoji reacted with hooray emoji reacted with confused emoji reacted with heart emoji reacted with rocket emoji reacted with eyes emoji
-
Hi folks, I'm trying to pass my interface
IFoo
to an exported function[DllImport("CppProject")] static extern int FooFunction(IFoo foo);
but I'm hitting a slight issue. During marshalling, the runtime asks my ComWrapper for the Vtable. I'm able to generate the Vtbale and pass it to the runtime. Problem is, when the exportedFooFunction
is called, it passes a completely made up pointer to theIFoo
instance, but the Vtable is correct. What am I doing wrong?Beta Was this translation helpful? Give feedback.
All reactions