如何通过 Pinvoke 将指针传递给结构体?

发布于 2024-08-21 21:20:34 字数 1068 浏览 8 评论 0 原文

我正在尝试编写与以下内容等效的 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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

自在安然 2024-08-28 21:20:34

您可能最好使用智能设备框架,它具有电池控制到位。请参阅此处获取社区版的下载链接。

编辑:如果您仍然想要该结构的 pinvoke 等效项,请查看此处:

[StructLayout(LayoutKind.Sequential, Pack = 1)]
public struct BATT_ID
{
    [System.Runtime.InteropServices.MarshalAsAttribute(System.Runtime.InteropServices.UnmanagedType.ByValArray, SizeConst = 8, ArraySubType = System.Runtime.InteropServices.UnmanagedType.I8)]
      public int[] nBattId;
};

然后在 p/invoking 之前,您需要 'DeviceIoControl' 如图所示:

[DllImport("coredll.dll", EntryPoint="DeviceIoControl", SetLastError=true)]
        internal static extern int DeviceIoControlCE(
            int hDevice, 
            int dwIoControlCode, 
            byte[] lpInBuffer, 
            int nInBufferSize, 
            byte[] lpOutBuffer, 
            int nOutBufferSize, 
            ref int lpBytesReturned, 
            IntPtr lpOverlapped);

调用将如下所示:

IntPtr ptr = IntPtr.Zero;
BATT_ID battId;
int sz = Marshal.SizeOf(battId.GetType());
ptr = Marshal.AllocHGlobal(sz);
Marshal.StructureToPtr((BATT_ID)battId, ptr, false);
byte[] pBattId = ptr.ToPointer();
out int bytesReturned = 0;
DeviceIoControl(handle, IOCONTROL_ID, null, 0, pBattId, sz, ref bytesReturned, IntPtr.Zero);
battId = Marshal.PtrToStructure(ptr, battId.GetType());
Marshal.FreeHGlobal(ptr);

我希望我有这个权利...

编辑#2:ctacke (谢谢!)指出我的代码示例是错误的......

unsigned byte[8] battId;
DeviceIoControl(g_hDevice, SOMO650_PWR_GET_BATT_ID, null, 0, battId, Marshal.SizeOf(battId), ref bytesReturned, IntPtr.Zero);

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:

[StructLayout(LayoutKind.Sequential, Pack = 1)]
public struct BATT_ID
{
    [System.Runtime.InteropServices.MarshalAsAttribute(System.Runtime.InteropServices.UnmanagedType.ByValArray, SizeConst = 8, ArraySubType = System.Runtime.InteropServices.UnmanagedType.I8)]
      public int[] nBattId;
};

Then prior to p/invoking, you need the signature for the 'DeviceIoControl' as shown:

[DllImport("coredll.dll", EntryPoint="DeviceIoControl", SetLastError=true)]
        internal static extern int DeviceIoControlCE(
            int hDevice, 
            int dwIoControlCode, 
            byte[] lpInBuffer, 
            int nInBufferSize, 
            byte[] lpOutBuffer, 
            int nOutBufferSize, 
            ref int lpBytesReturned, 
            IntPtr lpOverlapped);

The call would look like this:

IntPtr ptr = IntPtr.Zero;
BATT_ID battId;
int sz = Marshal.SizeOf(battId.GetType());
ptr = Marshal.AllocHGlobal(sz);
Marshal.StructureToPtr((BATT_ID)battId, ptr, false);
byte[] pBattId = ptr.ToPointer();
out int bytesReturned = 0;
DeviceIoControl(handle, IOCONTROL_ID, null, 0, pBattId, sz, ref bytesReturned, IntPtr.Zero);
battId = Marshal.PtrToStructure(ptr, battId.GetType());
Marshal.FreeHGlobal(ptr);

I hope I have this right...

Edit#2: As ctacke (thanks!) pointed out my code sample is wrong...

unsigned byte[8] battId;
DeviceIoControl(g_hDevice, SOMO650_PWR_GET_BATT_ID, null, 0, battId, Marshal.SizeOf(battId), ref bytesReturned, IntPtr.Zero);
小糖芽 2024-08-28 21:20:34

基本上你有一个 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文