asp.net C#調用託管DLL和非託管DLL檔案的區別

來源:互聯網
上載者:User

asp教程.net c#調用託管dll和非託管dll檔案的區別
託管dll檔案,可以在dotnet環境通過 “添加引用” 的方式,直接把託管dll檔案添加到項目中。然後通過 using  dll命名空間,來調用相應的dll對象 。

    非託管dll檔案,在dotnet環境應用時,通過 dllimport 調用。

   c# 調用非託管dll檔案。dll檔案是用c語言編寫的。
如下:
1:結構定義
  rditag_t  
  rditag_t結構定義了測點的結構
  typedef struct _rditag_t
  {
char tagname[rdi_max_tagname_len]; /*[in]*/
float val; /*[out]*/
int status; /*[out]*/
  } rditag_t;
  2:rditagname_t
  rditagname_t定義了測點名類型
  typedef char rditagname_t[rdi_max_tagname_len];
----------------------------------------------------
函數說明:
  1:void rdi_read(rditag_t tags[], int count, int timeout);
  參數
  tags 讀取資料點數組。
  count 輸入參數。
  timeout 輸入參數。
  2: void rdi_read0(rditagname_t tagnames[], float values[], int statuses[], int count, int timeout);
參數
tagnames 輸入參數。
values 輸出參數。
statuses 輸出參數。
count 輸入參數。
timeout 逾時值(毫秒)。
傳回值
無。

    --以下是源碼。及供參考學習。不能用於商業盈利。 
public const string dll_name = "rdiapi.dll";
  public const byte rdi_max_tagname_len = 0x18;
  public const int rdi_sts_failed = -1;
  public const int rdi_sts_invalid_handle = -2;
  public const int rdi_sts_invalid_tag = -5;
  public const int rdi_sts_net_error = -3;
  public const byte rdi_sts_ok = 1;
  public const int rdi_sts_timeout = -4;

  [dllimport("rdiapi.dll")]
  public static extern int rdi_close();
  [dllimport("rdiapi.dll")]
  public static extern int rdi_init();
  [dllimport("rdiapi.dll")]
  public static extern int rdi_isopen();
  [dllimport("rdiapi.dll")]
  public static extern int rdi_open(string server, int port);
  [dllimport("rdiapi.dll")]
  --用c#編寫的函數(注意參數[in, out] rditag_t[] tags 而不是 ref )
  public static extern int rdi_read([in, out] rditag_t[] tags, int count, int timeout);
  [dllimport("rdiapi.dll")]
  public static extern int rdi_read0(rditag_t1[] tags, [in, out] float[] value_array, [in, out] int[] status_array, int count, int timeout);
  [dllimport("rdiapi.dll")]
  public static extern int rdi_uninit();
  -- 用c#定義結構
  [structlayout(layoutkind.sequential)]
  public struct rditag_t
  {
  [marshalas(unmanagedtype.byvaltstr, sizeconst = 0x18)]
  public string tagname;
  public float val;
  public int status;
  }
  -- 用c#定義結構 
  [structlayout(layoutkind.sequential)]
  public struct rditag_t1
  {
  [marshalas(unmanagedtype.byvaltstr, sizeconst = 0x18)]
  public string tagname;

 

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.