DllImport with ASP.NET ?

B

Brian Anderson

Hello,

is it possible to use DllImport to call a DLL in ASP.NET ?
Or is it necessarry that my DLL has to be copied into \System32 ?

My DLL is a native C++ 7.1 DLL (not managed, no COM, no regsvr32) and uses
Assembler to make some math in SSE.
The result is given out as an Int.

Using a C# console app (code below) it work fine & fast.

However, once again ASP.NET refuses to execute my inline ASP.NET code (code
below).

Error Code:

Description: An unhandled exception occurred during the execution of the
current web request. Please review the stack trace for more information
about the error and where it originated in the code. Exception Details:
System.DllNotFoundException: Unable to load DLL (pac.dll).nable to load DLL
(pac.dll).]Stack Trace: [DllNotFoundException: Unable to load DLL
(pac.dll).]

:(

Even the tip out of G00gle to use the full path in DllImport wasn't a
solution.
I've check this on my ISPs websapce and on my local IIS6.

Is the reason for this problem the general extremely over-limited runtime
permission of ASP.NET ?
Can ASP.NET or IIS6 be told to allow DllImport calls to my DLL ?
My ISP is ready to make configuration changes for my case but don't know a
solution, too.

Thx for your opinions !


// Code EXE Console

public class Class1
{
[DllImport("pac.dll")]
public static extern int PaqGetVersion();

public static void Main(string[] args)
{
int t = Class1.PaqGetVersion();
Console.WriteLine(t);

[...]


// CODE ASP.NET

<%@ Page Language="C#" Debug="true" %>
<%@ Import Namespace="System.Runtime.InteropServices" %>
<HTML>
<HEAD>
<Meta Name="CODE_LANGUAGE" Content="C#">

<Script Language=CS Runat=Server>

[DllImport(@"pac.dll")]
public static extern int PaqGetVersion();

void Page_Load(object sender, System.EventArgs e)
{
int i = PaqarGetVersion();
TextBox1.Text = i.ToString();
}
</script>
</HEAD>

[...]
 
C

Chris R. Timmons

Hello,

is it possible to use DllImport to call a DLL in ASP.NET ?
Or is it necessarry that my DLL has to be copied into \System32
?

My DLL is a native C++ 7.1 DLL (not managed, no COM, no
regsvr32) and uses Assembler to make some math in SSE.
The result is given out as an Int.

Using a C# console app (code below) it work fine & fast.

However, once again ASP.NET refuses to execute my inline ASP.NET
code (code below).

Error Code:

Description: An unhandled exception occurred during the
execution of the current web request. Please review the stack
trace for more information about the error and where it
originated in the code. Exception Details:
System.DllNotFoundException: Unable to load DLL (pac.dll).nable
to load DLL (pac.dll).]Stack Trace: [DllNotFoundException:
Unable to load DLL (pac.dll).]

:(

Even the tip out of G00gle to use the full path in DllImport
wasn't a solution.
I've check this on my ISPs websapce and on my local IIS6.

Is the reason for this problem the general extremely
over-limited runtime permission of ASP.NET ?
Can ASP.NET or IIS6 be told to allow DllImport calls to my DLL ?
My ISP is ready to make configuration changes for my case but
don't know a solution, too.

Thx for your opinions !


// Code EXE Console

public class Class1
{
[DllImport("pac.dll")]
public static extern int PaqGetVersion();

public static void Main(string[] args)
{
int t = Class1.PaqGetVersion();
Console.WriteLine(t);

[...]


// CODE ASP.NET

<%@ Page Language="C#" Debug="true" %>
<%@ Import Namespace="System.Runtime.InteropServices" %>
<HTML>
<HEAD>
<Meta Name="CODE_LANGUAGE" Content="C#">

<Script Language=CS Runat=Server>

[DllImport(@"pac.dll")]
public static extern int PaqGetVersion();

void Page_Load(object sender, System.EventArgs e)
{
int i = PaqarGetVersion();
TextBox1.Text = i.ToString();
}
</script>
</HEAD>

[...]

Brian,

It could be a security problem, but it seems more likely that
pac.dll is not being found. This is because it is not in the same
folder as your .aspx page dll [*1], nor is it on the system's
search path for dlls [*2].

You stated that using pac.dll's full path in the DllImport
attibute didn't work. Did you get an error message?
If so, what was it?

It's also possible that your application does not have
sufficient permissions to call unmanaged code. You can
find out by running the .aspx page at the end of this article.

If your page cannot call unmanaged code, you will need to have
your ISP configure the .Net security to grant your pages that
permission.

--
Hope this helps.

Chris.
-------------
C.R. Timmons Consulting, Inc.
http://www.crtimmonsinc.com/

------------------------------------------------------------

[*1]
ASP.Net compiles each in-lined .aspx page into its own separate
DLL and places it in a temporary directory under c:\windows\microsoft.net.
An example would be:

C:\WINDOWS\Microsoft.NET\Framework\v1.1.4322\Temporary ASP.NET Files\WebApplication1\64f05764\576f54e7

There's no way to know what name ASP.Net will give this temporary
directory, so you can't put pac.dll there.

In contrast, code in separate code-behind files is compiled into a
single DLL, and that is placed into a \bin folder under the web application,
e.g. c:\inetpub\wwwroot\WebApplication1\bin.

[*2]

Windows uses the algorithm described here to locate DLLs:

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dllproc/base/loadlibrary.asp

------------------------------------------------------------

<%@ Page Language="C#" AutoEventWireup="True" %>
<%@ Import Namespace="System.Security" %>
<%@ Import Namespace="System.Security.Permissions" %>

<html>
<script runat="server">

private static bool CanCallUnmanagedCode
{
get
{
try
{
new SecurityPermission(SecurityPermissionFlag.UnmanagedCode).Demand();
}
catch(SecurityException)
{
return false;
}

return true;
}
}

void Page_Load(Object sender, EventArgs e)
{
if (CanCallUnmanagedCode)
Label1.Text = "This page can call unmanaged code.";
else
Label1.Text = "This page can NOT call unmanaged code.";
}

</script>

<body>
<form runat=server>
<asp:Label id="Label1" Text="Label Control" runat="server"/>
</form>
</body>
</html>
 
B

Brian Anderson

Hello Chris !
You stated that using pac.dll's full path in the DllImport
attibute didn't work. Did you get an error message?
If so, what was it?

It was exactly the same error message (not found).

Will try your tip and report back.

Thx!

---

Chris R. Timmons said:
Hello,

is it possible to use DllImport to call a DLL in ASP.NET ?
Or is it necessarry that my DLL has to be copied into \System32
?

My DLL is a native C++ 7.1 DLL (not managed, no COM, no
regsvr32) and uses Assembler to make some math in SSE.
The result is given out as an Int.

Using a C# console app (code below) it work fine & fast.

However, once again ASP.NET refuses to execute my inline ASP.NET
code (code below).

Error Code:

Description: An unhandled exception occurred during the
execution of the current web request. Please review the stack
trace for more information about the error and where it
originated in the code. Exception Details:
System.DllNotFoundException: Unable to load DLL (pac.dll).nable
to load DLL (pac.dll).]Stack Trace: [DllNotFoundException:
Unable to load DLL (pac.dll).]

:(

Even the tip out of G00gle to use the full path in DllImport
wasn't a solution.
I've check this on my ISPs websapce and on my local IIS6.

Is the reason for this problem the general extremely
over-limited runtime permission of ASP.NET ?
Can ASP.NET or IIS6 be told to allow DllImport calls to my DLL ?
My ISP is ready to make configuration changes for my case but
don't know a solution, too.

Thx for your opinions !


// Code EXE Console

public class Class1
{
[DllImport("pac.dll")]
public static extern int PaqGetVersion();

public static void Main(string[] args)
{
int t = Class1.PaqGetVersion();
Console.WriteLine(t);

[...]


// CODE ASP.NET

<%@ Page Language="C#" Debug="true" %>
<%@ Import Namespace="System.Runtime.InteropServices" %>
<HTML>
<HEAD>
<Meta Name="CODE_LANGUAGE" Content="C#">

<Script Language=CS Runat=Server>

[DllImport(@"pac.dll")]
public static extern int PaqGetVersion();

void Page_Load(object sender, System.EventArgs e)
{
int i = PaqarGetVersion();
TextBox1.Text = i.ToString();
}
</script>
</HEAD>

[...]

Brian,

It could be a security problem, but it seems more likely that
pac.dll is not being found. This is because it is not in the same
folder as your .aspx page dll [*1], nor is it on the system's
search path for dlls [*2].

You stated that using pac.dll's full path in the DllImport
attibute didn't work. Did you get an error message?
If so, what was it?

It's also possible that your application does not have
sufficient permissions to call unmanaged code. You can
find out by running the .aspx page at the end of this article.

If your page cannot call unmanaged code, you will need to have
your ISP configure the .Net security to grant your pages that
permission.

--
Hope this helps.

Chris.
-------------
C.R. Timmons Consulting, Inc.
http://www.crtimmonsinc.com/

------------------------------------------------------------

[*1]
ASP.Net compiles each in-lined .aspx page into its own separate
DLL and places it in a temporary directory under c:\windows\microsoft.net.
An example would be:

C:\WINDOWS\Microsoft.NET\Framework\v1.1.4322\Temporary ASP.NET Files\WebApplication1\64f05764\576f54e7

There's no way to know what name ASP.Net will give this temporary
directory, so you can't put pac.dll there.

In contrast, code in separate code-behind files is compiled into a
single DLL, and that is placed into a \bin folder under the web application,
e.g. c:\inetpub\wwwroot\WebApplication1\bin.

[*2]

Windows uses the algorithm described here to locate DLLs:

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dllproc/base/loadlibrary.asp

------------------------------------------------------------

<%@ Page Language="C#" AutoEventWireup="True" %>
<%@ Import Namespace="System.Security" %>
<%@ Import Namespace="System.Security.Permissions" %>

<html>
<script runat="server">

private static bool CanCallUnmanagedCode
{
get
{
try
{
new SecurityPermission(SecurityPermissionFlag.UnmanagedCode).Demand();
}
catch(SecurityException)
{
return false;
}

return true;
}
}

void Page_Load(Object sender, EventArgs e)
{
if (CanCallUnmanagedCode)
Label1.Text = "This page can call unmanaged code.";
else
Label1.Text = "This page can NOT call unmanaged code.";
}

</script>

<body>
<form runat=server>
<asp:Label id="Label1" Text="Label Control" runat="server"/>
</form>
</body>
</html>
 

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

Top