出口C&x2B+;C#应用程序的命名共享内存读取器变量 我有Win32 C++的DLL,读取一个命名共享内存,我想导出这些应用程序接收到C应用程序的值。
实际上,我有这段代码,但当我访问C#App中的值时,我会收到:出口C&x2B+;C#应用程序的命名共享内存读取器变量 我有Win32 C++的DLL,读取一个命名共享内存,我想导出这些应用程序接收到C应用程序的值。,c#,interop,pinvoke,C#,Interop,Pinvoke,实际上,我有这段代码,但当我访问C#App中的值时,我会收到: An unhandled exception of type 'System.AccessViolationException' occurred in ETS2 Utilities.exe Additional information: Attempted to read or write protected memory. This is often an indication that other memory is cor
An unhandled exception of type 'System.AccessViolationException' occurred in ETS2 Utilities.exe
Additional information: Attempted to read or write protected memory. This is often an indication that other memory is corrupt.
C++代码:
extern“C”{
__declspec(dllexport)int__cdeclreturngear();
}
外部内部cdecl returnGear()
{
返回共享内存->档位;
}
C#代码:
p/invoke具有错误的调用约定。应该是:
[DllImport("ETS2_Utilities_Plugin.dll", CallingConvention = CallingConvention.Cdecl)]
public static extern int returnGear();
然而,我怀疑这是你的问题。我怀疑真正的问题是共享_内存
没有指向有效内存。很可能是共享内存
是nullptr
作为互操作的快速测试,请使用以下内容替换未损坏的函数:
int __cdecl returnGear()
{
return 42;
}
如果这样做有效,则说明互操作是正常的,但未正确初始化非托管库
最后一个注意事项是,在非托管
returnGear
函数的定义中使用了extern
。这是一个错误。删除它,正如我上面所做的。< /P>这个问题可能会有帮助(注意包含<代码> CalpCueNe==CalgnValue.Cdecl < /Cult>在<代码> DLimPurt属性)。您需要调试C++代码,显然它不快乐。项目+属性,调试选项卡,勾选“启用本机代码调试”选项。在函数的入口点上设置一个断点,以验证调试器是否正常工作,并查看调试器从何处开始运行。如果我将return 42放在C#应用程序中,这将返回42。空指针?在控制台应用程序中,如果我这样做:coutgear;打印值。共享内存在哪里初始化?David Heffernan:telemetry\u state\u t*共享内存=NULL;在main()中:shared_memory=static_cast(MapViewOfFile(内存映射,文件映射,所有访问,0,0))MapViewOfFile
可能返回NULL
。你查过了吗?若否,原因为何?在调用Win32函数时总是检查错误。但为什么在控制台应用程序中,当我执行cout gear时,这会起作用?
int __cdecl returnGear()
{
return 42;
}