How to import user32.dll and others into C#?

Asked

Viewed 197 times

2

I found several and various results on the internet teaching to import a specific function from user32.dll.

But I can’t get out of my head if someone hasn’t done a project with all the imports organized feats.

So it would matter that this project and the IDE would help find all the other constants and functions.

Or will Microsoft itself not do it?

The pinvoke.net project doesn’t have it ready. I would have to copy and paste each one and some have documentation comments others don’t.

  • 1

    Good would be if there was a tool like Dllwrapperbuilder, you play the dll and it gives you a file. Cs with the Imports.

2 answers

3

It doesn’t exist and it doesn’t make much sense to have since only you know what needs to matter. Besides this it would be difficult to keep something up to date and that met the correct version.

And if you want to import everything and you won’t use it, you’re doing something wrong. The fact that there are several things does not mean that everything should be available for use. The very use of importing unmanaged content should be done sparingly. Following this will not take much work.

Imagine something like the P/Invoke that uses this intensely did not, it is because it should not do. But it is the source that will help you.

If you want to know everything there is you should look in the documentation.

  • Adding a library to the repository is not importing everything. In addition we are talking about P/Invoke or are only headers the DLL will be used added anyway. What would be good is due to the constants. As the documentation is really a good all use, we can take advantage of and disable the auto-complete of Visual Studio and program in VI. each project open documentation and copy and paste constants and headers.

2


When importing a dll Windows has a resolution process of Assembly, it checks in some places like Windows System, Windows System32, Windows Syswow64, in the global cache, or in the application directory. I can’t tell you the exact algorithm or the precedence of each directory.

For sure you will find libraries (Wrappers) of the type NativeMethods through the internet, it is common to do this.

Behold:

https://referencesource.microsoft.com/#Presentationframework/src/Framework/System/windows/Standard/Nativemethods.Cs

The library of this example is mixed.

And another detail, the app only loads the dlls you use, to see this test:

[DllImport("fake.dll")]
static extern void FuncaoFake();

If you do not call the function the application shows no error.

But call FuncaoFake() it shows the following error:

System.Dllnotfoundexception: 'Unable to load the DLL 'fake.dll': Could not find the specified module. (Exception of HRESULT: 0x8007007E)'

Remembering that the abusive use of platform invoke It’s a bit of a departure from the philosophy of managed code. Native function calls need some extra instructions to be invoked, Microsoft recommends few calls passing enough data instead of many calls passing little data per call, sorry I did not find the documentation link on this.

If your application depends intensely on this type of call it is more recommended to write in C++.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.