P/Invoke 代码适用于 WinXP,但 Win2k8 上例外
我正在尝试使用 C# 和 C++ 访问 DLL 中的函数。
C++ 运行良好,WinXP 上的 C# 也运行良好。但是,当我尝试在 Win2k8 系统上访问该函数时,出现以下错误:
Unhandled Exception: System.AccessViolationException: Attempted to read or write protected memory. This is often an indication that other memory
is corrupt.
at Router.GetAddress()
C# 中的声明是:
[DllImport("Constants.dll")]
static extern String GetAddress();
C# 中的用法(目前)只是输出它:
Console.WriteLine(GetAddress());
DLL 函数的内容只是:
const static WCHAR* szAddress= L"net.tcp://localhost:4502/TestAddress";
extern "C" __declspec(dllexport) const WCHAR* GetAddress()
{
return szAddress;
}
我真的不认为这里有什么争议。我唯一能想到的是 GetAddress 的 const 返回,但我不确定如何将相应的关键字应用于 C#,因为我还不太熟悉该语言。
任何建议将不胜感激。
I'm attempting to access a function in a DLL in C# and C++.
C++ is working fine, as is C# on WinXP. However I'm getting the following error when attempting to access the function on a Win2k8 system:
Unhandled Exception: System.AccessViolationException: Attempted to read or write protected memory. This is often an indication that other memory
is corrupt.
at Router.GetAddress()
The declaration in C# is:
[DllImport("Constants.dll")]
static extern String GetAddress();
Usage in C# (at the moment) is just outputting it:
Console.WriteLine(GetAddress());
And the contents of the DLL's function are just:
const static WCHAR* szAddress= L"net.tcp://localhost:4502/TestAddress";
extern "C" __declspec(dllexport) const WCHAR* GetAddress()
{
return szAddress;
}
I really didn't think there was anything controversial here. The only thing I can think of is the const return from GetAddress, but I'm not sure how to apply the corresponding keyword to C# as I'm not as familiar with that language yet.
Any suggestions would be greatly appreciated.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我最终使用 http://social.msdn.microsoft.com/Forums/en-US/csharpgeneral/thread/4e387bb3-6b99-4b9d-91bb-9ec00c47e3a4。
我将声明更改为:
用法因此变为:
并且 DLL 更改为:
I ended up fixing this problem using the details in http://social.msdn.microsoft.com/Forums/en-US/csharpgeneral/thread/4e387bb3-6b99-4b9d-91bb-9ec00c47e3a4.
I changed the declaration to:
The usage therefore became:
And the DLL was changed to: