Converting multibyte array to Unicode

401 views Asked by At

I'm creating an FTP downloader using WinInet in a Univeral Windows program with Visual Studio 2019.

Visual Studio 2019 doesn't give an option to change the character set in the properties configuration tab.

I need to convert the following, which is in multibyte format, to Unicode:

CHAR * szHead[] = "Accept: */*\r\n\r\n";

The overall code comes from a previous older project using a compiler that had multi-byte character set options, and works fine as is. Unfortunately, I need to make this work in a compiler that has Windows Store support, which only has Unicode.

if (!(hConnect = InternetOpenUrlW(hOpen, szUrl, szHead, strlen(szHead), INTERNET_FLAG_DONT_CACHE, 0)))

I get the following errors:

Error (active)  E0520   initialization with '{...}' expected for aggregate object   
Error   C2440   'initializing': cannot convert from 'const wchar_t [16]' to 'LPCTSTR []'    
Error   C2664   'size_t strlen(const char *)': cannot convert argument 1 from 'LPCTSTR []' to 'const char *'    

From other research, these errors are arising because the compiler is expecting Unicode but is getting multibyte.

3

There are 3 answers

0
Remy Lebeau On

First off, CHAR * szHead[] = "Accept: */*\r\n\r\n"; does not compile. You are trying to initialize an array of non-const pointers using a single const pointer. You need to use this instead:

CHAR szHead[] = "Accept: */*\r\n\r\n";`

Or this:

const CHAR * szHead = "Accept: */*\r\n\r\n";

That being said, InternetOpenUrlW() takes WCHAR strings (hence the W in its name), not CHAR strings. Have you considered simply using InternetOpenUrlA() instead?

CHAR szHead[] = "Accept: */*\r\n\r\n";

if (!(hConnect = InternetOpenUrlA(..., szHead, -1, )))

If that is not an option, then you will have to either:

  • use MultiByteToWideChar() (or equivalent) to convert the CHAR string into a WCHAR string, eg:
CHAR szHead[] = "Accept: */*\r\n\r\n";
WCHAR wszHead[16];
...
MultiByteToWideChar(CP_ACP, 0, szHead, -1, wszHead, _countof(wszHead));
if (!(hConnect = InternetOpenUrlW(..., wszHead, -1, ...)))
  • or simply use WCHAR strings to begin with, eg:
WCHAR szHead[] = L"Accept: */*\r\n\r\n";
...
if (!(hConnect = InternetOpenUrlW(..., szHead, -1, ...)))
1
Deek880 On

This solved the error message

LPCWSTR szHead = L"Accept: /\r\n\r\n";

I don't know if the program works yet because I get 3 unresolved externals. Exactly the same error message that I would get in Visual Studio 6.0 if I unlinked Wininet.lib from the linker.

I need to find a way to link in Wininet.lib however this compiler does not give you any obvious or easy way to do that. I'm talking about the community version of visual studio 2019

0
Deek880 On

Actually you can add wininet.lib to in visual studio 2019. You go to add existing item and navigate to vc98 lib folder and click on wininet.lib and the compiler put it right into the build and the errors went away. Unfortunately, it seems that you can't use code that downloads other code in a store project as I find that the app aborts when the download starts and says operation not supported. There are easier and more current ways to download a file using c# but even with current code simple C# code the app aborts when a download starts saying operation not supported