user32.dll api and keyboard layout

D

Dan

Sorry if this post may sound a bit off-topic, but I could not find a
specialized newsgroup. I have created a dotnet 2.0 library containing some
user controls and written in C#. One of the controls (an extension of
RichTextControl) needs to programmatically change the keyboard layout, so it
imports some user32.dll API functions (see below). This control works fine
in Windows XP, but it crashes under Win64 systems when trying to call the
user32 API. How can I change the code so that it runs under Win64 or where
could I find more information about porting API-dependent dotnet code under
this OS?

Thank you in advance!

Code snippets:

// API DECLARATIONS
private const uint KLF_ACTIVATE = 1; // activate the layout
private const int KL_NAMELENGTH = 9; // length of the keyboard buffer

[DllImport("user32.dll")]
private static extern long LoadKeyboardLayout(
string pwszKLID, // input locale identifier
uint Flags // input locale identifier options
);
[DllImport("user32.dll")]
private static extern long GetKeyboardLayoutName(
System.Text.StringBuilder pwszKLID //[out] string that receives the name
of the locale identifier
);

sample call:

LoadKeyboardLayout("00000409", KLF_ACTIVATE);
 
N

Nicholas Paldino [.NET/C# MVP]

Dan,

What is the crash that you get? The declarations that you have given
don't look like they should give you a problem.
 
W

Willy Denoyette [MVP]

The retuns from the API's are not longs! One is an IntPtr (a Handle or void*
in Win32) the other a bool.
IntPtr's are plaform dependent values!

Check the documentation for the API's.

Willy.

| Sorry if this post may sound a bit off-topic, but I could not find a
| specialized newsgroup. I have created a dotnet 2.0 library containing some
| user controls and written in C#. One of the controls (an extension of
| RichTextControl) needs to programmatically change the keyboard layout, so
it
| imports some user32.dll API functions (see below). This control works fine
| in Windows XP, but it crashes under Win64 systems when trying to call the
| user32 API. How can I change the code so that it runs under Win64 or where
| could I find more information about porting API-dependent dotnet code
under
| this OS?
|
| Thank you in advance!
|
| Code snippets:
|
| // API DECLARATIONS
| private const uint KLF_ACTIVATE = 1; // activate the layout
| private const int KL_NAMELENGTH = 9; // length of the keyboard buffer
|
| [DllImport("user32.dll")]
| private static extern long LoadKeyboardLayout(
| string pwszKLID, // input locale identifier
| uint Flags // input locale identifier options
| );
| [DllImport("user32.dll")]
| private static extern long GetKeyboardLayoutName(
| System.Text.StringBuilder pwszKLID //[out] string that receives the
name
| of the locale identifier
| );
|
| sample call:
|
| LoadKeyboardLayout("00000409", KLF_ACTIVATE);
|
|
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top