Solved

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

Posted on 2006-07-21
24
1,083 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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
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

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

  Included as part of the C++ Standard Template Library (STL) is a collection of generic containers. Each of these containers serves a different purpose and has different pros and cons. It is often difficult to decide which container to use and …
This article shows you how to optimize memory allocations in C++ using placement new. Applicable especially to usecases dealing with creation of large number of objects. A brief on problem: Lets take example problem for simplicity: - I have a G…
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…

757 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

21 Experts available now in Live!

Get 1:1 Help Now