Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I programmatically determine the current default codepage of Windows?

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?

like image 520
Daniel Rikowski Avatar asked May 26 '09 10:05

Daniel Rikowski


2 Answers

It could be even more succinct by using GetACP - the Win32 API call for returning the default code page! (Default code page is often called "ANSI")

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.

like image 88
MarkJ Avatar answered Nov 01 '22 23:11

MarkJ


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;
like image 43
Serge Wautier Avatar answered Nov 01 '22 22:11

Serge Wautier