Solved

How to call Kernel32 GetCPInfoEx

Posted on 2001-06-23
10
856 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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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
 

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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction In a recent article (http://www.experts-exchange.com/A_7811-A-Better-Concatenate-Function.html) for the Excel community, I showed an improved version of the Excel Concatenate() function.  While writing that article I realized that no o…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
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…

680 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