使用Win32汇编开发一个dll并在C#中调用
生活随笔
收集整理的這篇文章主要介紹了
使用Win32汇编开发一个dll并在C#中调用
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
使用RadASM,新建一個Dll Project;
下一步,默認;
dll包含Def文件;
完成工程構建;都默認;
完成以后項目結構如下;
asm代碼;
.386 .model flat, stdcall option casemap :none include windows.inc .data?dwCounter dd ?.codeDllEntry proc _hInstance,_dwReason,_dwReserved ;動態鏈接庫的入口函數mov eax,TRUEret DllEntry Endp_CheckCounter proc ;功能函數1,確保數字的大小在0--10之間mov eax,dwCountercmp eax,0jge @F ;大于等于則跳轉xor eax,eax@@:cmp eax,10jle @F mov eax,10@@:mov dwCounter,eaxret _CheckCounter endp_IncCounter proc ;功能函數2,實現將當前的數字加1inc dwCountercall _CheckCounterret _IncCounter endp_DecCounter proc ;功能函數3,將當前的數字減1dec dwCountercall _CheckCounterret _DecCounter endp_Mod proc uses ecx edx _dwNumber1,_dwNumber2 ;功能函數4,求輸入的兩個數的模xor edx,edxmov eax,_dwNumber1mov ecx,_dwNumber2.if ecxdiv ecxmov eax,edx.endifret _Mod endpEnd DllEntry ;最后DLL的結尾def文件;dll導出3個函數供外部調用,_Mod這個是求2個數的模;
在編程語言中,% 是取模運算符,或求余運算放,指的是求數除法的余數;比如,8%3 運算的值是2,計算的是第一個操作數除以第二個操作數的余數;
EXPORTS _IncCounter_DecCounter_Mod?從菜單編譯,連接,構建;構建成功;
看下dll文件,lib文件這些出來了;
用C#做一個程序,嘗試按調用C++ dll的方法進行調用;結果如下,可以調用;
?
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目錄;如下;
?
《新程序員》:云原生和全面數字化實踐50位技術專家共同創作,文字、視頻、音頻交互閱讀總結
以上是生活随笔為你收集整理的使用Win32汇编开发一个dll并在C#中调用的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: GIS叠加分析功能学习
- 下一篇: Oracle 作业学习总结