[ASP.NET]載入非託管資源(unmanaged resource) ex:C/C++.dll檔

  • 988
  • 0
  • 2014-09-03

摘要:[ASP.NET]載入非託管資源(unmanaged resource) ex:非CRL .dll檔(C/C++.dll)

前言

當ASP.net 需要用到 非託管資源(如:C/C++ dll)時,就必須在Application_Start事件加入該 非託管資源dll 路徑,才能使相關CRL(.NET)程式得以呼叫(call function)與使用,否則就會'發生找不該資源'狀況。

 

第一步

將 非託管資源dll 檔 放置所屬網站專案 Bin資料夾 裡面。

 

第二步

將網站專案加入Global.asax檔
(Global.asax檔介紹:http://msdn.microsoft.com/zh-tw/library/aa720712(v=vs.71).aspx)
對 網站專案 按右鍵=>加入=>加入新項目=>Global.asax檔(如下圖所示)。

 

第三步

Global.asax檔中 Application_Start事件加入下面程式碼即可。

 


    void Application_Start(object sender, EventArgs e) 
    {
        //取得 bin資料夾 實體路徑 效果同Server.MapPath("~/bin");
        string unmanagedPath = System.AppDomain.CurrentDomain.RelativeSearchPath;
        
        //將 指定相關資源路徑(unmanagedPath 變數) 加入網站的 運行環境中
        System.Environment.SetEnvironmentVariable("PATH", unmanagedPath, EnvironmentVariableTarget.Process);
    }

PS補充:上面的 unmanagedPath變數 值也可換成下面的寫法


//取得處理序當下的可用 系統目錄路徑及其他路徑 並加入 bin資料夾路徑
//string unmanagedPath = String.Concat(System.Environment.GetEnvironmentVariable("PATH"), ";", System.AppDomain.CurrentDomain.RelativeSearchPath);

 

 

※在此感謝所有的幫助者,感謝~