我正在尝试编写与以下内容等效的 C#:
typedef struct BATT_ID
{
UINT8 nBattID[8];
} BATT_ID, *PBATT_ID;
HANDLE g_hDevice;
// Connect to the driver
g_hDevice = CreateFile(L"BAT1:", GENERIC_READ | GENERIC_WRITE, FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
void GetBattID(PBATT_ID pBattId)
{
// ... snipped code to check g_hDevice is valid ...
DeviceIoControl(g_hDevice, SOMO650_PWR_GET_BATT_ID, NULL, 0, pBattId, sizeof(BATT_ID), dwByteReturn, NULL))
}
// once BATT_ID has been filled it can be formatted as follows
wsprintf(strInfo, TEXT("%02X:%02X:%02X:%02X:%02X:%02X"), BattID.nBattID[6], BattID.nBattID[5], BattID.nBattID[4], BattID.nBattID[3], BattID.nBattID[2], BattID.nBattID[1]);
代码连接到 Windows Mobile 设备的电源驱动程序并尝试检索电池 ID。
这是针对 SoMo650 的最新 ROM 版本,Socket 只能提供 C 语言的示例代码。
除了调用 DeviceIoControl 之外,我可以成功地完成所有操作(据我所知),因为我不知道如何翻译 BATT_ID结构到 C# 中。
我猜想,因为它是一个结构体,而 DeviceIoControl 需要一个指针,所以我应该查看 Marshal.PtrToStructure(),但我的 C 经验很少,感觉超出了我的深度。
任何帮助将不胜感激。
I am trying to write the C# equivalent to the following:
typedef struct BATT_ID
{
UINT8 nBattID[8];
} BATT_ID, *PBATT_ID;
HANDLE g_hDevice;
// Connect to the driver
g_hDevice = CreateFile(L"BAT1:", GENERIC_READ | GENERIC_WRITE, FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
void GetBattID(PBATT_ID pBattId)
{
// ... snipped code to check g_hDevice is valid ...
DeviceIoControl(g_hDevice, SOMO650_PWR_GET_BATT_ID, NULL, 0, pBattId, sizeof(BATT_ID), dwByteReturn, NULL))
}
// once BATT_ID has been filled it can be formatted as follows
wsprintf(strInfo, TEXT("%02X:%02X:%02X:%02X:%02X:%02X"), BattID.nBattID[6], BattID.nBattID[5], BattID.nBattID[4], BattID.nBattID[3], BattID.nBattID[2], BattID.nBattID[1]);
The code is connecting to the power driver of a Windows Mobile device and attempting to retrieve the battery Id.
This is for the latest ROM version of the SoMo650 and Socket are only able to provide sample code in C.
I can successfully do everything (as best as I can tell) apart from calling DeviceIoControl as I don't know how to translate the BATT_ID structure into C#.
I'm guessing that as it's a structure and DeviceIoControl expects a pointer I shoould be looking at Marshal.PtrToStructure() but I have very little C experience and feel very out of my depth.
Any assitance would be greatly appreciated.
发布评论
评论(2)
您可能最好使用智能设备框架,它具有电池控制到位。请参阅此处获取社区版的下载链接。
编辑:如果您仍然想要该结构的 pinvoke 等效项,请查看此处:
然后在 p/invoking 之前,您需要 'DeviceIoControl' 如图所示:
调用将如下所示:
我希望我有这个权利...
编辑#2:ctacke (谢谢!)指出我的代码示例是错误的......
You might be better to use the Smart Device Framework which has a battery control in place..see here for the download link for the community edition.
Edit: If you still want the pinvoke equivalent of the structure look here:
Then prior to p/invoking, you need the signature for the 'DeviceIoControl' as shown:
The call would look like this:
I hope I have this right...
Edit#2: As ctacke (thanks!) pointed out my code sample is wrong...
基本上你有一个 8 字节的结构。只需将 8 个字节长的 byte[] 传递给 DeviceIoControl 调用即可。无需调用 AllocHGlobal,或执行任何其他古怪的封送处理。
Basically you have a structure that is 8 bytes. Just pass in a byte[] that is 8 bytes long to the DeviceIoControl call. There is no need to call AllocHGlobal, or do any other wacky marshaling.