Code Analysis CA1060 Fix
Asked Answered
T

3

6

I have the following code in my application:

[DllImport("user32.dll")]
private static extern int GetWindowLong(IntPtr hwnd, int index);

[DllImport("user32.dll")]
private static extern int SetWindowLong(IntPtr hwnd, int index, int newStyle);

[DllImport("user32.dll")]
private static extern bool SetWindowPos(IntPtr hwnd, IntPtr hwndInsertAfter,
               int x, int y, int width, int height, uint flags);

[DllImport("user32.dll")]
private static extern IntPtr SendMessage(IntPtr hwnd, uint msg,
               IntPtr wParam, IntPtr lParam);

I am getting the following warning from Code Analysis (FxCop):

CA1060 : Microsoft.Design : Because it is a P/Invoke method, 'IconHelper.GetWindowLong(IntPtr, int)' should be defined in a class named NativeMethods, SafeNativeMethods, or UnsafeNativeMethods.

Can someone tell me which class I should put them in? I don't know if it is Native, SafeNative, or UnsafeNative.

Trifurcate answered 20/10, 2011 at 14:25 Comment(2)
possible duplicate of How to know if native method is safe / unsafe?Abercrombie
FAQ: How do I fix a violation of MovePInvokesToNativeMethodsClass?Abercrombie
V
9

You have detailed information about this warning here: http://msdn.microsoft.com/en-us/library/ms182161.aspx. In short:

For most applications, moving P/Invokes to a new class that is named NativeMethods is enough.

Venola answered 20/10, 2011 at 14:30 Comment(0)
H
5

Try moving them all into a NativeMethod class, it will solve the problem

Your code should look like this after fixing it

public class NativeMethods {
[DllImport("user32.dll")]
private static extern int GetWindowLong(IntPtr hwnd, int index);

[DllImport("user32.dll")]
private static extern int SetWindowLong(IntPtr hwnd, int index, int newStyle);

[DllImport("user32.dll")]
private static extern bool SetWindowPos(IntPtr hwnd, IntPtr hwndInsertAfter,
               int x, int y, int width, int height, uint flags);

[DllImport("user32.dll")]
private static extern IntPtr SendMessage(IntPtr hwnd, uint msg,
               IntPtr wParam, IntPtr lParam);
}

Remember to change all the places where you are calling these methods

Before change

SendMessage(IntPtr hwnd, uint msg,IntPtr wParam, IntPtr lParam)

should be

NativeMethods.SendMessage(IntPtr hwnd, uint msg,IntPtr wParam, IntPtr lParam)
Heeheebiejeebies answered 25/6, 2015 at 6:53 Comment(3)
Shouldn't the methods be public?Lordling
In above code methods should be "internal". "Public" raise warning "CA1401 P/Invokes should not be visible"Monkeypot
Making it a static class would be smart also (you're just putting methods in that class, not holding any state in it). So your class declaration is like internal static class NativeMethods { ...Saprolite
M
0

You can suppress this warning by defining

<PropertyGroup> 
..... 
     <NoWarn>CA1060</NoWarn>
..... 
</PropertyGroup>

in the configuration file (.csproj file).

Menstruate answered 12/2, 2020 at 8:15 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.