To write a CS file in Visual Studio, you need to use dllimport to call an unmanaged Win32 API, such
1 Using System;
2 Using System. runtime. interopservices;
3
4 Class Example
5 {
6 // Use dllimport to import the Win32 MessageBox function.
7 [Dllimport ( " User32.dll " , Charset = Charset. Auto)]
8 Public Static Extern Int MessageBox (intptr hwnd, string text, string caption, Uint Type );
9
10 Static Void Main ()
11 {
12 // Call the MessageBox function using platform invoke.
13 MessageBox ( New Intptr ( 0 ), " Hello world! " , " Hello Dialog " , 0 );
14 }
15 }
16
I often have the following problems:
Dllimport andPublic StaticThe most important part of the two rows should be manually typed in, such as the function name MessageBox and the parameter list (intptr hwnd, string text, string caption, uint type ), why cannot powerful Visual Studio be automatically completed?
I think if Visual Studio is more user-friendly, it should be like this:
In *. right-click the CS file and choose "win api call" or "dllimport" from the shortcut menu. The DLL file selection window is displayed (commonly used Win32 dll can be listed here, so that you do not need to go to system32 ), select the DLL file and click OK. The DLL function list is displayed. Each function name has a checkbox before it. Select checkbox. OK.
[Dllimport ( " User32.dll " , Charset = Charset. Auto)]
Public Static Extern Int MessageBox (intptr hwnd, string text, string caption, Uint Type );
Insert it to the head of the CS file, and automatically convert the function parameter list to the CSHARP type and fill it in completely. How cool. Why is Ms not done? It should not be unexpected. You can useDumpbin/exports user32.dllOr Link/dump/exports user32.dll Command.
What worries me most is the parameter list and parameter type.If a third-party vendor threw you a crypt. DLL without documentation, the boss asked you to use C # To Call crypt. DL to implement some features of the product, even if I usedDumpbinI have obtained the function list, but I don't know the parameters. Isn't it possible? Also, if some function parameters are not the basic data type of C #, isn't it possible? Let's talk about it.
I am not a newbie, please kindly advise me.