自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

學(xué)習(xí)C# DllImport相關(guān)知識

開發(fā) 后端
本文通過一些詳細(xì)的例子介紹了C# DLLImport的相關(guān)內(nèi)容,希望對大家有所幫助。

MSDN中對DllImport Attribute的解釋是這樣的:可將該屬性應(yīng)用于方法。DllImport Attribute 屬性提供對從非托管 DLL 導(dǎo)出的函數(shù)進(jìn)行調(diào)用所必需的信息。作為***要求,必須提供包含入口點(diǎn)的 DLL 的名稱。

并給了一個示例:

  1. [DllImport("KERNEL32.DLL", EntryPoint="MoveFileW", SetLastError=true,  
  2.  
  3. CharSet=CharSet.Unicode, ExactSpelling=true,  
  4.  
  5. CallingConvention=CallingConvention.StdCall)]  
  6.  
  7. 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++的代碼,需要用到的只有三個方法:

  1. bool OpenSerialPort1()  
  2.  
  3. bool fnGetIO(unsigned char& P1, unsigned char& P2, unsigned char& P3)  
  4.  
  5. bool CloseSerialPort1() 

于是就在自己的程序中寫了:

  1. using System.Runtime.InteropServices;  
  2.  
  3. ……  
  4.  
  5. [DllImport("GetIODll.dll", EntryPoint = "OpenSerialPort1")]  
  6.  
  7. public static extern bool OpenSerialPort1();  
  8.  
  9. [DllImport("GetIODll.dll", EntryPoint = "fnGetIO")]  
  10.  
  11. public static extern bool fnGetIO(ref byte P1, ref byte P2, ref byte P3);  
  12.  
  13. [DllImport("GetIODll.dll", EntryPoint = "CloseSerialPort1")]  
  14.  
  15. public static extern bool CloseSerialPort1(); 

然而程序在運(yùn)行時無論如何總是提示“找不到入口點(diǎn)”,搞得懵了,只好上網(wǎng)搜去,***下了一個叫eXeScope的小軟件,裝完之后查看該DLL,果然如貼子中寫的,DLL中的函數(shù)名稱發(fā)生了變化,分別成了:

?OpenSerialPort1@@YA_NXZ

?fnGetIO@@YA_NAAE00@Z

?CloseSerialPort1@@YA_NXZ

將這些怪怪的名稱代入到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文件好像用得少了,下面是讀寫的程序:

  1. {  
  2.  
  3. publicstring path;  
  4.  
  5. [DllImport("kernel32")]  
  6.  
  7. privatestaticexternlong WritePrivateProfileString(string section,string key,string val,string filePath);  
  8.  
  9. [DllImport("kernel32")]  
  10.  
  11. privatestaticexternint GetPrivateProfileString(string section,string key,string def,StringBuilder  
  12.  
  13. retVal,int size,string filePath);  
  14.  
  15. public IniFile(string INIPath)  
  16.  
  17. {  
  18.  
  19.        path = INIPath;  
  20.  
  21. }  
  22.  
  23. publicvoid IniWriteValue(string Section,string Key,string Value)  
  24.  
  25. {  
  26.  
  27.        WritePrivateProfileString(Section,Key,Value,this.path);  
  28.  
  29. }  
  30.  
  31. publicstring IniReadValue(string Section,string Key)  
  32.  
  33. {  
  34.  
  35.        StringBuilder temp = new StringBuilder(255);  
  36.  
  37.        int i = GetPrivateProfileString(Section,Key,"",temp,255,this.path);  
  38.  
  39.        return temp.ToString();  
  40.  
  41. }  
  42.  

網(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];

答曰:

  1. [DllImport("yourDllFile.dll"]    
  2.  
  3. Private static extern int GetValue([MarshalAs(UnmanagedType.LPArray)]byte[] pValue); 

如果是out參數(shù),可以如下 

  1. [DllImport("yourDllFile.dll")]  
  2.  
  3. Private static extern int GetValue([Out,MarshalAs(UnmanagedType.LPArray)]byte[] pValue); 

 C# DllImport的相關(guān)內(nèi)容就介紹到這里。

【編輯推薦】

  1. 學(xué)習(xí)C#消息:循序漸進(jìn)
  2. 解惑答疑:C#委托和事件
  3. 學(xué)習(xí)C#實(shí)現(xiàn)HTTP協(xié)議:多線程文件傳輸
  4. 進(jìn)一步接觸C#委托與事件
  5. 淺析四種C#轉(zhuǎn)換的區(qū)別
責(zé)任編輯:book05 來源: hi.baidu
相關(guān)推薦

2009-08-21 08:41:44

C#反射

2009-09-01 16:14:08

C# Socket類

2009-08-10 14:03:08

C# COM接口

2009-08-07 13:30:20

C# Excel導(dǎo)入

2009-08-13 14:59:00

C#數(shù)據(jù)訪問層

2009-06-12 09:22:44

VB.NET類型C#

2009-09-01 15:25:01

C#位域

2010-01-12 17:54:42

VB.NET Dlli

2009-08-24 17:07:09

C# 泛型

2009-07-30 18:20:21

C#繼承

2009-08-05 18:39:54

C#異常類

2009-08-27 16:11:03

C# delegateC# event

2010-03-24 10:54:50

企業(yè)私有云

2009-08-13 12:50:45

C#基礎(chǔ)知識

2009-08-13 16:13:03

C#基礎(chǔ)知識

2009-08-27 16:37:06

C#基礎(chǔ)知識

2009-08-24 11:02:52

C#接口映射

2009-08-24 09:55:26

C#接口轉(zhuǎn)換

2009-09-02 14:06:14

C#文件傳送

2009-08-12 14:13:51

C#讀寫Excel文件
點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號