Call to external DLL from C# with integer pointer

前提是你 提交于 2019-12-02 04:51:00

问题


I'm trying to call an external .dll function from c#. The doc for the dll defines the function:

int funcName(int *retVal)

I've tried various configurations and always the unbalanced stack error from p/invoke; My c# code currently looks like this:

[DLLImport("dllName");
unsafe static extern int funcName(ref IntPtr retVal);
unsafe IntPtr retNum;
int status = funcName(ref retNum);

Any ideas are appreciated!


回答1:


Your p/invoke declaration has the wrong parameter type.

  • ref Int32 is the correct match for int*.

  • IntPtr can also work.

  • ref IntPtr would be int**. Definitely not what you want.

Use

[DLLImport("dllName")]
static extern int funcName(ref Int32 retVal);

Also make sure that the calling convention matches. You should never use a dllexport in C or C++ without also using an explicit calling convention, and then the C# DllImport needs to have the matching convention.

Generally the prototype in C++ should be

extern "C" int __stdcall funcName(int* arg);

Is there a header file provided for C and C++ clients that you could check to verify the signature?



来源:https://stackoverflow.com/questions/23325668/call-to-external-dll-from-c-sharp-with-integer-pointer

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