|
|
1
5
您最好使用 Smart Device Framework 它有一个电池控制。请参阅此处以获取社区版的下载链接。 如果您仍然想要与该结构类似的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;
};
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: 作为 (谢谢!)指出我的代码示例是错误的。。。 unsigned byte[8] battId; DeviceIoControl(g_hDevice, SOMO650_PWR_GET_BATT_ID, null, 0, battId, Marshal.SizeOf(battId), ref bytesReturned, IntPtr.Zero); |
|
|
2
3
基本上你有一个8字节的结构。只需向DeviceIoControl调用传入一个8字节长的字节[]。不需要调用AllocHGlobal,也不需要执行任何其他古怪的封送处理。 |
|
|
A B · C#Excel自动调整列避免长文本时出错 1 年前 |
|
|
Megrez7 · C#ToArray转换合并为一行,导致数组元素更改 1 年前 |
|
Aycon · 在工厂方法中释放部分创建的对象的正确方法是什么? 1 年前 |
|
|
Sei · Avalonia/WPF将路由器传递到控制模板 1 年前 |