views:

762

answers:

3

I have to convert the encoding of a string output of a VB6 application to a specific encoding.

The problem is, I don't know the encoding of the string, because of that:

According to the VB6 documentation when accessing certain API functions the internal Unicode strings are converted to ANSI strings using the default codepage of Windows.

Because of that, the encoding of the string output can be different on different systems, but I have to know it to perform the conversion.

How can I read the default codepage using the Win32 API or - if there's no other way - by reading the registry?

+2  A: 

GetSystemDefaultLCID() gives you the system locale.

If the LCID is not enough and you truly need the codepage, use this code:

  TCHAR szCodePage[10];
  int cch= GetLocaleInfo(
    GetSystemDefaultLCID(), // or any LCID you may be interested in
    LOCALE_IDEFAULTANSICODEPAGE, 
    szCodePage, 
    countof(szCodePage));

  nCodePage= cch>0 ? _ttoi(szCodePage) : 0;
Serge - appTranslator
A: 

That worked for me, thanks, but can be written more succinctly as:

UINT nCodePage = CP_ACP;
const int cch = ::GetLocaleInfo(LOCALE_SYSTEM_DEFAULT,
     LOCALE_RETURN_NUMBER|LOCALE_IDEFAULTANSICODEPAGE,
     (LPTSTR)&nCodePage, sizeof(nCodePage) / sizeof(_TCHAR) );
+3  A: 

It could be even more succinct by using GetACP - the Win32 API call for returning the default code page!

int nCodePage=GetACP();

Also many API calls (such as MultiByteToWideChar) accept the constant value CP_ACP (zero) which always means "use the system code page". So you may not actually need to know the current code page, depending on what you want to do with it.

MarkJ