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.
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.
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.
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.
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.
HWND in C++ is really a void *, which is represented in C# by the IntPtr type.
This site has nice examples with structures already defined for quite a few APIs. The structure is here.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With