1

In the following code,

using System.Runtime.InteropServices;
[DllImport("user32.dll", CharSet = CharSet.Auto, CallingConvention = CallingConvention.StdCall)]
public static extern void mouse_event(uint dwFlags, uint dx, uint dy, uint cButtons, uint dwExtraInfo);

I googled the "mouse_event" function of user32.dll, and got this. Is importing user32.dll the equivalent of referencing the Win32 API, making said documentation a valid resource for info of other functions and their parameters? And why is it that a "user32.dll" would in "c# bear the function names of what is consistently referred to elsewhere as "WINAPI" or "WIN32"?

joseph taylor
  • 123
  • 12
  • 1
    user32.dll is one of the many libraries that collectively are called "WinAPI". Your second link points to the `__stdcall` modifier which declares the calling convention WinAPI happens to use, not represents what it is. – GSerg Feb 16 '18 at 08:57
  • User32.dll are the droids you are looking for – TheGeneral Feb 16 '18 at 09:04
  • 2
    It is part of it, the winapi is very large and implemented in hundreds of DLLs. Many of them obscure, user32 is not since it implements the legacy desktop UI support functions. Fwiw, mouse_event() was deprecated 25 years ago, you are supposed to use SendInput(). It has a flaw, it cannot report an error. Once Microsoft publishes a winapi function they can never remove it again. – Hans Passant Feb 16 '18 at 09:06
  • So does __stdcall automatically know which WinAPI dll contains the function you are referencing? Why is it in c# I must explicitly define which dll contains the method? Is there not an equivalent to __stdcall in c#? – joseph taylor Feb 16 '18 at 20:22

1 Answers1

3

What Microsoft calls the "Win32 API" is the interface to the windows operating system. Any interaction with the OS (creating windows, opening files, accessing network etc.) needs to go trough any of these well documented function calls. Many C# functions are behind the scenes actually wrapper implementations for these operating system functions. Some of the more specific methods do not have C# equivalents though, and you can use code as the one you presented (using the [DllImport] attribute) to access them anyway.

The Win32 API for legacy reasons primarily consists of three files: kernel32.dll (file io/synchronisations) gdi32.dll (user interface) and user32.dll (more user interface). These three libraries include almost all the functions that make up that API. Other libraries where added over time and are used for specific purposes.

(Note: The dll's themselves don't do much any more. In current implementations, they're just wrappers around other libraries, but from an application point of view, that doesn't matter)

PMF
  • 14,535
  • 3
  • 23
  • 49
  • 2
    The second paragraph oversimplifies things. `kernel32.dll`, `user32.dll` and `gdi32.dll` contain oldest and the most essential parts of WinAPI, but there are dozens and dozens of other libraries implementing more parts of WinAPI. – user7860670 Feb 16 '18 at 09:15
  • Depends on what you call "essential". I have almost never needed to use any other kernel dll directly, as long as it was ordinary user code (except maybe DirectX). – PMF Feb 16 '18 at 09:18
  • 1
    `shlwapi` is very common, as is `ws2_32` (Shell, networking). – MSalters Feb 16 '18 at 10:05