自定义dll的PInvoke问题 [英] PInvoke issues with custom dll
本文介绍了自定义dll的PInvoke问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我只是在玩一些PInvoke的东西.尝试运行应用程序时出现AcessViolationException错误.这是我的C ++代码"
I am just playing around with some PInvoke stuff. I am getting an AcessViolationException error when I am trying to run the app. This is my C++ code"
extern "C"
{
__declspec(dllexport) double DecodeLatitude(string s, bool mustFlip)
{
bitset<28> hh(s);
if (mustFlip) {hh.flip();}
return hh.to_ulong();
}
}
这是我的C#代码,调用此代码:
And here is my C# code that calls this:
[DllImport("BinaryConvert.dll", CallingConvention = CallingConvention.Cdecl)]
public static extern double DecodeLatitude([MarshalAs(UnmanagedType.BStr)]string s, [MarshalAs(UnmanagedType.I1)]bool mustFlip);
static void Main()
{
double a = DecodeLongitude("001000110010001110111001100", false);
Console.ReadLine();
}
有人可以帮助初学者吗:P
Can someone help a beginner out :P
推荐答案
您需要检查C#和C ++之间不同类型的定义.例如,在此处定义的UnmanagedType.LPStr
[
You need to check the definitions for the different types between C# and C++. For example,UnmanagedType.LPStr
as defined here[^], is a pointer to a null-terminated array of ANSI characters. This equates to achar*
in C++, not astring
.
Similarly, you are returning anunsigned long (hh.to_ulong())
to a C# call that expects aDouble
value to be returned.
这篇关于自定义dll的PInvoke问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文