【发布时间】:2011-06-15 19:48:47
【问题描述】:
我正在尝试在 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;
函数调用是:
BOOL GetCurOperatorInfo(LPOperatorInfo pinfo);
我在.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;
}
在看到一些文章和 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);
注意:我使用序号定义的入口点调用该函数,因为 c++ 破坏了名称。
我遇到的问题是我总是抛出 notSupportedException。我不明白,因为 ref 参数应该给它指向结构的指针。
调用它的我的 .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;
}
}
为此我缺少什么。
更新
新的.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);
【问题讨论】:
-
您确定需要序号吗?试试这个名字。几乎所有 C++ API 都会使用
extern "C"来表示未损坏的名称。 -
CGSMAdapter 听起来像一个类名,而不是命名空间名。是静态方法吗?
-
这是 pda 制造商为我们提供的 gsm/gprs 调制解调器适配器的类名,用于拨打电话、发送/接收短信和创建 gprs 连接。
标签: c# compact-framework pinvoke