嗨,我有一个DLL需要调用的功能.签名是:
const char* callMethod(const char* key,const char* inParams);
如果我使用ruby一切正常工作:
attach_function :callMethod,[:string,:string],:string
如果我使用C或C#,我得到堆栈溢出!
C#:
[DllImport("DeviceHub.dll",CallingConvention = CallingConvention.Cdecl)] private unsafe static extern IntPtr callMethod( [MarshalAs(UnmanagedType.LPArray)] byte[] key,[MarshalAs(UnmanagedType.LPArray)] byte[] inParams ); System.Text.UTF8Encoding encoding = new UTF8Encoding(); IntPtr p = callMethod(encoding.GetBytes(key),encoding.GetBytes(args)); // <- stack overflow here
C :
extern "C" { typedef DllImport const char* ( *pICFUNC) (const char*,const char*); } HINSTANCE hGetProcIDDLL = LoadLibrary(TEXT("C:\\JOAO\\Temp\\testedll\\Debug\\DeviceHub.dll")); FARPROC lpfnGetProcessID = GetProcAddress(HMODULE (hGetProcIDDLL),"callMethod");* pICFUNC callMethod; callMethod = (pICFUNC) lpfnGetProcessID; const char * ptr = callMethod("c","{}");
我已经尝试了很多变化的函数调用:WINAPI,PASCAL,stdcall,fastcall,…没有任何工作.
DLL没有被我制作,我没有控制.
任何人都可以帮我任何建议!
解决方法
确保标题中的声明包含在一个extern“C”块中:
extern "C" { const char* callMethod(const char* key,const char* inParams); }
见http://www.parashift.com/c++-faq-lite/mixing-c-and-cpp.html#faq-32.3