Oral examination preparation (iii) -- use dllimport to call unmanaged Functions

Source: Internet
Author: User

Previous ArticleArticleAfter analyzing several methods for making the oral examination system, JMF seems to have a poor reputation, and the client also needs to download the JMF framework, which has never been used by red5 or FMS, microsoft does not seem to provide small web pages like Java appletsProgramBecause the application of the fat client has been occupied by flash, Silverlight has just begun to be promoted. Therefore, I decided to use the C/S architecture.

The first problem to be solved is to call the non-Trust function in the hosting program.

Use dllimport to call Win32 API. dllimport in the namespace system. runtime. interopservices.

Dllimport indicates the declared function.

The usage of dllimport is as follows: [dllimport ("user32.dll", entrypoint = "MessageBox", charset = charset. Auto)]

Here, user32.dll is the name of the DLL to be called, and entrypoint indicates the call entry point, that is, which function in the DLL is called. Charset indicates the called character set. Character sets include ANSI and Unicode.

The functions described by dllimport must be modified using static and extern.

Example:

Declaration prototype:

 

Lipwrap
1 Using System;
2 Using System. Collections. Generic;
3 Using System. text;
4 Using System. runtime. interopservices;
5
6 Namespace Recordtest
7 {
8 Class Lipwrap
9 {
10 [Dllimport ( " User32.dll " , Entrypoint =   " MessageBox " , Charset = Charset. Auto)]
11 Public   Static   Extern   Int Msgbox ( Int Hwnd, string text, string caption, Uint Type );
12 }
13 }
14

 

Call a function

 

Code
1 Using System;
2 Using System. Collections. Generic;
3 Using System. componentmodel;
4 Using System. Data;
5 Using System. drawing;
6 Using System. text;
7 Using System. Windows. forms;
8 Using System. runtime. interopservices;
9
10 Namespace Recordtest
11 {
12 Public   Partial   Class Form1: Form
13 {
14 Public Form1 ()
15 {
16 Initializecomponent ();
17 }
18
19 Private   Void Button#click ( Object Sender, eventargs E)
20 {
21 Lipwrap. msgbox ( 0 , " Hello, world " , " Njj " , 0 );
22 }
23
24
25
26 }
27
28
29
30
31 }

 

After running the program, When you click the button in the window, a hello World pop-up window will appear.

Appendix: managedCodeThe data type of the unmanaged code is incompatible. You must replace the Data Type of the unmanaged code with the data type in the managed code.

The following table lists the Data Types of the managed code corresponding to the unmanaged code from msdn.

 

The following table lists the data types used in Win32 APIs (listed in wtypes. h) and C style functions. Many non-writable libraries contain functions that pass these data types as parameters and return values. The third column lists the corresponding. NET Framework built-in value types or classes used in managed code. In some cases, you can replace the types listed in this table with the same size.

Unmanaged type in wtypes. h Unmanaged C language type Managed class name Description

Handle

Void *

32-bit windows and 64-bit windows.

Byte

Unsigned char

8-digit

Short

Short

16-bit

Word

Unsigned short

16-bit

Int

Int

32-bit

Uint

Unsigned int

32-bit

Long

Long

32-bit

Bool

Long

32-bit

DWORD

Unsigned long

32-bit

Ulong

Unsigned long

32-bit

Char

Char

It is modified with ANSI.

Lpstr

Char *

Or

It is modified with ANSI.

Lpcstr

Const char *

Or

It is modified with ANSI.

Lpwstr

Wchar_t *

Or

Use Unicode.

Lpcwstr

Const wchar_t *

Or

Use Unicode.

Float

Float

32-bit

Double

Double

64-bit

 

The following table describes the commonly used DLL in Win32 APIs.

DLL Description

Gdi32.dll

Graphical device interface (GDI) functions used for device output, such as functions used for drawing and font management.

Kernel32.dll

Low-level operating system functions for memory management and resource processing.

User32.dll

Windows management functions for message processing, timers, menus, and communication.

 

The multimedia API provided by WIN32API is called MCI, which is included in winmm. dll in the System32 directory.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.