How to know if native method is safe / unsafe?
Asked Answered
S

3

15

I implement this function : GetSystemPowerStatusEx & GetSystemPowerStatusEx2

according to this article on MSDN, I should create a class named according to the functions I will use, but my question is : How can I know in which class I should put GetSystemPowerStatusEx & GetSystemPowerStatusEx2 ?

I'm lost...

Thanks for help.

[EDIT] My question is : which of these three class names are the good one for me (NativeMethods / SafeNativeMethods / UnsafeNativeMethods) ?

These methods should be in one of the following classes:

NativeMethods - This class does not suppress stack walks for unmanaged code permission. (System.Security.SuppressUnmanagedCodeSecurityAttribute must not be applied to this class.) This class is for methods that can be used anywhere because a stack walk will be performed.

SafeNativeMethods - This class suppresses stack walks for unmanaged code permission. (System.Security.SuppressUnmanagedCodeSecurityAttribute is applied to this class.) This class is for methods that are safe for anyone to call. Callers of these methods are not required to perform a full security review to make sure that the usage is secure because the methods are harmless for any caller.

UnsafeNativeMethods - This class suppresses stack walks for unmanaged code permission. (System.Security.SuppressUnmanagedCodeSecurityAttribute is applied to this class.) This class is for methods that are potentially dangerous. Any caller of these methods must perform a full security review to make sure that the usage is secure because no stack walk will be performed.

Statued answered 22/12, 2010 at 16:41 Comment(0)
V
10

It's a pretty silly warning and ultimately unproductive. But keeping it happy is simple, just add a static class to your project named NativeMethods and put the [DllImport] declarations inside it. No need for separate classes. Declare them internal.

Beware that you cannot call these functions on an emulator, testing them is going to require running it on the device itself. To keep your program debuggable in the emulator be sure to wrap the code that calls them with #ifdef DEBUG.

Vinita answered 22/12, 2010 at 17:7 Comment(2)
It's okay, program runs but please see my Edit.Statued
Use NativeMethods. Until you find a really good reason to apply those attributes. When Microsoft hires you for example.Vinita
H
2

Just declare them as static methods in a static class; that is the standard approach.

Edit: Like the commenter pointed out, they can be placed in a non-static class as well. Essentially, as long as your Win32 methods are static extern with a DLLImport attribute, they can go pretty much in any class.

Homeric answered 22/12, 2010 at 16:43 Comment(4)
They can be put in a non-static class, too.Aureole
Sure, they can be put anywhere really. The standard approach I have seen is static in static but I guess the real answer is, it doesn't matter.Homeric
I agree, I always write a wrapper-function for them, too. So that I can easily replace or modify them, if necessary.Monolingual
It's okay, program runs but please see my Edit.Statued
A
0

Put them inside the class you are going to use them as a static methods, then wrap around them with normal methods so you won't bother the rest of the application with the notion that there is something extern involved.

I use that approach and it never fails.

EDIT:

Check this out:

http://pinvoke.net/search.aspx?search=GetSystemPowerStatusEx

Attaint answered 22/12, 2010 at 16:56 Comment(1)
It's okay, program runs but please see my Edit.Statued

© 2022 - 2024 — McMap. All rights reserved.