I’m not used to P/Invoke but I should declare a couple of WinAPI functions for getting or setting the keyboard layout. I declared the functions like:
[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(
StringBuilder pwszKLID //[out] string that receives the name of the locale identifier
);
But when I compile this (in a C# WPF application) I get the warnings:
CA1901
Microsoft.Portability
As it is declared in your code, the return type of P/Invoke
will be 4 bytes wide on 64-bit platforms.
This is not correct, as the actual native declaration of
this API indicates it should be 8 bytes wide on 64-bit platforms.
Consult the MSDN Platform SDK documentation for help determining
what data type should be used instead of ‘long’.
and (I suppose this is of less concern as keyboard layout names are just digits):
CA2101
Microsoft.Globalization
To reduce security risk, marshal parameter ‘pwszKLID’ as Unicode, by setting DllImport.CharSet to CharSet.Unicode, or by explicitly marshaling the parameter as UnmanagedType.LPWStr. If you need to marshal this string as ANSI or system-dependent, specify MarshalAs explicitly, and set BestFitMapping=false; for added security, also set ThrowOnUnmappableChar=true.
I tried using IntPtr for the first warning, but this does not solve the issue. Could anyone help by pointing me to the correct form for these declarations?
Thanks!
You can try using the following declarations:
The
CharSetspecification will clear up CA2101. Adjusting the returns of both methods to the correct return types and adding theMarshalAsfor the return on theGetKeyboardLayoutNamewill clear up the CA1901.