InternetReadFile function return funny character, how to convert it back?

I use wininet to read html file from remote URL, it works but it return funny character, any idea how to convert it back to normal character?
Here is the code:


   LPTSTR dataBuffer;
   DWORD  dwSize;       // size of the data available
   DWORD  dwDownloaded; // size of the downloaded data

   do
   {
       if (InternetQueryDataAvailable(inetSession,&dwSize,0,0))
       {
           dataBuffer = new TCHAR[dwSize+1];

           if (InternetReadFile(inetSession, (LPVOID)dataBuffer, dwSize, &dwDownloaded))
           {
               dataBuffer[dwDownloaded]='\0';

               MessageBox(NULL, (LPTSTR)dataBuffer, L"hehe", MB_OK);
           }

           delete[] dataBuffer;

           if (dwDownloaded == 0)
               break;
       }
       else
       {
           break;
       }
   }while (true);
iphoneAsked:
Who is Participating?
 
nafis_devlprConnect With a Mentor Commented:
now i got the problem, the problem is you are getting the downloaded data in ascii format while you are trying to show it in UNICODE, what you have to do is this

#define UNICODE
#include<tchar.h>

   LPTSTR dataBuffer;
   char *buffer;
   DWORD  dwSize;       // size of the data available
   DWORD  dwDownloaded; // size of the downloaded data

   do
   {
       if (InternetQueryDataAvailable(inetSession,&dwSize,0,0))
       {
            dataBuffer = new TCHAR[dwSize+1];
            buffer=new char[dwSize+1];
            buffer[0]=0;
            dataBuffer[0]=0;
            dwDownloaded=0;
            //checks if nothing downloaded than this should not be executed, in previouse cases it does as
            //dwDownloaded was not initialized
           if (InternetReadFile(inetSession, (LPVOID)buffer, dwSize, &dwDownloaded) && dwDownloaded != 0)
           {
               buffer[dwDownloaded]=0;
               wsprintf(dataBuffer, L"%hs", buffer);
               MessageBox(NULL, (LPCTSTR)dataBuffer, L"hehe", MB_OK);
           }
          delete[] dataBuffer;
           if (dwDownloaded == 0)
               break;
       }
       else
       {
           break;
       }
   }while (true);

this should work now

Nafis
0
 
nafis_devlprCommented:
probably the error is here

MessageBox(NULL, (LPTSTR)dataBuffer, L"hehe", MB_OK);

change this line to this

MessageBox(NULL, (LPCTSTR)dataBuffer, "hehe", MB_OK);

other things seems logivally ok.

Hope this helps

Nafis
0
 
nafis_devlprCommented:
you can change the function in the following way

   LPTSTR dataBuffer;
   DWORD  dwSize;       // size of the data available
   DWORD  dwDownloaded; // size of the downloaded data

   do
   {
       if (InternetQueryDataAvailable(inetSession,&dwSize,0,0))
       {
            dataBuffer = new _TCHAR[dwSize+1];
            dataBuffer[0]='\0';
            dwDownloaded=0;
            //checks if nothing downloaded than this should not be executed, in previouse cases it does as
            //dwDownloaded was not initialized
           if (InternetReadFile(inetSession, (LPVOID)dataBuffer, dwSize, &dwDownloaded) && dwDownloaded != 0)
           {
               dataBuffer[dwDownloaded]='\0';

               MessageBox(NULL, (LPCTSTR)dataBuffer, "hehe", MB_OK);
           }
          delete[] dataBuffer;
           if (dwDownloaded == 0)
               break;
       }
       else
       {
           break;
       }
   }while (true);

hope this helps

Nafis
0
Cloud Class® Course: Microsoft Exchange Server

The MCTS: Microsoft Exchange Server 2010 certification validates your skills in supporting the maintenance and administration of the Exchange servers in an enterprise environment. Learn everything you need to know with this course.

 
iphoneAuthor Commented:
I tried this but still give me the same thing
MessageBox(NULL, (LPCTSTR)dataBuffer, "hehe", MB_OK);

it gives me chinese characters that is not readable,
oh ya im using unicode in the program, does it have anything to do with this weirdness?



0
 
nafis_devlprCommented:
at the top of your source code file define this

#ifndef _UNICODE
   #define _UNICODE
#endif

and try to use the second code i posted

Nafis
0
 
nafis_devlprCommented:
try to use CString instead of _TCHAR array. if UNICODE is defined it will automatically change the characters to w_char
0
 
iphoneAuthor Commented:
I cant use CString because my project is win32 project, is there any way to use CString in win32 project?
0
 
rajeev_devinCommented:
>> I cant use CString because my project is win32 project, is there any way to use CString in win32 project?
Use wchar_t or wstring. Whichever you like.

If you use wchar_t then you have to reserve the space in advance.
wchar_t str[1024];

But its better to use wstring, since you are not bothered about the allocation/deallocation.

wstring str;
0
 
nafis_devlprCommented:
just #include <afx.h>, and u will be able to use CString
0
 
iphoneAuthor Commented:
i include <afx.h> but it give me this:
Fatal error C1189: #error :  WINDOWS.H already included.  MFC apps must not #include <windows.h>

its because my project is not a MFC project i guess, is there any way around this?
0
 
rajeev_devinCommented:
>> its because my project is not a MFC project i guess, is there any way around this?
remove
#include <windows.h> from your project and simply keep <afx.h>
0
 
iphoneAuthor Commented:
no i cant remove that, my whole project depends on windows.h, is there any other way to fix the funny character?
I think it might be translating it into a unicode, any way to convert it back to ansi?
0
 
nafis_devlprCommented:
windows.h is already defined in afx.h, so if you remove yours there shouldn't be any problem
0
 
nafis_devlprCommented:
if you still need to include windows.h file then do this

#include <afx.h>
#include <windows.h>

include the afx.h file before any of the header files, then include windows.h
0
 
iphoneAuthor Commented:
I try it just now, still can not...
Is there anyway to do it without CString? sorry im giving you a lot of problem, its because im doing it in Win32 instead of MFC..
Is there any other way to fix the character? im guessing its because of the Unicode..
0
 
nafis_devlprCommented:
ok try this

#define _UNICODE

   LPTSTR dataBuffer;
   DWORD  dwSize;       // size of the data available
   DWORD  dwDownloaded; // size of the downloaded data

   do
   {
       if (InternetQueryDataAvailable(inetSession,&dwSize,0,0))
       {
            dataBuffer = new _TCHAR[dwSize+1];
            dataBuffer[0]=0;
            dwDownloaded=0;
            //checks if nothing downloaded than this should not be executed, in previouse cases it does as
            //dwDownloaded was not initialized
           if (InternetReadFile(inetSession, (LPVOID)dataBuffer, dwSize, &dwDownloaded) && dwDownloaded != 0)
           {
               dataBuffer[dwDownloaded]=0;

               MessageBox(NULL, (LPCTSTR)dataBuffer, "hehe", MB_OK);
           }
          delete[] dataBuffer;
           if (dwDownloaded == 0)
               break;
       }
       else
       {
           break;
       }
   }while (true);

let me know something does the caption "hehe" shows correctly??

Nafis
0
 
nafis_devlprCommented:
do you know what the message box show?? if yes then what are they and what the message box is actually showing. i.e what the message box should show and what they are actually showing.

if the caption "hehe" is not showing properly then do this

MessageBox(NULL, (LPCTSTR)dataBuffer,  _TCHAR("hehe"), MB_OK);

let me know the results

Nafis
0
 
iphoneAuthor Commented:
MessageBox(NULL, (LPCTSTR)dataBuffer,  _TCHAR("hehe"), MB_OK);  <== can not compile

this one can compile:
MessageBox(NULL, (LPCTSTR)dataBuffer,  L"hehe", MB_OK);



the hehe does show, but the message show some weird chinese character, 4 characters, while it suppose to show "hehe" too because the remote file content is hehe.

0
 
nafis_devlprCommented:
dont forget to delete buffer using:
delete[] buffer;
0
 
nafis_devlprCommented:
#define UNICODE should be defined before including any of the header files
0
 
iphoneAuthor Commented:
wow it works now!! :D

im truly grateful to you nafis, thx a lots!! :D

have a nice weekend!!

btw, what is the %hs for??
0
 
nafis_devlprCommented:
it forces the wsprintf to convert the ascii character string to a unicode character string
0
 
nafis_devlprCommented:
sorry forgot to add that %hs also tell wsprintf that the corresponding value is a ascii or 8bit character string, so wsprintf just map the ascii characters from the ascii table and then map them to unicode characters.

Hope all these advices helped you

Nafis
0
 
jkrCommented:
Well, first of all, what are you trying to read and what "funny" character (ASCII code, please) are you getting?
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.