2007年12月11日

ASP.Net 連接到 C++ dll library



從 Draft 清出來的,一年前的筆記,都忘了做什麼的了  


一、由於工作需要,實作一個 .Net Library Dll for ASPX,而試著去 link 原來就有的 C++ dll library (比如為 mylib.dll, 檔頭為 mylib.h),會發生下述的錯誤訊息:


C4935: assembly access specifier modified from 'public' c:\Program Files\Microsoft Visual Studio .NET 2003\Vc7\PlatformSDK\Include\ServProv.h(93) : error C2872: 'IServiceProvider' : ambiguous symbol could be 'c:\Program Files\Microsoft Visual Studio .NET 2003\Vc7\PlatformSDK\Include\ServProv.h(48) : System::IServiceProvider IServiceProvider' or 'Stdafx.cpp(0) : System::IServiceProvider' c:\Program Files\Microsoft Visual Studio .NET 2003\Vc7\PlatformSDK\Include\ServProv.h(100) : fatal error C1903: unable to recover from previous error(s); stopping compilation AssemblyInfo.cpp




應該是在 #include "mylib.h" 前加上 #include "windows.h" 即可。若如果還會有類似下述的 error,


LINK : error LNK2020: unresolved token (0A000066) ??_7type_info@@6B@ LINK : error LNK2020: unresolved token (0A000067) _CrtDbgReport LINK : error LNK2020: unresolved token (0A000069) _CxxThrowException LINK : error LNK2020: unresolved token (0A000071) memset LINK : error LNK2020: unresolved token (0A000072) atexit LINK : error LNK2020: unresolved token (0A000077) free LINK : fatal error LNK1120: 6 unresolved externals




在 "additional dependence" 加上 msvcrtd.lib (debug) 或 msvcrt.lib 即可。但仍會有下述的 warning. 解決方法在此


msvcrtd.lib(secchk.obj) : warning LNK4210: .CRT section exists; there may be unhandled static initializers or terminators atlsd.lib(Externs.obj) : warning LNK4210: .CRT section exists; there may be unhandled static initializers or terminators atlsd.lib(atltrace.obj) : warning LNK4210: .CRT section exists; there may be unhandled static initializers or terminators atlsd.lib(atlbase.obj) : warning LNK4210: .CRT section exists; there may be unhandled static initializers or terminators




二、 String 和 wchar_t 各轉的問題,會有類似 cannot convert parameter 3 from 'System::String __gc *' to 'LPCWSTR' 的訊息出現,用下面的程式將之轉成 wchar_t *。


__wchar_t *StringToTCHAR(String * value) { // Convert from 'System::String __gc *' to '__wchar_t *'. int length = value->Length; __wchar_t *ret = new __wchar_t[length + 1]; ZeroMemory(ret, sizeof(__wchar_t) * (length + 1)); for (int j = 0; j < length; j++) ret[j] = value->Chars[j];// Return the '__wchar_t *' value. return ret; }











三、aspx project 在 Reference Library dll 後, 會出現「應用程式中發生伺服器錯誤」的訊息時,如下:


剖析器錯誤訊息: 找不到檔案或組件名稱 'MyLib' 或其相依性的其中之一。 原始程式錯誤: 行 196: <add assembly="System.EnterpriseServices, Version=1.0.5000.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a"/> 行 197: <add assembly="System.Web.Mobile, Version=1.0.5000.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a"/> 行 198: <add assembly="*"/> 行 199: </assemblies> 行 200: </compilation>




根據這裡的說法, aspx 會去 bin/ 目錄預讀 assemblies 的檔案,而 MyLib.dll 會被放到這個目錄下,而造成這個問題,處理方案有二:


1. 將你的 dll 放到 PATH 變數下。






2. 將下述這段加到 web.config






<configuration> <system.web> <compilation defaultLanguage="c#" debug="true"> <assemblies> <remove assembly="*"/> </assemblies> </compilation> </system.web>




四、然後可能還會有下述問題,


剖析器錯誤訊息: 無法載入型別 'WebApplication1.Global'。


原始程式錯誤:
行 1:  <%@ Application Codebehind="Global.asax.cs" Inherits="WebApplication1.Global" %>


根據這裡的說法,加上 xsrc=xxx



<%@ Application Codebehind="Global.asax.cs" SRC="Global.asax.cs"  


Inherits="WebApplication1.Global" %>




五、CS0234: 型別或命名空間 'ComponentModel' 不存在於類別或命名空間 'System' (您是否遺漏組件參考?)

六、終於將我之前的 C++ dll library 連接到 aspx 上了,還弄不太清楚 ASPX,之前只有弄過一些 ASP 而已 :p




Orignal From: ASP.Net 連接到 C++ dll library