« Previous Next »

Thread: IIS7 with unmanaged code ???

Last post 07-07-2009 3:14 AM by Jano42. 2 replies.

Average Rating Rate It (5)

RSS

Page 1 of 1 (3 items)

Sort Posts:

  • 07-06-2009, 12:09 PM

    • Jano42
    • Not Ranked
    • Joined on 03-10-2008, 10:33 AM
    • Posts 2

    IIS7 with unmanaged code ???

    I've got an IIS v6 (Windows Server 2003) web site which uses some old DLL written with unmanaged code. (Aspx, C# using System.Runtime.InteropService to call unmanaged dll)

    But I've bought a new Windows Server 2008 with IIS7 and call to unmanaged DLL is not working.

    In fact my C# code call an unmanaged DLL but the code never ends. It's entering in DLL code (all is working until the <return> instruction, but it does not return to C# !!).

    I've tried to configure the application pool (classic mode), the web site properties... but to hard for me.At a time I got the error "IIS Worker Thread stop working"!

     

    A sample:

    ASPX

     

    <%@ Page Language="C#" AutoEventWireup="true"  CodeFile="Default.aspx.cs" Inherits="_Default" %>

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

    <html xmlns="http://www.w3.org/1999/xhtml">
    <head runat="server">
        <title></title>
    </head>
    <body>
        <form id="form1" runat="server">
        <div>
        <asp:Table runat="server">
            <asp:TableRow>
                <asp:TableCell>
                    <asp:Button ID="Button1" runat="server" Text="Button" onclick="Button1_Click" />
                </asp:TableCell>
            </asp:TableRow>
        </asp:Table>
        </div>
        </form>
    </body>
    </html>

     

    CS

     

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using System.Runtime.InteropServices;

    public partial class _Default : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        [DllImport("user32.dll")]
        static extern int MessageBoxA(int hWnd, string title, string msg, int type);

        protected void Button1_Click(object sender, EventArgs e)
        {
            MessageBoxA(0, "Hello world !!", "Hello world !!", 0);
        }
    }
     

     

    With IIS6, when the button is clicked a message box is displayed. With IIS7 nothing is displayed !

     

    Any idea ?

     

    Thanks for help !

  • 07-06-2009, 12:59 PM In reply to

    Re: IIS7 with unmanaged code ???

    Is the code above the one that is failing?

    You should never call MessageBox API from a Server-side service. IIS is running in a different Session than the interactive desktop so you will never see that message box even if it was allowed.

    Could you explain what the goal is of this? Isnt it the intent to show the message box to the user and not the server? If that is the case you should use javascript (alert method) for that.

     

  • 07-07-2009, 3:14 AM In reply to

    • Jano42
    • Not Ranked
    • Joined on 03-10-2008, 10:33 AM
    • Posts 2

    Re: IIS7 with unmanaged code ???

    OK sorry for this poor sample. That's not the code I use, it is just a sample to highlight the problem.

     

    Let's use another one:

     

    ASPX

    <%@ Page Language="C#" AutoEventWireup="true"  CodeFile="Default.aspx.cs" Inherits="_Default" %>


    <html xmlns="http://www.w3.org/1999/xhtml">
    <head runat="server">
        <title></title>
    </head>
    <body>
        <form id="form1" runat="server">
        <div>
        <asp:Table runat="server">
            <asp:TableRow>
                <asp:TableCell>
                    <asp:Button ID="Button1" runat="server" Text="Button" onclick="Button1_Click" />
                </asp:TableCell>
            </asp:TableRow>
            <asp:TableRow>
                <asp:TableCell>
                    <asp:Literal ID="Literal1" runat="server"></asp:Literal>
                </asp:TableCell>
            </asp:TableRow>
        </asp:Table>
        </div>
        </form>
    </body>
    </html>

     

    CS

    using System;
    using System.Runtime.InteropServices;

    public partial class _Default : System.Web.UI.Page
    {
        [DllImport("MyDLL.dll", EntryPoint = "?get_html@@YAPBDN@Z")]
        static extern string get_html(double value);
        
        protected void Button1_Click(object sender, EventArgs e)
        {
            Random a = new Random();
            Literal1.Text = get_html(a.NextDouble());
        }

    }

     

    DLL C++

    #include "stdafx.h"
    #include "stdio.h"

    char myBuffer[MAX_PATH];

    __declspec(dllexport) const char * get_html(double value)
    {
        memset(myBuffer, 0x00, MAX_PATH);
        sprintf(myBuffer, "<html><title>Test</title></head><body>Entered value : %.2f<br>Value x2 = %.2f</body></html>", value, value*2);
        return myBuffer;
    }

     

     

    If I set traces in C++, code goes until the line "return myBuffer" and never returns to C#

     

    Thanks for help

     

Page 1 of 1 (3 items)
Microsoft Communities