0
LPWSTR data[256];
ToUnicode(vk_code, 0, 0, *data, 256, 0);

This code throws the following exception:

Access violation writing location 0xCCCCCCCC.

The vk_code value seemed to be perfectly normal at the breakpoint, and I checked with msdn to make sure my arguments were valid. What could I be doing wrong?

  • 2
    It's worth properly learning about arrays and pointers before trying to fumble with the Windows API. You're giving it an uninitialized pointer. – chris Sep 20 '17 at 03:34
  • @chris Will do, when looking into the `0xCCCCCCCC` address, it turns out to be a memory location used for debugging uninitialized values. – Michael Smith Sep 20 '17 at 14:56
  • That's not correct. The 'address' `0xCCCCCCCC` is not meaningful at all. `0xCC` is the pattern used to initialize uninitialized variables in a debug configuration. It helps you easily spot uninitialized variables during debugging. That actual value is not interesting, though. You'd get the very same exception when passing `nullptr` in place of `*data`. – IInspectable Sep 20 '17 at 19:40

1 Answers1

1

You are passing an uninitiated pointer to the pwszBuff parameter. It expects a pointer to an allocated array of characters. Change your array to use WCHAR elements instead of LPWSTR elements, and get rid of the * dereference operator:

WCHAR data[256];
int res = ToUnicode(vk_code, 0, 0, data, 256, 0);
switch (res) {
    case -1:
        // dead-key character, nothing written to data[]... 
        break;
    case 0:
        // no translation, nothing written to data[]... 
        break;
    default:
        // res # of characters written to data[]... 
        break;
}
Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770