Search code examples
c#windows-7createprocessadobe-reader

CreateProcess cannot start Adobe Reader in Windows 7


I have a c# program which opens the adobe reader and print the pdf for the user. It works fine in winxp, but not win7.

After investigating, I found that the problem is in the CreateProcess function. In win7, CreateProcess cannot start the adobe reader.

Please help if anyone knows how to solve it.

public bool startup(string acrobatLoc)
{
    bool result = false;
    if (!isAcrobatExsists(acrobatLoc))
    {
        sInfo = new STARTUPINFO();
        pInfo = new PROCESS_INFORMATION();
        sInfo.dwX = -1;
        sInfo.dwY = -1;
        sInfo.wShowWindow = 0;
        sInfo.dwXSize = -1;
        sInfo.dwYSize = -1;

        result = CreateProcess(null, new StringBuilder(acrobatLoc), null, null, false, 0, null, null, ref sInfo, ref pInfo);
        acrobatPHandle = pInfo.dwProcessId;
        IntPtr parentHandle = IntPtr.Zero;
        if (result)
        {
            while ((parentHandle = getWindowHandlerByClass("AcrobatSDIWindow")) == IntPtr.Zero)
            {
                System.Threading.Thread.Sleep(1 * 500);
            }
            acrobatMainWHandle = parentHandle;
            System.Threading.Thread.Sleep(3 * 1000);
        }
    }

    return result;
}

Solution

  • You shouldn't need to do P/Invoke to execute Acrobat, as .Net has it's own wrapper, Process.

    So you could do something like:

    Process viewer = new Process();
    viewer.StartInfo.FileName = "{path to acrobat}"; // Don't forget to substitute {path to acrobat}
    viewer.StartInfo.Arguments = "{command line arguments}"; // Don't forget to substitute {command line arguments}
    viewer.StartInfo.UseShellExecute = false;
    viewer.Start();
    

    Better still, you could open the PDF reader by using shell execute, for example:

    Process viewer = new Process();
    viewer.StartInfo.FileName = "{path to PDF document}"; // Don't forget to substitute {path to PDF document}
    viewer.StartInfo.UseShellExecute = true;
    viewer.Start();