1

我有一个 C# 程序。该程序创建一个 Adob​​e Reader 进程并打印一个PDF文档。它在 Windows XP 中工作正常,但在 Windows 7 中不起作用。我检查了 Windows 7 中的AcroRd32.exe路径是否正确。该FindWindow方法在 Windows 7 中总是返回 0。

    [DllImport("user32.dll")]
    public static extern bool ShowWindow(IntPtr hWnd, int nCmdShow);

    [DllImport("User32.dll")]
    public static extern IntPtr FindWindow(string ClassN, string WindN);

    [DllImport("user32.dll", EntryPoint = "SendMessageA")]
    private static extern int SendMessage(IntPtr hwnd, int wMsg, int wParam, int lParam);

    [DllImport("user32.dll", SetLastError = true)]
    static extern IntPtr FindWindowEx(IntPtr hwndParent, IntPtr hwndChildAfter, string lpszClass, string lpszWindow);

    [DllImport("shell32.dll ")]
    public static extern int ShellExecute(IntPtr hwnd, string lpszOp, string lpszFile, string lpszParams, string lpszDir, int FsShowCmd);

    [DllImport("kernel32.dll", CharSet = CharSet.Auto)]
    public static extern uint WinExec(string lpCmdLine, uint uCmdShow);

    public bool isAcrobatExsists(string acrobatLoc)
    {
        IntPtr currentHandle = getWindowHandlerByClass("AcrobatSDIWindow");
        if (currentHandle != IntPtr.Zero)
        {
            return true;
        }
        return false;
    }

    private static IntPtr getWindowHandlerByClass(string className)
    {
        IntPtr currentHandle = FindWindow(className, null);
        return currentHandle;
    }

    private static IntPtr getWindowHandlerByName(string appName)
    {
        IntPtr currentHandle = FindWindow(null, appName);
        return currentHandle;
    }
4

1 回答 1

1

Findwindow 很大程度上取决于您运行应用程序的方式。听起来您可能将其作为计划任务或 Windows 服务运行。它们在与用户桌面窗口所在的会话不同的会话中运行,因此它不会看到或无法与它们交互。

他们在 Windows Vista 中引入了此更改,因此执行此操作的应用程序在 XP 中可以正常工作,但在 Windows Vista 或更高版本中失败。

这是来自 msdn 的关于它的链接: 应用程序兼容性:会话 0 隔离

于 2014-07-21T18:19:32.407 回答