我试图在.net紧凑框架中从设备制造商给我的非托管dll中调用此函数:
Bool GetModelInfo(LPTSTR pszInfo, DWORD dwInfoType);
infoType是以下枚举之一:
enum ModemInfoType{
Model_name,
Model_revision,
Model_IMEI,
Model_IMSI
};
我的实际pinvoke电话如下:
[System.Runtime.InteropServices.DllImportAttribute(gsmaAdapterDLLName, EntryPoint = "#36", CallingConvention = CallingConvention.Winapi)]
[return: System.Runtime.InteropServices.MarshalAsAttribute(System.Runtime.InteropServices.UnmanagedType.Bool)]
internal static extern bool GetModelInfo([System.Runtime.InteropServices.MarshalAs(System.Runtime.InteropServices.UnmanagedType.LPStr)] out string pszInfo, uint dwInfoType);
我知道他们必须构建字符串,只是不知道我是否应该传递stringBuilder而不是字符串。问题是当我尝试运行对函数的调用时,我得到一个NotSupportedException。
/// <summary>
/// Gets the modem info.
/// </summary>
/// <param name="modemInfo">The modem info.</param>
/// <param name="infoRequested">The info requested.</param>
/// <returns></returns>
public bool GetModemInfo(out string modemInfo, NativeHelper.ModemInfoType infoRequested)
{
String _mymodemInfo;
if (NativeImports.GetModelInfo(out _mymodemInfo, (uint)infoRequested) == true)
{
modemInfo = _mymodemInfo;
return true;
}
else
{
modemInfo = "";
return false;
}
}
这是我的包装函数调用本机方法
答案 0 :(得分:0)
按顺序打电话很脆弱,所以我首先怀疑这个电话,但考虑到这一点,我试试这个:
[DllImport(gsmaAdapterDLLName, EntryPoint = "#36")]
[return: MarshalAs(UnmanagedType.Bool)]
internal static extern bool GetModelInfo(StringBuilder pszInfo, ModemInfoType dwInfoType);
private string GetModelName()
{
StringBuilder sb = new StringBuilder(1024);
if (!GetModelInfo(sb, ModemInfoType.Name))
{
throw new Exception("Call failed");
}
return sb.ToString();
}