學(xué)習(xí)C# DllImport相關(guān)知識
MSDN中對DllImport Attribute的解釋是這樣的:可將該屬性應(yīng)用于方法。DllImport Attribute 屬性提供對從非托管 DLL 導(dǎo)出的函數(shù)進(jìn)行調(diào)用所必需的信息。作為***要求,必須提供包含入口點(diǎn)的 DLL 的名稱。
并給了一個示例:
- [DllImport("KERNEL32.DLL", EntryPoint="MoveFileW", SetLastError=true,
- CharSet=CharSet.Unicode, ExactSpelling=true,
- CallingConvention=CallingConvention.StdCall)]
- public static extern bool MoveFile(String src, String dst);
上網(wǎng)搜了一下,最常見的就是使用它來調(diào)用WIN32的API,例如上面所示?;蛘哒{(diào)用一下C或C++編寫的DLL。
這東西沒怎么用過。只是前幾天忽然分配下一個臨時的任務(wù),做一個“停車廠管理”的小東西,聽說是一個大干部的小孩子要弄這么個東西,那干部是公司的客戶,討論正經(jīng)事之余又拜托了我們做這么個小東西。其中用到了單片機(jī)模擬車輛出入的一些信號。
我對單片機(jī)一竅不通,好在有人寫好了輪詢單片機(jī)的DLL,我只管調(diào)用,由于是C++寫的,于是將DLL拷貝到BIN目錄后(這DLLImport會從程序啟動目錄開始查找相應(yīng)名稱的DLL,未找到則轉(zhuǎn)至system32下查找),用DllImport來調(diào)用,但在這個過程中遇到了幾個問題:
1.看了一下C++的代碼,需要用到的只有三個方法:
- bool OpenSerialPort1()
- bool fnGetIO(unsigned char& P1, unsigned char& P2, unsigned char& P3)
- bool CloseSerialPort1()
于是就在自己的程序中寫了:
- using System.Runtime.InteropServices;
- ……
- [DllImport("GetIODll.dll", EntryPoint = "OpenSerialPort1")]
- public static extern bool OpenSerialPort1();
- [DllImport("GetIODll.dll", EntryPoint = "fnGetIO")]
- public static extern bool fnGetIO(ref byte P1, ref byte P2, ref byte P3);
- [DllImport("GetIODll.dll", EntryPoint = "CloseSerialPort1")]
- public static extern bool CloseSerialPort1();
然而程序在運(yùn)行時無論如何總是提示“找不到入口點(diǎn)”,搞得懵了,只好上網(wǎng)搜去,***下了一個叫eXeScope的小軟件,裝完之后查看該DLL,果然如貼子中寫的,DLL中的函數(shù)名稱發(fā)生了變化,分別成了:
將這些怪怪的名稱代入到EntryPoin中,編譯運(yùn)行,沒有問題了。
2.可是接上單片機(jī)之后,問題又來了,雖然OpenSerialPort1返回的結(jié)果是true,但fnGetIO讀出一數(shù)據(jù)全是0,按理應(yīng)該是全1才對。來了一個同事,說反正有源碼,把原來的DLL弄成標(biāo)準(zhǔn)C的試試,標(biāo)準(zhǔn)C不標(biāo)準(zhǔn)C的我也沒明白,讓那人給改了一下,把編譯之后的DLL拷到自己程序的BIN下,將EntryPoin換成正常的函數(shù)名,運(yùn)行,這回是真的OK了。
讀寫.ini文件時,也會用到C# DllImport,不過現(xiàn)在.ini文件好像用得少了,下面是讀寫的程序:
- {
- publicstring path;
- [DllImport("kernel32")]
- privatestaticexternlong WritePrivateProfileString(string section,string key,string val,string filePath);
- [DllImport("kernel32")]
- privatestaticexternint GetPrivateProfileString(string section,string key,string def,StringBuilder
- retVal,int size,string filePath);
- public IniFile(string INIPath)
- {
- path = INIPath;
- }
- publicvoid IniWriteValue(string Section,string Key,string Value)
- {
- WritePrivateProfileString(Section,Key,Value,this.path);
- }
- publicstring IniReadValue(string Section,string Key)
- {
- StringBuilder temp = new StringBuilder(255);
- int i = GetPrivateProfileString(Section,Key,"",temp,255,this.path);
- return temp.ToString();
- }
- }
網(wǎng)上關(guān)于C# DllImport的很多問題是由于所調(diào)方法的參數(shù)比較復(fù)雜,現(xiàn)在我還沒用到,看到一篇貼子,參數(shù)中帶指針的,也先錄下來,以備將來查用:
參數(shù)是用指針來獲取一個數(shù)組:Int GetData(byte * pBuffer)
pBuffer是數(shù)組的首地址,也就是說GetData會寫pBuffer[0],pBuffer[1]....pBuffer[100];
答曰:
- [DllImport("yourDllFile.dll"]
- Private static extern int GetValue([MarshalAs(UnmanagedType.LPArray)]byte[] pValue);
如果是out參數(shù),可以如下
- [DllImport("yourDllFile.dll")]
- Private static extern int GetValue([Out,MarshalAs(UnmanagedType.LPArray)]byte[] pValue);
C# DllImport的相關(guān)內(nèi)容就介紹到這里。
【編輯推薦】