[英]Unknown error when using .NET 4.0 with dll
c++ code is C ++代码是
MSIPC_SDK LONG __stdcall Ms_IpcClient_CaptureImage(LONG nUserId, char *sFilePath,
int nPathLen, const char *sDiskPath = NULL);//sDiskPath example: "C: \\".
Affect: Take a snapshoot 影响:快速拍摄
Parameters remark: 参数备注:
LONG nUserId
: Ms_Ipc_Login()//Return value after login successfully LONG nUserId
:Ms_Ipc_Login()//成功登录后返回值 char *sFilePath
: //destination for saving the recording files char *sFilePath
://保存录音文件的目的地 int nPathLen
: //the length of the path int nPathLen
://路径的长度 const char *sDiskPath = NULL
: //which disk to be saved const char *sDiskPath = NULL
://要保存的磁盘 my c# code is: 我的C#代码是:
[DllImport("MsIpcSDK", CharSet = CharSet.Ansi,
CallingConvention = CallingConvention.StdCall)]
public static extern int Ms_IpcClient_CaptureImage(
int lUserID,
[MarshalAs(UnmanagedType.LPStr)]
string sFilePath,
int nPathLen,
[MarshalAs(UnmanagedType.LPStr)]
string sDiskPath
);
and using is method: 并使用is方法:
var ret = Ms_IpcClient_CaptureImage(loginID, "C:\\a.bmp", 10000, "C:\\");
It is working in .Net Framework 2 but does not work in .Net Framework 4. How can I fix it in .Net Framework 4? 它可以在.Net Framework 2中工作,但不能在.Net Framework 4中工作。如何在.Net Framework 4中修复它?
sFilePath
is used to pass a string from callee to caller. sFilePath
用于将字符串从被调用方传递给调用方。 That's why the type is char*
rather than const char*
, and that's why there is a parameter for buffer length. 这就是为什么类型是char*
而不是const char*
的原因,这就是为什么有一个用于缓冲区长度的参数的原因。 That means you need to use StringBuilder
rather than string
. 这意味着您需要使用StringBuilder
而不是string
。 The p/invoke should be: p /调用应为:
[DllImport("MsIpcSDK", CharSet = CharSet.Ansi,
CallingConvention = CallingConvention.StdCall)]
public static extern int Ms_IpcClient_CaptureImage(
int lUserID,
StringBuilder sFilePath,
int nPathLen,
string sDiskPath
);
And the call should be: 呼叫应该是:
var filePath = new StringBuilder(260);
var ret = Ms_IpcClient_CaptureImage(loginID, filePath, filePath.Capacity, "C:\\");
Your code has always been wrong and you've just been getting away with it up until now. 您的代码一直以来都是错误的,直到现在您一直在不理会它。 The fact that you passed a made up buffer length value of 10000
should have set the alarm bells ringing! 您通过了一个组合的缓冲区长度值10000
的事实应该设置了警报铃声!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.