Marshaling Delphi 5 OleVariant to C#

二次信任 提交于 2019-12-12 02:33:37

问题


I'm trying to use some legacy Delphi 5 DLLs from C# (2.0/3.5). Some of the exported functions are declared as such:

function SimpleExport: OleVariant; stdcall;
function BiDirectionalExport(X: OleVariant; var Y: OleVariant): OleVariant; stdcall;

I wish to set these up as delegates using Marshal.GetDelegateForFunctionPointer, but I'm having trouble getting the data Marshaled correctly. I'm using kernel32 imports of LoadLibrary and GetProcAddress, so I'm relying on GetDelegateForFunctionPointer to do my actual marshaling, not static p/invoke declarations.

Since the .NET marshaling services can marshal objects to COM OleVariants, I tried this. But this causes an exception: "PInvoke restriction: can not return variants.". So I'm figuring I need to use a custom marshaller.

What's the correct way to Marshal a Delphi 5 OleVariant into something .NET readable?


回答1:


Since OleVariant can be nearly any type, and all types in C# can be handled as objects I would try system.object.

Also this link may be helpful.



来源:https://stackoverflow.com/questions/1769969/marshaling-delphi-5-olevariant-to-c-sharp

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!