Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How are Win32 types represented in C# P/Invoke?

I am trying to call the PrintDlgW Win32 API from C# via P/Invoke. With the help of P/Invoke Interop Assistant 1.0 from Microsoft, I am able to declare necessary data structures and import functions from DLL. But how to use the HDC or HWND in C#? Thanks in advance.

like image 438
smwikipedia Avatar asked Feb 17 '10 13:02

smwikipedia


People also ask

Which function in Win32 API is equivalent to the main () function of C or C++?

WinMain() is the C entry point function of any windows application. Like normal DOS/console based application which has main() function as C entry point, in windows we have WinMain() instead.

What data type is HWND?

HWND data types are "Handles to a Window", and are used to keep track of the various objects that appear on the screen. To communicate with a particular window, you need to have a copy of the window's handle. HWND variables are usually prefixed with the letters "hwnd", just so the programmer knows they are important.

What does hInstance mean?

hInstance is something called a "handle to an instance" or "handle to a module." The operating system uses this value to identify the executable (EXE) when it is loaded in memory. The instance handle is needed for certain Windows functions—for example, to load icons or bitmaps. hPrevInstance has no meaning.

What is WinAPI in C?

WinAPI (also known as Win32; officially called the Microsoft Windows API) is an application programming interface written in C by Microsoft to allow access to Windows features. The main components of the WinAPI are: WinBase: The kernel functions, CreateFile, CreateProcess, etc.


3 Answers

HWND in C++ is really a void *, which is represented in C# by the IntPtr type.

like image 101
Otávio Décio Avatar answered Oct 05 '22 15:10

Otávio Décio


This site has nice examples with structures already defined for quite a few APIs. The structure is here.

like image 31
Mark Wilkins Avatar answered Oct 05 '22 14:10

Mark Wilkins


It might make it easier to do this

using HWND = System.IntPtr;

Then use HWND in your pInvoke...to make it easier to read and keep it "the same" as the pinvoke signature.

Hope this helps, Best regards, Tom.

like image 35
t0mm13b Avatar answered Oct 05 '22 14:10

t0mm13b