0

If I use the System.Windows.Forms.MessageBox class' show function to draw a message box, a window pops up. I would like to know where the actual code to draw this window is? Is it in some Win32 API or user32.dll etc.? Through reflector I am unable to see the definition of Show method.

Also, is .NET a wrapper around win32 api then? I know that when we compile .net code it generates IL which is converted to CPU specific instructions at run time and then our CPU executes those instructions. But I have a feeling that there is some common code in Windows OS that's responsible for drawing a window (or reading from text file etc.). All these languages like C++, F#, C#, Java etc. eventually call that code from within the framework. But the question how can I go about finding it and verify it?

For example: in MFC or Win32 whichever function we called to draw a window on the screen, MessageBox.Show also calls the same function but its abstracted from us?

Then on the other hand we can inter operate between two languages.

So I am little confused here. Also I am reading CLR via C# these days so my brain is in super excited state :-)

Dante May Code
  • 11,177
  • 9
  • 49
  • 81
Varun Sharma
  • 2,591
  • 8
  • 45
  • 63
  • 1
    Why does it matter where it is implemented? (It most likely just calls the `MessageBox` API) – Billy ONeal Dec 05 '12 at 00:01
  • While interesting, this doesn't seem on topic to me. You're asking how Microsoft implements functionality, not how to code anything. – David Dec 05 '12 at 00:02
  • 1
    Look up and buy Charles Petzold's WinAPI Bible! The answers are in there Neo! – t0mm13b Dec 05 '12 at 00:09
  • 1
    I think the question is legitimate: you need to know the layers of the system and how they relate to each other if you want to do anything even moderately complex. – rodrigo Dec 05 '12 at 00:16

1 Answers1

4

I think that you are on the right track. No matter the language or the framework, at the very bottom is the Operating System, so everything ends there, eventually: system calls.

For example, to open a file in Windows the system call is CreateFile(), no matter if you used C fopen, C++ ifstream, C# System.IO.* or Java java.io.*.

About your questions of System.Windows.Forms, they use the usual Win32 windows, so the windows are identified by HWND values, represented in the CLR as IntPtr.

And about the function to show the window, your question is a bit misleading, as there are several functions involved, and several layers in the Win32 API.

For example to create a window, you call CreateWindowEx(), to show it if hidden call ShowWindow(), and the paint itself will be handled when the window procedure (a callback you have to write) receives the WM_PAINT message. But in order to receive messages you have to write a message pump (GetMessagte() / DispatchMessage()).

But to create a simple MessageBox() there is a function already available that does all that for you: MessageBox(), for which the System.Windows.Forms.MessageBox class is a direct wrapper.

So now, what was the question? Ah, yes, can we interoperate between System.Windows.Forms and Win32? Yes!

rodrigo
  • 94,151
  • 12
  • 143
  • 190
  • 1
    Thanks for the reply Rodrigo. Just as you gave an example of CreateFile(), then this applies to almost all .net classes/functions (excluding System.Math class and others where you can get things done at the top layer itself) as well. Eventually at some point they go down to the C++ or C code and call stuff there. Basically my goal is to debug all the way down if possible (using windbg/ntsd etc.). I have setup .net symbols from http://www.microsoft.com/en-nz/download/details.aspx?id=4917) as well so that I can go inside the .net code. I am trying to not be an average developer !!! – Varun Sharma Dec 05 '12 at 00:24
  • @VVV how did you end up going with this btw? – David Klempfner Jun 03 '16 at 08:29
  • 1
    @Backwards_Dave - You can use windows debugger to see the call stack all the way up to kernel for creating the file. I haven't used windbg for this exact scenario though but since I used it for other issues, I know that you can use it for this as well. – Varun Sharma Jun 07 '16 at 21:09