为什么我向DeviceIoControl
发送命令:
byte lpInBuffer[44] = { 0x2C, 0x00, 0x00, 0x00, 0x01... };
byte lpOutBuffer[88];
BOOL result = DeviceIoControl(open, 0x0004D004, lpInBuffer, sizeof(lpInBuffer), &lpOutBuffer, sizeof(lpOutBuffer), 0x00000000, 0x00000000);
err = GetLastError(); //
cout << "result:" << result << ", err: " << err << "\n";
结果还可以!
但如果我这样做,结果会出错:
void putMessage(HANDLE handle, int type, int output_len, byte message[]) {
byte* lpOutBuffer = new byte[output_len];
byte* lpInBuffer = message;
for (int i = 0; i < sizeof(lpInBuffer); i++) {
cout << lpInBuffer[i];
}
BOOL result = DeviceIoControl(handle, type, lpInBuffer, sizeof(lpInBuffer), &lpOutBuffer, sizeof(lpOutBuffer), 0x00000000, 0x00000000);
int err = GetLastError();
cout << "result:" << result << ", err: " << err << "\n";
for (int i = 0; i < sizeof(lpOutBuffer); i++) {
cout << lpOutBuffer[i];
}
cout << "\n\n";
}
putMessage(open, 0x0004D004, 88, new byte[44]{ 0x2C, 0x00, 0x00, 0x00, 0x01... });
答案 0 :(得分:0)
在你的第一个代码示例中,你处理的是 array 类型的变量,而这种类型的sizeof
是(已知的编译时间常数)大小以CHAR_BITS
位的元素为单位。
在第二个代码示例中,您不处理数组类型的变量,而是处理指针类型的变量。指针的大小独立于它可能指向的数组的大小(可能在编译时未知)。
要解决此问题,您还需要将message
大小传递给您的函数。在函数中使用该大小以及输出缓冲区大小而不是那些sizeof
表达式。
答案 1 :(得分:0)
此代码正常运行:
void putMessage(HANDLE handle, int type, int output_size, vector<byte> input) {
vector<byte> output(output_size);
cout << "lpInBuffer:" << input.size() << ", lpOutBuffer: " << output.size() << "\n";
BOOL result = DeviceIoControl(handle, type, input.data(), input.size(), (void*)output.data(), output.size(), 0x00000000, 0x00000000);
int err = GetLastError(); // 5 i.e. ERROR_ACCESS_DENIED
cout << "result:" << result << ", err: " << err << "\n";
for (int i = 0; i < output.size(); i++) {
cout << hexToString(output[i]) << " ";
}
cout << "\n\n";
}