Solved

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

Posted on 2006-07-21
24
1,093 Views
Last Modified: 2008-01-09
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);
0
Comment
Question by:iphone
  • 14
  • 7
  • 2
  • +1
24 Comments
 
LVL 7

Expert Comment

by:nafis_devlpr
ID: 17159027
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
 
LVL 7

Expert Comment

by:nafis_devlpr
ID: 17159068
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
 

Author Comment

by:iphone
ID: 17159099
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
 
LVL 7

Expert Comment

by:nafis_devlpr
ID: 17159129
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
 
LVL 7

Expert Comment

by:nafis_devlpr
ID: 17159143
try to use CString instead of _TCHAR array. if UNICODE is defined it will automatically change the characters to w_char
0
 

Author Comment

by:iphone
ID: 17159216
I cant use CString because my project is win32 project, is there any way to use CString in win32 project?
0
 
LVL 12

Expert Comment

by:rajeev_devin
ID: 17159231
>> 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
 
LVL 7

Expert Comment

by:nafis_devlpr
ID: 17159232
just #include <afx.h>, and u will be able to use CString
0
 

Author Comment

by:iphone
ID: 17159483
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
 
LVL 12

Expert Comment

by:rajeev_devin
ID: 17159514
>> 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
 

Author Comment

by:iphone
ID: 17159541
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
 
LVL 7

Expert Comment

by:nafis_devlpr
ID: 17159553
windows.h is already defined in afx.h, so if you remove yours there shouldn't be any problem
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 7

Expert Comment

by:nafis_devlpr
ID: 17159567
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
 

Author Comment

by:iphone
ID: 17159587
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
 
LVL 7

Expert Comment

by:nafis_devlpr
ID: 17159596
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
 
LVL 7

Expert Comment

by:nafis_devlpr
ID: 17159605
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
 

Author Comment

by:iphone
ID: 17159642
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
 
LVL 7

Accepted Solution

by:
nafis_devlpr earned 500 total points
ID: 17159688
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
 
LVL 7

Expert Comment

by:nafis_devlpr
ID: 17159694
dont forget to delete buffer using:
delete[] buffer;
0
 
LVL 7

Expert Comment

by:nafis_devlpr
ID: 17159701
#define UNICODE should be defined before including any of the header files
0
 

Author Comment

by:iphone
ID: 17159708
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
 
LVL 7

Expert Comment

by:nafis_devlpr
ID: 17159718
it forces the wsprintf to convert the ascii character string to a unicode character string
0
 
LVL 7

Expert Comment

by:nafis_devlpr
ID: 17159725
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
 
LVL 86

Expert Comment

by:jkr
ID: 17160523
Well, first of all, what are you trying to read and what "funny" character (ASCII code, please) are you getting?
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Errors will happen. It is a fact of life for the programmer. How and when errors are detected have a great impact on quality and cost of a product. It is better to detect errors at compile time, when possible and practical. Errors that make their wa…
What is C++ STL?: STL stands for Standard Template Library and is a part of standard C++ libraries. It contains many useful data structures (containers) and algorithms, which can spare you a lot of the time. Today we will look at the STL Vector. …
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

948 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now