|
|
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,也不需要执行任何其他古怪的封送处理。 |
|
|
Mike Bruno · 访问模拟帐户的私钥 9 年前 |
|
John · 通过P/Invoke使用回调和堆对象的安全方法 10 年前 |
|
|
Residuum · 带有P/Invoke和指针的泛型 10 年前 |
|
|
hl3mukkel · 约束与使用SafeHandle的抽象类 10 年前 |