James Andrew
James Andrew

Reputation: 7243

C++ printf WORD* (ToAscii)

I am using hooks and I have a vkCode and a scanCode so I am using the command ToAscii() as such:

...
LPWORD wCharacter;
ToAscii(kbdStruct.vkCode, kbdStruct.scanCode, keyboard_state, wCharacter, 0);

So now wCharacter should hold the Ascii character right?

How can I print that out?

I tried: printf(wCharacter); but it says: "cannot convert 'WORD*' to 'const char*'"

What am I doing wrong? How do I print out a WORD*? Or was I doing the ToAscii command wrong?

Upvotes: 1

Views: 5038

Answers (3)

JMS
JMS

Reputation: 181

LPWORD wCharacter;
is a Long Pointer to a word. Word here as an integer of two bytes not a "word" in a sentence. It's basically an int16. printf would handle that as.

printf("%hd", *wCharacter );
  1. where h spefifies a 16 bit value
  2. d specifies an integer
  3. *wCharacter is the dereferenced pointer or value of you int.

if you wanted your printed value to reflect it as unsigned

printf("%hu", *wCharacter );

unsigned hex

printf("%hx", *wCharacter );

unsigned hex with capital letters

printf("%hX", *wCharacter );

Now having said all that it's possible your WORD int is a Unicode character or two byte character as apposed to a normal 8 bit standard char.

In unicode if you are still representing standard ascii characters as apposed to some Arabic or Chinese characters you can translate the unicode character into a standard char by ignoring the first byte.

LPWORD wCharacter;
char *pChar = (char*)wCharacter;

printf("%c", pChar[1]);
  1. Declare pChar a pointer to an 8 bit value (char).
  2. Set the pChar address to your WORD pointer.
  3. Use the pChar pointer as an array and incremented it to the second byte [1];

This works if you aren't using international character set.

Upvotes: 0

Hans Passant
Hans Passant

Reputation: 942000

You are not going to get far with this, you are passing an uninitialized pointer to ToAscii(). Proper code should look like this:

WORD wCharacter[2];
int len = ToAscii(kbdStruct.vkCode, kbdStruct.scanCode, keyboard_state, wCharacter, 0);
if (len == 1) printf("%c", wCharacter[0]);
if (len == 2) printf("%c", wCharacter[1]);

This ought to compile and work, somewhat. In practice you cannot get this reliable. The *keyboard_state* variable you pass should be the keyboard state of the process that owns the foreground window. And you should pay attention to the keyboard layout that's active for that process (see ToAsciiEx). That cannot be made to work with a low-level keyboard hook. A keyboard logger must use a WH_CALLWNDPROC hook instead to intercept WM_CHAR messages (I think, never wrote one). Much harder to get right, that requires a DLL that can be injecting into other processes. You are of course inventing a wheel, buy and not build is the best advice. Also would make your users a bit more comfortable about your intentions.

Upvotes: 1

Petr Baudis
Petr Baudis

Reputation: 1198

WORD nor ToAscii() is standard C++, so answering this is kind of tricky. However, there are two issues anyhow:

  • printf() first argument should be a format string. You did not supply any.
  • String is a sequence of characters terminated by a zero byte. If you want to print out a single character passed as a parameter, assuming that WORD is something int-ish, you can use "%c" format string.

Upvotes: 1

Related Questions