Using NativeMethods/SafeNativeMethods/UnsafeNativeMethods

Asked 2 years ago, Updated 2 years ago, 78 views

Visual Studio's code analysis tool is warning against old P/Invoke code created by Euler.

CA1060 'MyClass.WriteFile(...)' is a P/Invoke method and must be defined in the class named NativeMethods, SafeNativeMethods, or UnsafeNativeMethods

The meaning of the warning and how to deal with it is understandable as it is written on the CA1060 description page, but has no specific policy on how to use these three people.

Q1. What specific safety does Safe/Unsafe here mean?
Q2. How do I use it differently (as Microsoft has submitted this specification)?
Q3. Are there any differences in performance and user interface?

visual-studio .net

2022-09-30 17:40

2 Answers

Uncle-Kei explained that the purpose was to use different class names in NativeMethods, SafeNativeMethods, and UnsafeNativeMethods, and to apply code access security according to the class name.

However, since code access security has been deprecated (disabled by default) from .NET4, distinguishing class names may not make sense now.


2022-09-30 17:40

As the text will be longer, I will use the answer field to express my personal opinion.
I'm afraid it's not an answer.I thought it would be a good opportunity for me to get an answer, or to have a discussion, etc.
Also, as I am not a .NET expert, I would like to remind you that there may be some mistakes.

First, we assume that security is meant to protect your environment and its users.

In the past, OSes were designed to rely on the permissions of logged-in users.
This is a reasonable idea if multiple users use the same hardware.

"The ""application permissions"" are generally examined in current mobile operating systems."
In addition, the application requests general access to the application during installation and authenticates it by the user.
Access to critical resources is checked from time to time and users may be asked to confirm.
In addition, users can deprive them of their privileges.

You can imagine that the .NET Framework takes into account the latter security design.
I could not find an official document that directly mentioned this content, but

https://docs.microsoft.com/en-us/dotnet/framework/misc/code-access-security?redirectedfrom=MSDN
According to

(1) The .NET Framework incorporates a mechanism called Code Access Security (CAS).
(2)The above checks permission to access specific resources.
(3)An inspection is determined by comparing the called functional resource with the authority of the calling source.
(4)An inspection is performed for all of a series of call sequences of functions in the called DLL (assembly).
(5)This is called "stack walk".

Therefore, security in this case can be interpreted by examining whether the caller has access to a function or resource.

Now, on https://docs.microsoft.com/en-us/visualstudio/code-quality/ca1060?view=vs-2019&redirectedfrom=MSDN,

(A) NativeMethods... .NET "stack walk" to inspect, so it's safe (unauthorized or denied).
(B) SafeNativeMethods... .NET will not be inspected.However, the provider said it was safe.
(C) UnsafeNativeMethods... .NET is not inspected.The supplier said it was dangerous.

It is shown that(A)It can be interpreted that the CAS inspects the members of the .
From here on out, you can imagine that members (B) and (C) cannot be used or published without special permission.

So, how do you use it differently?
(A)is an exception to almost every function provided by the operating system, such as GetTickCount().
(B)is a reference to a function of the DLL provided by the customer that does not use (A).
(C)Not applicable to the above conditions.
I imagine thatIt's just my imagination.
Also, I don't know what usage penalties there are for these.

Lastly, I would like to refrain from answering Q1 to Q2.


2022-09-30 17:40

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.