asp.net web页面 调用windows api

用vs可以调通的这个隐藏,显示窗体代码(参考:http://blog.csdn.net/studentsky/article/details/8141146

// 获取窗口标题
    [DllImport("user32", SetLastError = true)]
    public static extern int GetWindowText(IntPtr hWnd, StringBuilder lpString, int nMaxCount
    );

    //获取类的名字
    [DllImport("user32.dll")]
    private static extern int GetClassName(
    IntPtr hWnd,
    StringBuilder lpString,
    int nMaxCount
    );

    //根据坐标获取窗口句柄
    [DllImport("user32")]
    private static extern IntPtr WindowFromPoint(
    Point Point
    );

    [DllImport("user32.dll")]
    public static extern bool ShowWindow(IntPtr hWnd, int nCmdShow);
    public static class NativeMethods
    {
        internal const uint GW_OWNER = 4;

        internal delegate bool EnumWindowsProc(IntPtr hWnd, IntPtr lParam);

        [DllImport("User32.dll", CharSet = CharSet.Auto)]
        internal static extern bool EnumWindows(EnumWindowsProc lpEnumFunc, IntPtr lParam);

        [DllImport("User32.dll", CharSet = CharSet.Auto)]
        internal static extern int GetWindowThreadProcessId(IntPtr hWnd, out IntPtr lpdwProcessId);

        [DllImport("User32.dll", CharSet = CharSet.Auto)]
        internal static extern IntPtr GetWindow(IntPtr hWnd, uint uCmd);

        [DllImport("User32.dll", CharSet = CharSet.Auto)]
        internal static extern bool IsWindowVisible(IntPtr hWnd);
    }
    // 隐藏窗口
    public const int SW_HIDE = 0;
    public const int SW_SHOWNORMAL = 1;
    public const int SW_NORMAL = 1;
    public const int SW_SHOWMINIMIZED = 2;
    public const int SW_SHOWMAXIMIZED = 3;
    public const int SW_MAXIMIZE = 3;
    public const int SW_SHOWNOACTIVATE = 4;
    public const int SW_SHOW = 5;
    public const int SW_MINIMIZE = 6;
    public const int SW_SHOWMINNOACTIVE = 7;
    public const int SW_SHOWNA = 8;
    public const int SW_RESTORE = 9;
    public const int SW_SHOWDEFAULT = 10;
    public const int SW_FORCEMINIMIZE = 11;
    public const int SW_MAX = 11;

    protected void button2_Click(object sender, EventArgs e)
    {
        IntPtr formHandle = GetMainWindowHandle(GetProcessPID());
        ShowWindow(formHandle, SW_HIDE);
    }

    protected void button3_Click(object sender, EventArgs e)
    {
        IntPtr formHandle = GetMainWindowHandle(GetProcessPID());
        ShowWindow(formHandle, SW_SHOW);
    }
    /// <summary>
    /// 通过进程PID取得窗体句柄
    /// </summary>
    /// <param name="processId"></param>
    /// <returns></returns>
    public static IntPtr GetMainWindowHandle(int processId)
    {
        IntPtr MainWindowHandle = IntPtr.Zero;

        NativeMethods.EnumWindows(new NativeMethods.EnumWindowsProc((hWnd, lParam) =>
        {
            IntPtr PID;
            NativeMethods.GetWindowThreadProcessId(hWnd, out PID);

            if (PID == lParam &&
                
                NativeMethods.GetWindow(hWnd, NativeMethods.GW_OWNER) == IntPtr.Zero)//NativeMethods.IsWindowVisible(hWnd) && 把原文中这句去掉,因为要 隐藏后还能显示
            {
                MainWindowHandle = hWnd;
                return false;
            }

            return true;

        }), new IntPtr(processId));

        return MainWindowHandle;
    }

    /// <summary>
    /// 通过进程名取得进程PID
    /// </summary>
    /// <returns></returns>
    private int GetProcessPID()
    {
        try
        {
            //进程名称不要加上.exe
            System.Diagnostics.Process[] processes = System.Diagnostics.Process.GetProcessesByName("WindowsFormsApplication1");
            if (processes != null && processes.Length != 0)
            {
                return processes[0].Id;
            }

            throw new Exception();
        }
        catch (Exception ex)
        {
            throw ex;
        }
    }

放到iis中 调用不成功,为什么调用服务端的windows api也不行?

[DllImport("advapi32.dll")]                          //引入Windows的API 
        public static extern bool LogonUser(
        string lpszUsername,
        string lpszDomain,
        string lpszPassword,
        int dwLogonType,
        int dwLogonProvider,
        out int phToken
        ); 

1、调用windows 登录验证(放到iis中也正常):

 int num1; if (!LogonUser("huhaibin", "YYDDD-20111030O", "123456", 2, 0, out num1)) { this.listBox1.Items.Add("huhaibin登录失败"); } else { this.listBox1.Items.Add("huhaibin登录成功!"); }

2、还有一个调用电脑发声的 放到iis中也正常,

 [DllImport("kernel32.dll")]
    private static extern int Beep(int dwFrep, int dwDuration);

调用:

protected void Button2_Click(object sender, EventArgs e)
    {
        Beep(1000, 500);//扬声器发声
    }

就是 用到user32.dll 放在iis中不起作用,难道和dll有关?请懂的 指点一二

为了解决上面调用的不起的现象,参考:(whuthj) http://whuthj.iteye.com/blog/665699 创建activeX控件,把上面的 隐藏、显示代码替换进去,安装控件后,在iis中运行测试页,正常(可以访问客户端的windows api,在服务端浏览网页当然也能调用服务端的,但是仍然没有解决 客户端调用服务端api的这个问题)

也许和权限有关,有空看看:http://www.cnblogs.com/fish-li/archive/2012/05/07/2486840.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值