C # Implementation dynamically invokes Windows DLL

Source: Internet
Author: User
Tags emit

Call Method:

Object obj = Windllinvoke ("Kernel32.dll", "Beep", new object[] {750, n, typeof (void));

Function code:

1 [System.Runtime.InteropServices.DllImport ("kernel32")]
2 private static extern IntPtr LoadLibrary (string lplibfilename);
3
4 [System.Runtime.InteropServices.DllImport ("kernel32")]
5 private static extern IntPtr GetProcAddress (IntPtr hmodule, string lpprocname);
6
7 [System.Runtime.InteropServices.DllImport ("kernel32")]
8 private static extern IntPtr FreeLibrary (IntPtr hlibmodule);
9
10///
11///Dynamic Call to Windows DLL
12///
///DLL filename
14///function name to be called
15///Function parameters
16///return value
17///Call Result
Private static Object Windllinvoke (String fileName, String funname, object[] objparams, Type returntype)
19 {
IntPtr libhandle = IntPtr.Zero;
21st
Try
23 {
24//Get function address
Libhandle = LoadLibrary (FileName);
if (Libhandle = = IntPtr.Zero) return null;
IntPtr procaddres = GetProcAddress (Libhandle, funname);
if (procaddres = = IntPtr.Zero) return null;
29
30//Get parameter type
type[] Paramtypes = new Type[objparams.length];
for (int i = 0; i < objparams.length; ++i)
33 {
Paramtypes[i] = objparams[i]. GetType ();
35}
36
37//Build Call Method model
AssemblyName asembyname = new AssemblyName ();
Asembyname.name = "windllinvoke_assembly";
AssemblyBuilder Asembybuilder = AppDomain.CurrentDomain.DefineDynamicAssembly (Asembyname, Assemblybuild Eraccess.run);
ModuleBuilder ModuleBuilder = Asembybuilder.definedynamicmodule ("Windllinvoke");
MethodBuilder MethodBuilder = Modulebuilder.defineglobalmethod ("Invokefun", Methodattributes.public | M Ethodattributes.static, ReturnType, paramtypes);
43
44//Get a ilgenerator for sending the required IL
ILGenerator IL = Methodbuilder.getilgenerator ();
for (int j = 0; j < paramtypes.length; ++j)
47 {
48//Press the parameters into the stack
if (Paramtypes[j]. Isvaluetype)
50 {
Wu IL. Emit (Opcodes.ldarg, J); by Value
52}
Or else
54 {
IL. Emit (Opcodes.ldarga, J); by addrsss
56}
57}
58
59//Determine processor type
if (intptr.size = 4)
61 {
The IL. Emit (OPCODES.LDC_I4, Procaddres.toint32 ());
63}
$ else if (Intptr.size = 8)
65 {
IL. Emit (Opcodes.ldc_i8, Procaddres.toint64 ());
67}
Or else
69 {
throw new PlatformNotSupportedException ("Sorry, I don't know you!") ");
71}
72
IL. Emitcalli (Opcodes.calli, Callingconvention.stdcall, ReturnType, paramtypes);
The IL. Emit (Opcodes.ret); return value
Modulebuilder.createglobalfunctions ();
76
77//Get method information
MethodInfo MethodInfo = Modulebuilder.getmethod ("Invokefun");
79
Methodinfo.invoke (null, objparams);//Call method and return its value
81}
The catch is {return null;}
and finally
84 {
if (libhandle!= IntPtr.Zero) FreeLibrary (Libhandle); Releasing resources
86}
87}

Related Article

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.