471,067 Members | 1,561 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,067 software developers and data experts.

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

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);
}

}
}

--
Message posted via http://www.dotnetmonster.com
Jan 18 '06 #1
6 1914
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

--
Co-founder, Eggheadcafe.com developer portal:
http://www.eggheadcafe.com
UnBlog:
http://petesbloggerama.blogspot.com


"Andy Sutorius via DotNetMonster.com" wrote:
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);
}

}
}

--
Message posted via http://www.dotnetmonster.com

Jan 18 '06 #2
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)
"Andy Sutorius via DotNetMonster.com" <u9025@uwe> schreef in bericht
news:5a8af5601547b@uwe...
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);
}

}
}

--
Message posted via http://www.dotnetmonster.com

Jan 18 '06 #3
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?

Peter Bromberg [C# MVP] wrote:
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]
}
}


--
Message posted via http://www.dotnetmonster.com
Jan 18 '06 #4
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" <u9025@uwe> schreef in bericht
news:5a8b25a4918b3@uwe...
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?

Peter Bromberg [C# MVP] wrote:
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]
}
}


--
Message posted via http://www.dotnetmonster.com

Jan 18 '06 #5
Edwin,

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

Andy

Edwin Knoppert wrote:
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]
}
}


--
Message posted via http://www.dotnetmonster.com
Jan 18 '06 #6
I answered my own question. It's in vb. My translation skills are not strong
enough to go from vb to c#.
Andy Sutorius wrote:
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]
> }
> }


--
Message posted via http://www.dotnetmonster.com
Jan 18 '06 #7

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

2 posts views Thread by Fred Armitage | last post: by
3 posts views Thread by moondaddy | last post: by
5 posts views Thread by Richard Dixson | last post: by
171 posts views Thread by tshad | last post: by
17 posts views Thread by tshad | last post: by
3 posts views Thread by Bart Van Hemelen | last post: by
reply views Thread by leo001 | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.