Re: DllImport with ASP.NET ?

Tech-Archive recommends: Repair Windows Errors & Optimize Windows Performance

From: Brian Anderson (nospam_at_127.0.0.1)
Date: 12/26/04

  • Next message: jongalloway: "Re: Exclude from Form Authentication"
    Date: Sun, 26 Dec 2004 08:58:00 -0000
    
    

    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" <crtimmons@X_NOSPAM_Xcrtimmonsinc.com> wrote in message
    news:Xns95CAB4AAA66BBcrtimmonscrtimmonsin@207.46.248.16...
    > "Brian Anderson" <nospam@127.0.0.1> wrote in
    > news:eXDWhys6EHA.1264@TK2MSFTNGP12.phx.gbl:
    >
    > > 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>
    

  • Next message: jongalloway: "Re: Exclude from Form Authentication"

    Relevant Pages

    • RE: fax notification
      ... Thank you for posting in the SBS newsgroup. ... message "mail system wrong DLL", and the inbound fax cannot was deliver to ... Please copy the exact error message and paste it in your reply. ...
      (microsoft.public.windows.server.sbs)
    • Re: Redirecting stderr
      ... console application that makes calls to the DLL. ... That sounds like a question more appropriate for a Windows ... libraries). ... it doesn't print an error message; ...
      (comp.lang.c)
    • Re: C++/TCL Need Solution to Compile Error c2784
      ... jnior_300.h"}" and created other errors where it didn't even compile. ... Error Message: ... Which appears to be an error because it depending on the SDK dll ... DLL properties to see dependencies and where the dependencies should ...
      (comp.lang.tcl)
    • Re: C++/TCL Need Solution to Compile Error c2784
      ... Error Message: ... first nonwhite space ... Which appears to be an error because it depending on the SDK dll ... DLL properties to see dependencies and where the dependencies should ...
      (comp.lang.tcl)
    • Re: Pinvoke help
      ... you say you put the dll one directory down from where the .sln ... public static extern int StartCamera; ... MouseButtons button, Int32 clicks) ...
      (microsoft.public.dotnet.languages.csharp)