C++ put byte array to function

1

Why if I send a command to 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";

result is ok!

But if I do this, I get an error in result:

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... });
c++
byte
asked on Stack Overflow Apr 17, 2017 by lampa • edited Dec 7, 2018 by Cœur

2 Answers

0

In your first code example you're dealing with variables of array type, and the sizeof of such a type is the (compile time constant known) size of the array in units of elements with CHAR_BITS bits.

In the second code example you're not working on variables of array type but on variables of pointer type. The size of a pointer is independent of the (possibly at compile time unknown) size of the array it may point to.

To solve this you need to pass also the message size to your function. From within your function use that size as well as the output buffer size instead of those sizeof expressions.

answered on Stack Overflow Apr 17, 2017 by Daniel Jour • edited Apr 17, 2017 by Daniel Jour
0

This code is working:

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";
}
answered on Stack Overflow Apr 17, 2017 by lampa

User contributions licensed under CC BY-SA 3.0