Abhineet
Abhineet

Reputation: 6627

Getting Junk text. How to convert character array variable to LPARAM type?

I need to display the text on Text Box whatever data retrieve from files.

On push of particular button (IDB_SHOW_BUTTON) in windows application, I am doing as mentioned below:-

case IDB_SHOW_BUTTON:{  
   char buf[1000];  
   vReadFileFromHardisk(buf); //storing the read data of file to buffer
   SendMessage(editHwnd,WM_SETTEXT,NULL,(LPARAM)buf);//Due to this, I get junk text.
}
break;

I am reading data from file and storing it into buffer. I don't understand why i am getting junk text whenever I am clicking on the button.

When I change the 4th parameter of SendMessage i.e. as mentioned below, I get the proper output on push of a particular button:-

SendMessage(editHwnd,WM_SETTEXT,NULL,(LPARAM)L"My First Edit Window");

Please let me know to how to display the proper text in case of storing data to buf and sending the same through SendMessage.

Thanks in advance.

Upvotes: 1

Views: 1111

Answers (1)

Rup
Rup

Reputation: 34418

See the 'L' in the working example? You're passing a narrow character string into a function that expects Unicode.

You'll need to either

  1. convert the text you've read in into Unicode - you can do this using MultiByteToWideChar amongst others; you'll need to know what encoding the input is, e.g. UTF-8
  2. send the narrow form of the message, WM_SETTEXTA, to use the current system encoding for the data you've read
  3. change your program settings from Unicode to Multi-byte (but don't do this)

I strongly recommend 1 and getting used to using Unicode (a.k.a. UTF-16) throughout your program.

Upvotes: 2

Related Questions