[Webinar] Streamline your web hosting managementRegister Today

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 917
  • Last Modified:

How to call Kernel32 GetCPInfoEx

I have the following (I trimmed the rest of the code) coded:

Public Type CPINFOEX
  MaxCharSize As Long
  DefaultChar() As Byte
  LeadByte() As Byte
  UnicodeDefaultChar As Byte
  CodePage As Integer
  CodePageName As String * 128
End Type

Public Declare Function GetCPInfoEx Lib "kernel32" Alias "GetCPInfoExA" (ByVal CodePage As Long, ByVal Flags As Long, lpCPInfo As CPINFO) As Long

    Dim cpi As Long
    Dim lpCharSet As CPINFOEX

     cpi = 0
     lpCharSet.CodePageName = Space$(128)
     cpi = GetCPInfoEx(oSQL_Server.CodePage, cpi, lpCharSet)

However it does not work. There seems to be a mismatch in the lpCPInfo structure. Can anyone tell me what the correct calling structure within VB is please.

I do not want to call the GetCPInfo DLL as its only the Extended version that gives me what I want (the Code Page Name given a code page number).

I took the definitions from the Windows SDK.

Also does anyone have a mapping between DLL Data types and VB data types. I know about the API viewer. But it only helps so far and does not list the GetCPInfoEx function at all.

Regards,

Graham

0
Felicitous
Asked:
Felicitous
  • 6
  • 3
1 Solution
 
Richie_SimonettiIT OperationsCommented:
I think structure is missed. Take a look at this:

CPINFOEX
[This is preliminary documentation and subject to change.]

The CPINFOEX structure contains information about a code page. It is used by the GetCPInfoEx function.

typedef struct _cpinfoex {
    UINT    MaxCharSize;
    BYTE    DefaultChar[MAX_DEFAULTCHAR];
    BYTE    LeadByte[MAX_LEADBYTES];
    WCHAR   UnicodeDefaultChar;
    UINT    CodePage;
    TCHAR   CodePageName[MAX_PATH];
} CPINFOEX *LPCPINFOEX;
 
Members
MaxCharSize
Specifies the maximum length, in bytes, of a character in the code page.
DefaultChar
Specifies the default character used when translating character strings into this code page. This character is used by the WideCharToMultiByte function if an explicit default character is not given.
LeadByte
Specifies a fixed-length array of lead-byte ranges, where the number of lead-byte ranges is variable. If there are no lead bytes in this code page, every element of the array is NULL. If there are lead bytes in this code page, the array contains a starting and ending value for each range. Ranges are inclusive. The maximum number of lead-byte ranges for any code page is 5. The array uses 2 bytes to describe each range, with a double-byte null terminator after the last range.
UnicodeDefaultChar
Specifies the Unicode default character used in translations from this code page. This character is used by the MultiByteToWideChar function.
CodePage
Specifies the code page value.
CodePageName
Specifies the full, localized name of the code page.
Remarks
Lead bytes are unique to double-byte character sets (DBCS). A lead byte is the first byte of a 2-byte character in a DBCS. Lead bytes occupy a specific range of byte values.
0
 
FelicitousAuthor Commented:
Hmmm,

thanks for info but most of this I already know. I am trying to get the code page name given its number. Therefore at point of calling I do not know anything else but the UINT CodePage.

I do not know the MAX_DEFAULTCHAR or MAX_LEADBYTES.

The detail is useful but does not help my cause. Can you help a little further and maybe give me a working example. I have tried just about everything to get this working and it just will not behave.

I am writing in Visual Basic 6 SP5 and I am not a great C++ programmer.

Regards,

Graham
0
 
FelicitousAuthor Commented:
Hmmm,

thanks for info but most of this I already know. I am trying to get the code page name given its number. Therefore at point of calling I do not know anything else but the UINT CodePage.

I do not know the MAX_DEFAULTCHAR or MAX_LEADBYTES.

The detail is useful but does not help my cause. Can you help a little further and maybe give me a working example. I have tried just about everything to get this working and it just will not behave.

I am writing in Visual Basic 6 SP5 and I am not a great C++ programmer.

Regards,

Graham
0
Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

 
Richie_SimonettiIT OperationsCommented:
Well, that is what i found

#define MAX_LEADBYTES             12          // 5 ranges, 2 bytes ea., 0 term.
#define MAX_DEFAULTCHAR           2           // single or double byte

If i understood that notation, they should become:
const MAX_LEADBYTES = 12
const MAX_DEFAULTCHAR = 2
0
 
hangmanCommented:
The structure you are using is wrong. In the DLL declaration, it needs a CPINFO structure which looks like

struct _cpinfo {
    UINT MaxCharSize;
    BYTE DefaultChar[MAX_DEFAULTCHAR];
    BYTE LeadByte[MAX_LEADBYTES];
} CPINFO;
 
not the CPINFOEX
0
 
FelicitousAuthor Commented:
Hi, (re Hangman Post)

I believe from the SDK there are two functions.

GetCPInfoEx which uses the Extended structure (CPINFOEX).

GetCPInfo which uses the standard structure (CPINFO).

I need the extended structure to get the code page name.

Regards,

Graham
0
 
FelicitousAuthor Commented:
Richie,

I will try your solution this evening and get back to you. Thanks for the further info.

The structure I will now try is as follows:

Public Type CPINFOEX
 MaxCharSize As Long
 DefaultChar(2) As Byte
 LeadByte(12) As Byte
 UnicodeDefaultChar As Byte
 CodePage As Integer
 CodePageName As String * 128
End Type

If you think it is wrong please post here.

Kind Regards,

Graham
0
 
FelicitousAuthor Commented:
Hmmm,

with the lead from hangman and a re-read of my code it looks like I may have coded the Declare incorrectly.

Public Declare Function GetCPInfoEx Lib "kernel32" Alias "GetCPInfoExA" (ByVal CodePage As Long, ByVal
Flags As Long, lpCPInfo As CPINFO) As Long

Prehaps the following would help:

Public Declare Function GetCPInfoEx Lib "kernel32" Alias "GetCPInfoExA" (ByVal CodePage As Long, ByVal
Flags As Long, lpCPInfo As CPINFOEX) As Long

I may have a combination of problems. (Apart from me :-) )

Regards,

Graham
0
 
FelicitousAuthor Commented:
Richie,

thanks for the info. The lengths did the trick.

Regards,

Graham
0
 
Richie_SimonettiIT OperationsCommented:
Uf, at least!
Sorry for delay, i was in problems with Internet connection an EE .
0

Featured Post

[Webinar] Kill tickets & tabs using PowerShell

Are you tired of cycling through the same browser tabs everyday to close the same repetitive tickets? In this webinar JumpCloud will show how you can leverage RESTful APIs to build your own PowerShell modules to kill tickets & tabs using the PowerShell command Invoke-RestMethod.

  • 6
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now