ASP.NET (c# code behind) will not load legacy C++ dll

  • Thread starter Andy Sutorius via DotNetMonster.com
  • Start date
A

Andy Sutorius via DotNetMonster.com

Using the code below the browser just sits and spins. The dll is located in
the root of the web app. System.Runtime.Interop is in the using statements. I
have tried this in ASP.NET 1.1 and 2.0 and I get the same result. For testing
purposes I have hooked it up to a windows app and it performs as expected. Do
you know why the dll won't load in asp.net?

namespace WebApplication2
{
public class _default : System.Web.UI.Page
{
[DllImport("Ecghcfa.dll")] public extern static int ECgHCFA(int
lEditNo, string sParams, string sServer, string sDatabase, string sUsername,
string sPassword);

private void Page_Load(object sender, System.EventArgs e)
{
// Put user code to initialize the page here

int i = ECgHCFA(1, "218275", "cltecdev5\audit", "ca101web",
"350336", "Ec2KWeb");
Response.Write(i);
}

}
}
 
G

Guest

Have you tried putting it in the /bin folder? That would be the equivalent of
the primary probing path in a Windows App (The same folder as the exe).
Peter
 
E

Edwin Knoppert

I had the same, and asked for a solution as well.
I had no other solution than to preload using the loadlibrary() API.
And on dispose(!) free the instance.
The next time ASP.NET might have a refer to the dll if a valid function was
called.
So the next time the class get's loaded use GeModuleHandle(...) stuff to
determine if the dll is already loaded.

Unf. the dll is not released overnight and replacing the dll is a problem if
you upgrade your app.
I recommend to use an addition folder like win32 instead of bin so copying
is easier later on.
(Select all, unselect the win32 folder.. paste)
 
A

Andy Sutorius via DotNetMonster.com

With the dll in the bin folder:

System.DllNotFoundException: Unable to load DLL 'Ecghcfa.dll': The specified
module could not be found. (Exception from HRESULT: 0x8007007E) at _Default.
ECgHCFA(Int32 lEditNo, String sParams, String sServer, String sDatabase,
String sUsername, String sPassword) at _Default.Button1_Click(Object sender,
EventArgs e) in c:\Inetpub\wwwroot\hcfadll\Default.aspx.cs:line 32

In the page load I put a File.exists statement with a path to the bin folder
and it sees the dll.

I just don't understand.

I have even tried reflection and I get File not found exception.

Anyother ideas?


Have you tried putting it in the /bin folder? That would be the equivalent of
the primary probing path in a Windows App (The same folder as the exe).
Peter
Using the code below the browser just sits and spins. The dll is located in
the root of the web app. System.Runtime.Interop is in the using statements. I
[quoted text clipped - 21 lines]
 
E

Edwin Knoppert

O well, here is my class, VB though:

Example:
pl = New DLL_Preload("dllname")
do something...
pl.dispose

Class:

Imports Microsoft.VisualBasic

Public Class DLL_Preload
Implements IDisposable

' Preloads an ordinary *win32* dll from the bin folder or found through
the Windows %path%.
' Once ASP.NET has called a function in this library, preloading is no
longer required.
' This is handled for you.
' It's for the best you unload this class by setting it to nothing when
done.

Declare Ansi Function GetModuleHandle Lib "KERNEL32.DLL" Alias
"GetModuleHandleA" (ByVal lpModuleName As String) As Int32
Declare Ansi Function LoadLibrary Lib "KERNEL32.DLL" Alias
"LoadLibraryA" (ByVal lpLibFileName As String) As Int32
Declare Ansi Function FreeLibrary Lib "KERNEL32.DLL" Alias "FreeLibrary"
(ByVal hLibModule As Int32) As Int32
Private __hLib As Int32
Private hLib As Int32

' sModuleName, library's module name, usually equal to dllfilename
WITHOUT extension and WITHOUT folder.
' sDLLShortFileName, the modulename can differ, if the module is not
loaded yet, this will optionally be used as dll filename.
' Provide filename without folder but add it's extension like: .dll
Public Sub New(ByVal sModuleName As String, Optional ByVal
sDLLShortFileName As String = "")

On Error Resume Next

If Len(sModuleName) = 0 Then Exit Sub

sDLLShortFileName = Trim(sDLLShortFileName)
If Len(sDLLShortFileName) = 0 Then sDLLShortFileName = sModuleName &
".dll"

'-------------------------------------------------------------------------------------
' Preloads library if not loaded yet, we try loading it from the
website folder.
'-------------------------------------------------------------------------------------
' Test for already loaded module.
hLib = GetModuleHandle(sModuleName)

' Module does not exist, force loading.
If hLib = 0 Then

' Load from this website folder.
hLib =
LoadLibrary(System.Web.HttpContext.Current.Server.MapPath("~\bin\" &
sDLLShortFileName))
Select Case hLib

' Failed, load from anywhere on the computer.
Case 0 To 32

hLib = LoadLibrary(sDLLShortFileName)
Select Case hLib
Case 0 To 32
hLib = 0
Case Else
End Select

End Select

__hLib = hLib

End If

End Sub

'--------------------------------------------------------------------------------
' IDisposable
'--------------------------------------------------------------------------------
Private disposed As Boolean = False

Private Overloads Sub Dispose(ByVal disposing As Boolean)
On Error Resume Next
If Not Me.disposed Then
If __hLib <> 0 Then FreeLibrary(__hLib)
__hLib = 0
End If
Me.disposed = True
End Sub

#Region " IDisposable Support "
' This code added by Visual Basic to correctly implement the disposable
pattern.
Public Overloads Sub Dispose() Implements IDisposable.Dispose
' Do not change this code. Put cleanup code in Dispose(ByVal
disposing As Boolean) above.
Dispose(True)
GC.SuppressFinalize(Me)
End Sub

Protected Overrides Sub Finalize()
' Do not change this code. Put cleanup code in Dispose(ByVal
disposing As Boolean) above.
Dispose(False)
MyBase.Finalize()
End Sub
#End Region
'--------------------------------------------------------------------------------

Public Function GethInstance() As Int32
Return hLib
End Function

Public Function IsLoaded() As Boolean
Return CBool(GethInstance())
End Function

End Class


Andy Sutorius via DotNetMonster.com said:
With the dll in the bin folder:

System.DllNotFoundException: Unable to load DLL 'Ecghcfa.dll': The
specified
module could not be found. (Exception from HRESULT: 0x8007007E) at
_Default.
ECgHCFA(Int32 lEditNo, String sParams, String sServer, String sDatabase,
String sUsername, String sPassword) at _Default.Button1_Click(Object
sender,
EventArgs e) in c:\Inetpub\wwwroot\hcfadll\Default.aspx.cs:line 32

In the page load I put a File.exists statement with a path to the bin
folder
and it sees the dll.

I just don't understand.

I have even tried reflection and I get File not found exception.

Anyother ideas?


Have you tried putting it in the /bin folder? That would be the equivalent
of
the primary probing path in a Windows App (The same folder as the exe).
Peter
Using the code below the browser just sits and spins. The dll is located
in
the root of the web app. System.Runtime.Interop is in the using
statements. I
[quoted text clipped - 21 lines]
 
A

Andy Sutorius via DotNetMonster.com

Edwin,

Thank you for your code. What language was this written in C++? What version?

Andy

Edwin said:
O well, here is my class, VB though:

Example:
pl = New DLL_Preload("dllname")
do something...
pl.dispose

Class:

Imports Microsoft.VisualBasic

Public Class DLL_Preload
Implements IDisposable

' Preloads an ordinary *win32* dll from the bin folder or found through
the Windows %path%.
' Once ASP.NET has called a function in this library, preloading is no
longer required.
' This is handled for you.
' It's for the best you unload this class by setting it to nothing when
done.

Declare Ansi Function GetModuleHandle Lib "KERNEL32.DLL" Alias
"GetModuleHandleA" (ByVal lpModuleName As String) As Int32
Declare Ansi Function LoadLibrary Lib "KERNEL32.DLL" Alias
"LoadLibraryA" (ByVal lpLibFileName As String) As Int32
Declare Ansi Function FreeLibrary Lib "KERNEL32.DLL" Alias "FreeLibrary"
(ByVal hLibModule As Int32) As Int32
Private __hLib As Int32
Private hLib As Int32

' sModuleName, library's module name, usually equal to dllfilename
WITHOUT extension and WITHOUT folder.
' sDLLShortFileName, the modulename can differ, if the module is not
loaded yet, this will optionally be used as dll filename.
' Provide filename without folder but add it's extension like: .dll
Public Sub New(ByVal sModuleName As String, Optional ByVal
sDLLShortFileName As String = "")

On Error Resume Next

If Len(sModuleName) = 0 Then Exit Sub

sDLLShortFileName = Trim(sDLLShortFileName)
If Len(sDLLShortFileName) = 0 Then sDLLShortFileName = sModuleName &
".dll"

'-------------------------------------------------------------------------------------
' Preloads library if not loaded yet, we try loading it from the
website folder.
'-------------------------------------------------------------------------------------
' Test for already loaded module.
hLib = GetModuleHandle(sModuleName)

' Module does not exist, force loading.
If hLib = 0 Then

' Load from this website folder.
hLib =
LoadLibrary(System.Web.HttpContext.Current.Server.MapPath("~\bin\" &
sDLLShortFileName))
Select Case hLib

' Failed, load from anywhere on the computer.
Case 0 To 32

hLib = LoadLibrary(sDLLShortFileName)
Select Case hLib
Case 0 To 32
hLib = 0
Case Else
End Select

End Select

__hLib = hLib

End If

End Sub

'--------------------------------------------------------------------------------
' IDisposable
'--------------------------------------------------------------------------------
Private disposed As Boolean = False

Private Overloads Sub Dispose(ByVal disposing As Boolean)
On Error Resume Next
If Not Me.disposed Then
If __hLib <> 0 Then FreeLibrary(__hLib)
__hLib = 0
End If
Me.disposed = True
End Sub

#Region " IDisposable Support "
' This code added by Visual Basic to correctly implement the disposable
pattern.
Public Overloads Sub Dispose() Implements IDisposable.Dispose
' Do not change this code. Put cleanup code in Dispose(ByVal
disposing As Boolean) above.
Dispose(True)
GC.SuppressFinalize(Me)
End Sub

Protected Overrides Sub Finalize()
' Do not change this code. Put cleanup code in Dispose(ByVal
disposing As Boolean) above.
Dispose(False)
MyBase.Finalize()
End Sub
#End Region
'--------------------------------------------------------------------------------

Public Function GethInstance() As Int32
Return hLib
End Function

Public Function IsLoaded() As Boolean
Return CBool(GethInstance())
End Function

End Class
With the dll in the bin folder:
[quoted text clipped - 29 lines]
 
A

Andy Sutorius via DotNetMonster.com

I answered my own question. It's in vb. My translation skills are not strong
enough to go from vb to c#.


Andy said:
Edwin,

Thank you for your code. What language was this written in C++? What version?

Andy
O well, here is my class, VB though:
[quoted text clipped - 125 lines]
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,009
Latest member
GidgetGamb

Latest Threads

Top