關於C#中的DLLImport

來源:互聯網
上載者:User

MSDN中對DllImportAttribute的解釋是這樣的:可將該屬性應用於方法。DllImportAttribute 屬性提供對從非託管 DLL 匯出的函數進行調用所必需的資訊。作為最低要求,必須提供包含進入點的 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);

上網搜了一下,最常見的就是使用它來調用WIN32的API,例如上面所示。或者調用一下C或C++編寫的DLL。

這東西沒怎麼用過。只是前幾天忽然分配下一個臨時的任務,做一個“停車廠管理”的小東西,聽說是一個大幹部的小孩子要弄這麼個東西,那幹部是公司的客戶,討論正經事之餘又拜託了我們做這麼個小東西。其中用到了單片機類比車輛出入的一些訊號。

我對單片機一竅不通,好在有人寫好了輪詢單片機的DLL,我只管調用,由於是C++寫的,於是將DLL拷貝到BIN目錄後(這DLLImport會從程式啟動目錄開始尋找相應名稱的DLL,未找到則轉至system32下尋找),用DllImport來調用,但在這個過程中遇到了幾個問題:

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();

然而程式在運行時無論如何總是提示“找不到進入點”,搞得懵了,只好上網搜去,最後下了一個叫eXeScope的小軟體,裝完之後查看該DLL,果然如貼子中寫的,DLL中的函數名稱發生了變化,分別成了:

?OpenSerialPort1@@YA_NXZ

?fnGetIO@@YA_NAAE00@Z

?CloseSerialPort1@@YA_NXZ

將這些怪怪的名稱代入到EntryPoin中,編譯運行,沒有問題了。

2.可是接上單片機之後,問題又來了,雖然OpenSerialPort1返回的結果是true,但fnGetIO讀出一資料全是0,按理應該是全1才對。來了一個同事,說反正有源碼,把原來的DLL弄成標準C的試試,標準C不標準C的我也沒明白,讓那人給改了一下,把編譯之後的DLL拷到自己程式的BIN下,將EntryPoin換成正常的函數名,運行,這回是真的OK了。

讀寫.ini檔案時,也會用到DllImport,不過現在.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();

}

}

網上關於DllImport的很多問題是由於所調方法的參數比較複雜,現在我還沒用到,看到一篇貼子,參數中帶指標的,也先錄下來,以備將來查用:

參數是用指標來擷取一個數組:Int GetData(byte * pBuffer)  

pBuffer是數組的首地址,也就是說GetData會寫pBuffer[0],pBuffer[1]....pBuffer[100];

答曰:

[DllImport("yourDllFile.dll"]  

Private static extern int GetValue([MarshalAs(UnmanagedType.LPArray)]byte[] pValue);

如果是out參數,可以如下  

[DllImport("yourDllFile.dll")]  

Private static extern int GetValue([Out,MarshalAs(UnmanagedType.LPArray)]byte[] pValue);

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.