1

I am memory reading a string from a Japanese game and I am trying to display it in the console window and copy the string to clipboard. So far I've managed to display it in console, but whenever I try to copy it to my clipboard, the string gets distorted. I've tried to search the problem, but it was mostly people who were struggling displaying the string in console, something I have no problems with.

char StrTxt[500];
// TextAddress: The memory address of the string
ReadProcessMemory(hProcess, (LPCVOID)(TextAddress), &StrTxt, sizeof(StrTxt) / sizeof(*StrTxt), 0);
toClipboard(StrTxt);
cout << StrTxt << endl;

The Japanese text is correctly displayed in the console after cout, however the clipboard value that is being stored looks something like: y•zuƒXƒ~. I have no clue why it works fine when I output it into the console, because the string that is being read from memory is also distorted just as it appears in the clipboard. Here's the clipboard function if it will be of some help:

void toClipboard(const string &s) {
OpenClipboard(0);
EmptyClipboard();
HGLOBAL hg = GlobalAlloc(GMEM_MOVEABLE, s.size());
if (!hg) {
    CloseClipboard();
    return;
}
memcpy(GlobalLock(hg), s.c_str(), s.size());
GlobalUnlock(hg);
SetClipboardData(CF_TEXT, hg);
CloseClipboard();
GlobalFree(hg);
}

I've also tried handling the string using wchar_t and wstring, but I then got completely different symbols than expected.

My System Locale is currently set to Japanese if it matters.

What am I doing wrong here?

Gera
  • 65
  • 5
  • What do you mean by "stored in a clipboard"? How do you read it? In the clipboard you just have bytes. You need something to render them to actually see it. Maybe interpreting the clipboard goes wrong. You also titled the question that you have trouble outputting Japanese characters, but in question you say it works @_@... – luk32 May 17 '15 at 21:56
  • By outputting I meant that when I try to paste the characters from the clipboard (CTRL+V) I get a distorted string. In addition, the string is distorted to begin with when I read it from the process memory, but mystically becomes a readable string once outputted to the console via cout. – Gera May 17 '15 at 22:04
  • The actual byte data is (probably) being copied appropriately, but the character encoding used to interpret it is not what it should be. Japanese software might use several possible character sets, but these days I would try UTF-8 first. Other less likely possibilities are JIS, Shift-JIS, or EUC. For your users to have things correctly displayed in their console, their console would need to be set up appropriately. Same for the target of a paste command. It's not a programming problem. – Dave May 17 '15 at 22:58
  • Not sure what you meant by "try UTF-8" but I've just tried to set clipboard data with a CF_UNICODE parameter instead of CF_TEXT, the result is that I got a string full of random Japanese characters in my clipboard. – Gera May 17 '15 at 23:06

0 Answers0