Reputation: 61
First, please forgive my bad english ;)
I want to use the windows function ToUnicode (http://msdn.microsoft.com/en-us/library/windows/desktop/ms646320(v=vs.85).aspx) to convert a virtual key code into an unicode char with C++.
But everytime I call that function, Windows7 says "MyProcess funktioniert nicht mehr" - I don´t know the english eqiuvalent, perhaps it´s "MyProcess does not work any more"?! - and I have the choice to debug MyProcess or to Close.
However my gdb debugger shows that I get a SIGSEGV, which tells me there is a data access violation I think.
This is the code I use:
BYTE kbd[256];
GetKeyboardState(kbd);
UINT vk = 65; //vitual key represents 'a'
UINT sc = 30; //scan code represents 'a'
LPWSTR chars;
ToUnicode(vk,sc,kbd,chars,2,0);
I even tried the ToAscii function and i get the same error : SIGSEGV
Can you please help me? =)
Upvotes: 0
Views: 1446
Reputation: 11295
ToUnicode expects a buffer where it can write the translation result to.
You give it an unitialized pointer pointing somewhere (random) in your memory - this is where the segfault is coming from, ToUnicode tries to write to memory which doesn't belong to your process.
See the following example for correct usage of this function
//Just a snippet showing the initialization the buffer part
const int BUFFER_LENGTH = 2; //Length of the buffer
WCHAR chars[BUFFER_LENGTH];
ToUnicode(vk,sc,kbd,chars,BUFFER_LENGTH,0);
Upvotes: 2