English 中文(简体)
从c调用带有变量类型参数的德尔菲dll#
原标题:Calling a delphi dll with a variant type parameter from c#
  • 时间:2012-05-10 10:30:13
  •  标签:
  • c#
  • delphi
  • dll

我有一个带有此功能的delphi dll:

function writeRate(data: variant):Double ; stdcall;

我使用此方法从c#调用函数:

[UnmanagedFunctionPointer(CallingConvention.Cdecl)]
private delegate double WriteRate(object data);
protected void UpdateRateChannel(string myData)
{   
    IntPtr pDll = NativeMethods.LoadLibrary("mydll.dll");

    IntPtr pAddressOfFunctionToCall = NativeMethods.GetProcAddress(pDll, "writeRate");
    WriteRate writeRate = (WriteRate)Marshal.GetDelegateForFunctionPointer(pAddressOfFunctionToCall, typeof(WriteRate ));


    double response = writeRate(myData);

    bool result = NativeMethods.FreeLibrary(pDll);
}

但我得到了一个例外:

PInvokeStackImbalance was detected

How do I call the dll? I supposed that the issue is in the variant type. thanks!

问题回答

Delphi代码中的stdcallCallingConvention.StdCall。您应该修复您的委托定义。

如果Delphi函数被声明为stdcall,为什么要在C#中将其声明为cdecl

这就是堆栈不平衡的原因。将C#声明更改为使用stdcall约定来匹配Delphi声明。

[UnmanagedFunctionPointer(CallingConvention.StdCall)]
private delegate double WriteRate(object data);

C#中的变量可能与Delphi中的普通变量不兼容。请改用Delphi的OleVariant。





相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

NSArray s, Primitive types and Boxing Oh My!

I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

How to Use Ghostscript DLL to convert PDF to PDF/A

How to user GhostScript DLL to convert PDF to PDF/A. I know I kind of have to call the exported function of gsdll32.dll whose name is gsapi_init_with_args, but how do i pass the right arguments? BTW, ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...

热门标签