Solved

How to call Kernel32 GetCPInfoEx

Posted on 2001-06-23
10
829 Views
Last Modified: 2007-12-19
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
Comment
Question by:Felicitous
  • 6
  • 3
10 Comments
 
LVL 16

Expert Comment

by:Richie_Simonetti
ID: 6221995
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
 

Author Comment

by:Felicitous
ID: 6222048
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
 

Author Comment

by:Felicitous
ID: 6222068
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
 
LVL 16

Accepted Solution

by:
Richie_Simonetti earned 100 total points
ID: 6222242
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
 
LVL 2

Expert Comment

by:hangman
ID: 6223541
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
Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

 

Author Comment

by:Felicitous
ID: 6223813
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
 

Author Comment

by:Felicitous
ID: 6223817
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
 

Author Comment

by:Felicitous
ID: 6223825
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
 

Author Comment

by:Felicitous
ID: 6225735
Richie,

thanks for the info. The lengths did the trick.

Regards,

Graham
0
 
LVL 16

Expert Comment

by:Richie_Simonetti
ID: 6225773
Uf, at least!
Sorry for delay, i was in problems with Internet connection an EE .
0

Featured Post

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

The debugging module of the VB 6 IDE can be accessed by way of the Debug menu item. That menu item can normally be found in the IDE's main menu line as shown in this picture.   There is also a companion Debug Toolbar that looks like the followin…
Have you ever wanted to restrict the users input in a textbox to numbers, and while doing that make sure that they can't 'cheat' by pasting in non-numeric text? Of course you can do that with code you write yourself but it's tedious and error-prone …
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

744 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

13 Experts available now in Live!

Get 1:1 Help Now