C#調(diào)用dll導(dǎo)出函數(shù)
作者:佚名 
  本文介紹C#調(diào)用dll導(dǎo)出函數(shù),在C++中我們能夠通過 LoadLibrary,GetProcAddress 來動態(tài)C#調(diào)用dll導(dǎo)出函數(shù)。在C#中也能夠用這樣的方式嗎?
 在 C++ 中我們能夠通過 LoadLibrary, GetProcAddress 來動態(tài)C#調(diào)用dll導(dǎo)出函數(shù)。在 C# 中也能夠用這樣的方式嗎?
在 DotNet 2.0 里面這樣是可以的, 這完全得益于 2.0新增的一個函數(shù)Marshal.GetDelegateForFunctionPointer 方法此方法在 .NET Framework 2.0 版中是新增的。將非托管函數(shù)指針轉(zhuǎn)換為委托。
C#調(diào)用dll導(dǎo)出函數(shù)實例代碼如下:
- publicdelegateintMsgBox(inthwnd,stringmsg,stringcpp,intok);
 - [DllImport("Kernel32")]
 - publicstaticexternintGetProcAddress(inthandle,Stringfuncname);
 - [DllImport("Kernel32")]
 - publicstaticexternintLoadLibrary(Stringfuncname);
 - [DllImport("Kernel32")]
 - publicstaticexternintFreeLibrary(inthandle);
 - privatestaticDelegateGetAddress(intdllModule,stringfunctionname,Typet)
 - {
 - intaddr=GetProcAddress(dllModule,functionname);
 - if(addr==0)
 - returnnull;
 - else
 - returnMarshal.GetDelegateForFunctionPointer(newIntPtr(addr),t);
 - }
 - privatevoidbutton1_Click(objectsender,EventArgse)
 - {
 - inthuser32=0;
 - huser32=LoadLibrary("user32.dll");
 - MsgBoxmymsg=(MsgBox)GetAddress(huser32,"MessageBoxA",typeof(MsgBox));
 - mymsg(this.Handle.ToInt32(),txtmsg.Text,txttitle.Text,64);
 - FreeLibrary(huser32);
 - }
 

以上介紹C#調(diào)用dll導(dǎo)出函數(shù)
【編輯推薦】
責(zé)任編輯:佚名 
                    來源:
                    MSDN
 














 
 
 
 
 
 
 