3
votes

The VS code analyzer throws this warning:

CA2101 Specify marshaling for P/Invoke string arguments To reduce security risk, marshal parameter 'buffer' 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. Reg2Bat CenteredMSGBox.vb 20

Here:

<DllImport("user32.dll")> _
Shared Function GetClassName(hWnd As IntPtr, buffer As System.Text.StringBuilder, buflen As Integer) As Integer
End Function

I need to use the ANSI encoding but I don't understand what I need to do, so how I need to marshall this?

1
Why do you need to use the ANSI version?David Heffernan
@David Heffernan I were confussed I don't need ANSI at lastElektroStudios

1 Answers

11
votes

Here's the declaration from pinvoke.net.

[DllImport("user32.dll", SetLastError = true, CharSet = CharSet.Auto)]
static extern int GetClassName(
    IntPtr hWnd, 
    StringBuilder lpClassName,
    int nMaxCount
);

If you (for whatever reason) wanted to import the ASCII version then it would look like

[DllImport("user32.dll", SetLastError = true, CharSet = CharSet.Ansi)]
static extern int GetClassNameA(
    IntPtr hWnd, 
    StringBuilder lpClassName,
    int nMaxCount
);

Another alternative is to specify marshalling behaviour for individual parameters as in

[DllImport("user32.dll", SetLastError = true)]
static extern int GetClassNameA(
    IntPtr hWnd, 
    [MarshalAs(UnmanagedType.LPStr)] StringBuilder lpClassName,
    int nMaxCount
);