⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 frmmain.cs

📁 CSharp 动态加载Dll动态链接库,看看CSharp是怎样完成的.
💻 CS
字号:
/*
 * Created by SharpDevelop.
 * User: HeadCQ
 * Date: 2008-3-28
 * Time: 15:03
 * 
 * To change this template use Tools | Options | Coding | Edit Standard Headers.
 */

using System;
using System.Collections.Generic;
using System.Drawing;
using System.Windows.Forms;
using System.Runtime.InteropServices;

namespace Test
{
	/// <summary>
	/// Description of MainForm.
	/// </summary>
	public partial class frmMain : Form
	{
		public delegate int MsgBox(int hWnd,string msg,string cpp,int ok);

		[DllImport("kernel32.dll", EntryPoint="GetProcAddress")]
		public static extern int GetProcAddress (
			int hModule,
			string lpProcName);

	    [DllImport("kernel32.dll", EntryPoint="LoadLibrary")]
		public static extern int LoadLibrary (
			string lpLibFileName);

	    [DllImport("kernel32.dll", EntryPoint="FreeLibrary")]
		public static extern int FreeLibrary (
			int hLibModule);
		//**************************************
		public frmMain()
		{
			//
			// The InitializeComponent() call is required for Windows Forms designer support.
			//
			InitializeComponent();
			
			//
			// TODO: Add constructor code after the InitializeComponent() call.
			//
		}
		
		void BtnTestClick(object sender, EventArgs e)
		{
			int hUser32 = 0;
            hUser32 = LoadLibrary("user32.dll");         
            MsgBox mymsg = (MsgBox)GetAddress(hUser32, "MessageBoxA", typeof(MsgBox));
            mymsg(this.Handle.ToInt32(), "TestMsg", "Title" , 64);
            FreeLibrary(hUser32);
		}
		
		static private Delegate GetAddress(int DllModule, string FunctionName, Type t)
        {
            int addr = GetProcAddress(DllModule, FunctionName);
            if (addr == 0) 
            {
            	return null; 
            }
            else
            {
            	return Marshal.GetDelegateForFunctionPointer(new IntPtr(addr), t);
            }
        }
	}
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -