Last post Sep 15, 2008 01:50 PM by rednael
Jun 07, 2007 07:05 PM|Steve Carson|LINK
I have a DLL created from C++ code that I want to call from an APS.NET AJAX Enabled Web Application. When I try to add a reference to this dll I get a message:
"A reference to 'D:\Documents and Settings\......fred.dll' could not be added. No type libraries were found in the component. "
I have written a C# application that calls the dll with no problems. It contains a class
that contains a statement of the form:
for each function in the dll, plus I just put the dll in the same folder as the executable.
But this same strategy is not working with my asp.net web application.
Jun 07, 2007 11:18 PM|Mikhail Arkhipov (MSFT)|LINK
The DLL must be accessible to IIS / ASP.NET process since that's what executes the page. Another issue is that C# client app runs under your user credentials and hence have access to your Documents & Settings while ASP.NET runs in a Web serveras a network
service. It does not have access to your user folders.
Jun 08, 2007 03:57 AM|Steve Carson|LINK
Thanks for the advice, but it is very general. Can you tell me what I should do specifically to solve my problem? That is, how do I make the DLL accessible to IIS / ASP.NET process?
I have gone into INetPub where the .aspx executable is and placed a copy of the dll there. That does not solve the problem.
Jun 08, 2007 05:08 PM|Mikhail Arkhipov (MSFT)|LINK
One can write a long article on the subject [:)]. Maybe I should do it one day.
There are multiple ways you can use native code. One is that you use and it relies on default OS behavior for loading DLLs. LoadLibrary loads from where main EXE is (not where calling DLL is) and from the PATH. Second is to make DLL a COM object and register
it. This way DLL can be anywhere and OLE activation will find it by the path list in OS registry. Third is to call LoadLibrary/GetProcAddress manually and specify whatever path you want.
Aspx is not an executable since it is not EXE file. Pages are compiled into assemblies which are dlls not exes either. If you are relying on default OS DLL loading, dll should be where EXE file is. in the ASP.NET case EXE is either IIS executable if ASP.NET
is running without process isolation or wp_aspnet.exe if ASP.NET is configured for process isolation. Or you can add path to dll to the system (not user) PATH variable.
As for data access, it depends what is acceptable for you. You can grant access you your user folder or you may decide to store data elsewhere, in a location accessible to the Web site running process (perhaps in the Web site folder itself or in a database).
Jun 09, 2007 11:14 PM|Steve Carson|LINK
Thanks. Based on your advice "Or you can add path to dll to the system (not user) PATH variable ", I searched and located an article "Search Path Used by Windows to Locate a DLL":
From this I saw that the "current directory" was in the search path. Since I already have special directories for the input and output files used in the optimization problem that this dll helps solve, I just added another directory in that same location
to hold the ancillary dlls and set it to be the current directory before the first dll code is called. This gave me a very quick and easy solution!
Thanks also for clarifying my sloppy language. You are right that the aspx is a dll, not an exe.
Nov 28, 2007 09:37 PM|WTangoFoxtrot|LINK
i had this problem earlier with a DLL written in C# so i placed it in the websites wwwroot\website\bin folder and it worked. now i need to use a DLL written in C++ and even though i placed the dll where the page can find them the problem persists.
any suggestions ?
Nov 28, 2007 09:44 PM|Mikhail Arkhipov (MSFT)|LINK
You cannot directly use C++ dll in bin. You need to wrap it into managed code. There are several ways of doing so, depending on how functions/methods are exported from the native dll. Is it a COM object? Or is a a set of C-style API methods exported via
Nov 28, 2007 09:51 PM|WTangoFoxtrot|LINK
thanks for the quick reply. the dll is basically a class with some external functions used by the class, nothing special.
Nov 28, 2007 10:37 PM|Mikhail Arkhipov (MSFT)|LINK
So do you want to use methods from the C++ class in the dll?
Nov 28, 2007 10:49 PM|WTangoFoxtrot|LINK
yup. the dll is basically a c++ and a few external functions used by the class.
just wondering do i need to make a .lib file indicating the availble methods in the dll ? afterall it does say that it cant find any type libraries in the component when i try to reference to the dll.
Nov 28, 2007 10:57 PM|Mikhail Arkhipov (MSFT)|LINK
This is the hardest case. There is no easy way to call C++ code from C# or VB.NET. CLR manages managed/native transitions automatically only when component is a COM object with proper type library. It cannot call C++ methods directly. You have to make C++
class a COM object, register it properly and then you'll be able to call it. Alternative way is to make C-style wrapper, export functions as C-style API (like Win32) then write a wrapper in C# which will translate calls from C# to C++ via DllImport definitions.
Nov 28, 2007 11:03 PM|WTangoFoxtrot|LINK
hmm....i think it may be easier to just rewrite the dll again in C#. thanks for your help anyways.
Sep 15, 2008 01:50 PM|rednael|LINK
Please read the following article:
It's an in-depth article about how to use a native DLL (or C++ DLL) in your managed .Net code. The article shows which types are interoperable, how to import a DLL, how to pass strings, how to pass structures and how to de-reference pointers.
And C# source code examples are included.
marshal dll import extern native-code managed-code .net