Dynamically call Win32 function (API)

Source: Internet
Author: User

Dynamically call Win32 function (API)

In many cases, we need to call the platform dynamically.
In. NET platform, unlike Win32, functions cannot be called directly through function pointers, and DLL can only be called after the function signature is declared in advance.

The following is a solution for dynamic platform calling:
1. Platform calling through Win32 DLL
Disadvantage: cannot interact with. net.
See: None

2. Define the dynamicProgramSet to declare the function signature and reflect the call platform call
Disadvantage: low efficiency, excessive resource occupation, and the resources occupied by dynamic assembly cannot be released. If appdomain is used, dynamic assembly can be released, but more resources will be occupied.
See: None

3. Define a dynamic assembly to generate a global method, and call a function pointer using the msil command in the global method.
Disadvantages: In addition to acceptable efficiency, other disadvantages are the same as above.
See: http://blog.csdn.net/pansiom/archive/2006/01/01/568096.aspx

4. Use the getdelegateforfunctionpointer method of the system. runtime. interopservices. Marshal class to dynamically call the function pointer according to the delegate.
Disadvantage: The function pointer can be called only after the delegate is defined, and the effect of "dynamic" is lost.
See: http://www.cnblogs.com/rick/archive/2006/07/13/449849.aspx

5. Use the msil command in dynamicmethod to call the function pointer
Disadvantages: Almost none
See: content of this article

Here, we use dynamicmethod to call a dynamic platform. Its main advantages include the following:
1. Lightweight: it occupies a small amount of resources. resources can be released if dynamicmethod is not used.
2. Efficiency: Why is ironpython more efficient than cpython? This is because dynamicmethod and JIT are used.
3. Flexibility: You can call almost any defined Win32 function dynamically, regardless of the call conventions or the parameter structure.

Description of dynamicmethod in msdn:
You can use the dynamicmethod class to generate and execute methods at runtime, instead of generating dynamic programs and dynamic types to include this method. The dynamic method is to generate and execute a fewCode.

Instance code:

Using System;
Using System. Collections. Generic;
Using System. text;
Using Zealic. windows;

Namespace Leleapplication1
{
Class Program
{
Static   Void Main ( String [] ARGs)
{
// Test 1
Dynamiclibrary hilib =   New Dynamiclibrary ( " Hi. dll " );
Nativemethodbuilder hibuilder =   New Nativemethodbuilder ();
Nativemethod Method = Hibuilder. makemethod (hilib, " Func " );
Console. writeline ( " Close the pop-up dialog box 'hille' " );
Method. Invoke ();
Hilib. Free ();

// Test 2
Dynamiclibrary krnlib =   New Dynamiclibrary ( " Kernel32.dll " );
Nativemethodbuilder beepbuilder =   New Nativemethodbuilder ();
Beepbuilder. parameterlength =   2 ;
Beepbuilder. setparameterinfo ( 0 , Typeof ( Int ));
Beepbuilder. setparameterinfo ( 1 , Typeof ( Int ));
Method = Beepbuilder. makemethod (krnlib, " BEEP " );
Console. writeline ( " Listen, your machine is screaming! " );
Method. Invoke ( 1000 , 1000 );
Console. writeline ( " Press any key to exit! " );
Console. readkey ( True );
}
}
}

So much code is the most practical:
Click to download the code and sample

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.