尝试读取或写入受保护的内存。这通常表明其他内存已损坏 DllImport
Attempted to read or write protected memory. This is often an indication that other memory is corrupt DllImport
我用 C++ 创建了一个 dll。我在该 dll 中有一个函数,其中包含以下代码。
__declspec(dllexport) void MyFunction(CString strPath)
{
BYTE startBuffer[] = { 80, 75, 5, 6, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0 };
FILE *f = _wfopen(strPath.GetBuffer(strPath.GetLength()), _T("wb"));
if (f != NULL)
{
strPath.ReleaseBuffer();
fwrite(startBuffer, sizeof(startBuffer), 1, f);
fclose(f);
}
}
如果我注释掉这一行然后调用dll。不会有问题的。
调用约定如下:
[DllImport("OutExt.dll",CharSet=CharSet.Unicode)]
static extern void MyFunction([MarshalAs(UnmanagedType.LPStr)] string strPath);
谁能帮我解决这个问题。
CString
是原生 C++ class,无法使用 p/invoke 编组。您将需要使用指针空终止字符数组。
或者:
__declspec(dllexport) void MyFunction(const char *strPath)
如果您必须限制自己使用旧版 ANSI 代码页,或者
__declspec(dllexport) void MyFunction(const wchar_t *strPath)
用于 Unicode。
在 C# 端,声明为:
[DllImport("OutExt.dll", CharSet = CharSet.Ansi, CallingConvention = CallingConvention.Cdecl)]
static extern void MyFunction(string strPath);
和
[DllImport("OutExt.dll", CharSet = CharSet.Unicode, CallingConvention = CallingConvention.Cdecl)]
static extern void MyFunction(string strPath);
分别
我用 C++ 创建了一个 dll。我在该 dll 中有一个函数,其中包含以下代码。
__declspec(dllexport) void MyFunction(CString strPath)
{
BYTE startBuffer[] = { 80, 75, 5, 6, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0 };
FILE *f = _wfopen(strPath.GetBuffer(strPath.GetLength()), _T("wb"));
if (f != NULL)
{
strPath.ReleaseBuffer();
fwrite(startBuffer, sizeof(startBuffer), 1, f);
fclose(f);
}
}
如果我注释掉这一行然后调用dll。不会有问题的。
调用约定如下:
[DllImport("OutExt.dll",CharSet=CharSet.Unicode)]
static extern void MyFunction([MarshalAs(UnmanagedType.LPStr)] string strPath);
谁能帮我解决这个问题。
CString
是原生 C++ class,无法使用 p/invoke 编组。您将需要使用指针空终止字符数组。
或者:
__declspec(dllexport) void MyFunction(const char *strPath)
如果您必须限制自己使用旧版 ANSI 代码页,或者
__declspec(dllexport) void MyFunction(const wchar_t *strPath)
用于 Unicode。
在 C# 端,声明为:
[DllImport("OutExt.dll", CharSet = CharSet.Ansi, CallingConvention = CallingConvention.Cdecl)]
static extern void MyFunction(string strPath);
和
[DllImport("OutExt.dll", CharSet = CharSet.Unicode, CallingConvention = CallingConvention.Cdecl)]
static extern void MyFunction(string strPath);
分别