Sor*_*6pt 2 c# pinvoke compact-framework
我试图在windows ce 6.0环境中调用.net cf 3.5中的非托管c ++ dll中的函数.
结构定义为:
typedef struct TagOperatorInfo
{
DWORD dwMode;
DWORD dwFormat; //Operator name format
DWORD dwAct; //Network type(Available in 3G module£ºGSM or 3G),
TCHAR szOper[32];
}OperatorInfo,*LPOperatorInfo;
Run Code Online (Sandbox Code Playgroud)
和函数调用是:
BOOL GetCurOperatorInfo(LPOperatorInfo pinfo);
Run Code Online (Sandbox Code Playgroud)
我在.net中定义了TagOperatorInfo,如下所示:
[System.Runtime.InteropServices.StructLayoutAttribute(System.Runtime.InteropServices.LayoutKind.Sequential, CharSet = System.Runtime.InteropServices.CharSet.Auto)]
public struct TagOperatorInfo
{
/// DWORD->unsigned int
public uint dwMode;
/// DWORD->unsigned int
public uint dwFormat;
/// DWORD->unsigned int
public uint dwAct;
/// TCHAR[32]
[System.Runtime.InteropServices.MarshalAsAttribute(System.Runtime.InteropServices.UnmanagedType.ByValTStr, SizeConst = 32)]
public string szOper;
}
Run Code Online (Sandbox Code Playgroud)
在看到一些文章和msdn文档后,我将本机函数称为:
[System.Runtime.InteropServices.DllImportAttribute(gsmaAdapterDLLName, EntryPoint = "#30", CallingConvention = CallingConvention.Winapi)]
[return: System.Runtime.InteropServices.MarshalAsAttribute(System.Runtime.InteropServices.UnmanagedType.Bool)]
internal static extern bool GetCurOperatorInfo([MarshalAs(UnmanagedType.LPStruct)] ref NativeHelper.TagOperatorInfo operatorInfo);
Run Code Online (Sandbox Code Playgroud)
注意:我使用序号定义的入口点调用该函数,因为c ++错位的名称.
我遇到的问题是我总是得到notSupportedException抛出.我不明白因为ref参数应该给它指向struct的指针.
我调用它的.net函数是:
/// <summary>
/// Gets the current operator information.
/// </summary>
/// <param name="operatorInfo">The operator info.</param>
/// <returns></returns>
public static bool GetOperatorInformation(out NativeHelper.TagOperatorInfo operatorInfo)
{
operatorInfo = new NativeHelper.TagOperatorInfo();
if (NativeImports.GetCurOperatorInfo(ref operatorInfo) == true)
{
return true;
}
else
{
return false;
}
}
Run Code Online (Sandbox Code Playgroud)
我错过了这个工作.
UPDATE
新的.Net Compact Framework方法调用
[System.Runtime.InteropServices.DllImportAttribute(gsmaAdapterDLLName, EntryPoint = "?GetCurOperatorInfo@CGSMAdapter@@YAHPAUTagOperatorInfo@@@Z", CallingConvention = CallingConvention.Winapi)]
[return: System.Runtime.InteropServices.MarshalAsAttribute(System.Runtime.InteropServices.UnmanagedType.Bool)]
internal static extern bool GetCurOperatorInfo([MarshalAs(UnmanagedType.LPStruct)]ref NativeHelper.TagOperatorInfo operatorInfo);
Run Code Online (Sandbox Code Playgroud)
您只能调用已导出为C样式函数的函数.据我所知,你无法通过P/Invoke直接调用C++函数,例如:
更新
实际上,看起来你可以在通过P/Invoke调用函数时使用受损的名称.这是我过去永远无法工作的东西,所以我站得更正了.使用名称而不是序数也应该更有弹性:
参考:未找到入口点例外
所以类似于:
[DllImport(gsmaAdapterDLLName,
EntryPoint="?GetCurOperatorInfo@CGSMAdapter@@YAHPAUTagOperatorInfo@@@Z",
ExactSpelling = true,
CallingConvention = CallingConvention.Cdecl)]
public static extern bool GetCurOperatorInfo(out TagOperatorInfo info);
Run Code Online (Sandbox Code Playgroud)
对于.Net CF:
DllImport(gsmaAdapterDLLName,
EntryPoint="?GetCurOperatorInfo@CGSMAdapter@@YAHPAUTagOperatorInfo@@@Z",
CallingConvention = CallingConvention.WinApi)]
public static extern bool GetCurOperatorInfo(out TagOperatorInfo info);
Run Code Online (Sandbox Code Playgroud)