FindWindow return 0 in Windows 7

3.2k views Asked by At

I have a C# program. The program creates an Adobe Reader process and prints a PDF document. It works fine in Windows XP, but does not work in Windows 7. I have checked that the AcroRd32.exe path is correct in Windows 7. The FindWindow method always returns 0 in Windows 7.

    [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;
    }
1

There are 1 answers

0
prudan On

Findwindow can depend a great deal on how you are running the application. It sounds like you might be running it as a scheduled task or a windows service. These run in a different session than what the user's desktop window is in, so it won't see or be able to interact with them.

They introduced this change in Windows Vista, so applications that do this will work just fine in XP but fail in Windows Vista or later.

Here's a link from msdn about it: Application Compatibility: Session 0 isolation