2014-01-08 23 views
1

我有一個C#程序。該程序創建一個Adobe Reader進程並打印一個PDF文檔。它工作在Windows XP中正常,但在Windows 7我已經檢查不工作的AcroRd32.exe路徑是正確的在Windows 7的FindWindow方法總是返回0在Windows 7FindWindow在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; 
    } 
+1

你是如何調用'getWindowHandlerByName()'?我啓動了Adobe Reader X,運行你的代碼(調用'getWindowHandlerByName(「Adobe Reader」);'),並找回了一個有效的句柄。 –

+0

我試過了。它仍然返回0。 – user3171372

回答

1

FindWindow函數可以依賴很大處理你如何運行應用程序。這聽起來像你可能將它作爲計劃任務或Windows服務運行。這些運行在與用戶桌面窗口不同的會話中,因此它不會看到或能夠與它們交互。

他們在Windows Vista中引入了此更改,因此,這樣做的應用程序在XP中可以正常工作,但在Windows Vista或更高版本中會失敗。

下面是從MSDN它的鏈接:Application Compatibility: Session 0 isolation

相關問題