我在 c++ 中有这个代码,我通过 dll 导出:
typedef struct {
unsigned short major;
unsigned short minor;
} Version;
EXPORTED_FUNC Result Init(Version *version, char *file);
extern "C" Result Init(Version *version, char *file)
{
if (file) {
if (!GFile.init(string(file))) {
return INVALID_PARAMETER;
}
if (version) {
version->major = VERSION_MAJOR1;
version->minor = VERSION_MAJOR2;
}
return OK;
}
我从 c# 调用 dll,这就是我在那里写的:
internal struct Version
{
ushort major { set; get; }
ushort minor { set; get; }
}
[DllImport("mydll.dll", CallingConvention=CallingConvention.Cdecl)]
static extern Result Init(ref Version versionInfo, [MarshalAs`(UnmanagedType.LPStr)] string FilePath);
这是对 Init 的调用:
string filePath = Application.StartupPath + "\\ABC.ini";
Version version = new Version();
result = _mydllWrapper.Init(ref version, filePath);
对于上述所有代码,当我运行 c# 应用程序时,有时会在 x64 机器中出现以下异常:
Unable to load DLL mydll.dll : invalid access to memory location (Exception from HRESULT.0x800703E6)
如何在不从编译中删除任何安全标志的情况下修复此代码?修复的代码示例真的很受欢迎!
谢谢!