使用Win32汇编开发一个dll并在C#中调用
生活随笔
收集整理的這篇文章主要介紹了
使用Win32汇编开发一个dll并在C#中调用
小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
使用RadASM,新建一個(gè)Dll Project;
下一步,默認(rèn);
dll包含Def文件;
完成工程構(gòu)建;都默認(rèn);
完成以后項(xiàng)目結(jié)構(gòu)如下;
asm代碼;
.386 .model flat, stdcall option casemap :none include windows.inc .data?dwCounter dd ?.codeDllEntry proc _hInstance,_dwReason,_dwReserved ;動(dòng)態(tài)鏈接庫(kù)的入口函數(shù)mov eax,TRUEret DllEntry Endp_CheckCounter proc ;功能函數(shù)1,確保數(shù)字的大小在0--10之間mov eax,dwCountercmp eax,0jge @F ;大于等于則跳轉(zhuǎn)xor eax,eax@@:cmp eax,10jle @F mov eax,10@@:mov dwCounter,eaxret _CheckCounter endp_IncCounter proc ;功能函數(shù)2,實(shí)現(xiàn)將當(dāng)前的數(shù)字加1inc dwCountercall _CheckCounterret _IncCounter endp_DecCounter proc ;功能函數(shù)3,將當(dāng)前的數(shù)字減1dec dwCountercall _CheckCounterret _DecCounter endp_Mod proc uses ecx edx _dwNumber1,_dwNumber2 ;功能函數(shù)4,求輸入的兩個(gè)數(shù)的模xor edx,edxmov eax,_dwNumber1mov ecx,_dwNumber2.if ecxdiv ecxmov eax,edx.endifret _Mod endpEnd DllEntry ;最后DLL的結(jié)尾def文件;dll導(dǎo)出3個(gè)函數(shù)供外部調(diào)用,_Mod這個(gè)是求2個(gè)數(shù)的模;
在編程語(yǔ)言中,% 是取模運(yùn)算符,或求余運(yùn)算放,指的是求數(shù)除法的余數(shù);比如,8%3 運(yùn)算的值是2,計(jì)算的是第一個(gè)操作數(shù)除以第二個(gè)操作數(shù)的余數(shù);
EXPORTS _IncCounter_DecCounter_Mod?從菜單編譯,連接,構(gòu)建;構(gòu)建成功;
看下dll文件,lib文件這些出來(lái)了;
用C#做一個(gè)程序,嘗試按調(diào)用C++ dll的方法進(jìn)行調(diào)用;結(jié)果如下,可以調(diào)用;
?
C#代碼如下;使用SharpDevelop工具;
using System; using System.Collections.Generic; using System.Drawing; using System.Windows.Forms; using System.Runtime.InteropServices;namespace dlldemo1 {/// <summary>/// Description of MainForm./// </summary>public partial class MainForm : Form{[DllImport("asmdlldemo.dll")]static extern int _Mod(int n1, int n2);public MainForm(){//// The InitializeComponent() call is required for Windows Forms designer support.//InitializeComponent();//// TODO: Add constructor code after the InitializeComponent() call.//}void Button1Click(object sender, EventArgs e){int ret=_Mod(Int32.Parse(textBox1.Text), Int32.Parse(textBox2.Text));textBox3.Text=ret.ToString();}} }dll放到exe目錄;如下;
?
《新程序員》:云原生和全面數(shù)字化實(shí)踐50位技術(shù)專家共同創(chuàng)作,文字、視頻、音頻交互閱讀總結(jié)
以上是生活随笔為你收集整理的使用Win32汇编开发一个dll并在C#中调用的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: GIS叠加分析功能学习
- 下一篇: C#访问Access和Win7 64位下