-2

I've got a C project that I'm working on and I'm having a problem. The program reads a string that is echoed by a .php page. It uses this code to read the data and appoint it to a variable, which get sent to the Commands() function:

LPSTR szBuffer=(LPSTR)chunk+0x1000;
DWORD dwRead;

if (CWA(_HttpSendRequestA, wininet, hHttpRequest, szHeaders, lpstrlenA(szHeaders), szReq, lpstrlenA(szReq)) != 0)
    {
        CWA(_InternetReadFileA, wininet, hHttpRequest, szBuffer, 0x400, &dwRead);
        if (dwRead)
            Commands((LPBYTE)szBuffer, dwRead);
    }

As you can see the data is sent to the Commands() function, which receives the LPBYTE szBuffer (named "command" in the function) and the DWORD dwRead (named "size" in the function).

In the Commands() function, it's supposed to read the string that it read from the .php page. However, since the data seems to be stored as LPBYTE, I've done a lot of things trying to get that to a char*. When I thought I had finally got it however, I tried outputting it using a MessageBox (to see if it displays the string it should have read). However, this returns me Chinese characters (while the original string should be this:

"TASKci=0C73CCFD206BBD011E7087CE0806E6F6E69630,job=dlex,ti=AD62A5950B76F3812C542C24040EACE9,pr=https,ur=//test.com/test.txt,cl=".

Screenshot of what it returns me: http://prntscr.com/h0p5iw

How the code inside Commands() looks:

BOOL Commands(LPBYTE command, DWORD size) {

LPTSTR x = (LPTSTR)((char*)command);

{

        int msgboxID = MessageBox(
        NULL,
        x,
        (LPCWSTR)L"Woop",       
        MB_ICONWARNING | MB_CANCELTRYCONTINUE | MB_DEFBUTTON2 );

}


CWA(Sleep, kernel32, 100);

return 1; }

I'm new at C (I've only written stuff in C# before) so I am sorry if I am asking any dumb questions, I've really tried my best but I cannot seem to find any solution by myself.

Also, keep in mind that everything except for the stuff inside the Commands() function is not coded by me but by someone who is way more experienced. That code should be fine and I am sure that it is reading the data from the page, it's probably just me screwing up a conversion somewhere.

halfer
  • 19,824
  • 17
  • 99
  • 186
Tsukirid
  • 33
  • 5
  • What is `LPSTR szBuffer=(LPSTR)chunk+0x1000`? Do you mean `char szBuffer[0x1000]={0}`? What are these `CWA` functions? It seems your project is Unicode but you are forcing ANSI functions. You are hiding some errors with `(LPTSTR)` cast. You should use WinAPI functions, example `HttpSendRequest`. Or `HttpSendRequestA` and if you want to force ANSI. Your code includes a lot of macros, casts, and unconventional code. `LPTSTR` and other `T` macros can be very confusing to beginners, try to avoid them. – Barmak Shemirani Oct 23 '17 at 01:55
  • Ask that someone who is way more experienced about the character encoding they chose. You'll need to know to properly convert to a wide character string using `MultiByteToWideChar`. Make sure to read [The Absolute Minimum Every Software Developer Absolutely, Positively Must Know About Unicode and Character Sets (No Excuses!)](https://www.joelonsoftware.com/2003/10/08/the-absolute-minimum-every-software-developer-absolutely-positively-must-know-about-unicode-and-character-sets-no-excuses/) as well. – IInspectable Oct 23 '17 at 06:15
  • Your current approach to type mismatch errors seems to be lie to the compiler by casting A to B even when A is not of type B. This never works out. You need to attempt to understand compiler errors. Stop ignoring them. – David Heffernan Oct 23 '17 at 09:07

1 Answers1

3

A narrow string (char*) tends to look like Chinese when you use it somewhere that expects a wide UTF-16 Unicode string.

You cannot just cast the string to change its type, you need to call MultiByteToWideChar.

Anders
  • 97,548
  • 12
  • 110
  • 164
  • Alternatively, call `MessageBoxA`, and have the system perform the conversion, iff the source string uses ANSI encoding (with the thread's current code page) or ASCII encoding. – IInspectable Oct 23 '17 at 08:15