CreateProcess无法在Windows 7中启动Adobe Reader

时间:2014-01-08 09:11:09

标签: c# windows-7 createprocess adobe-reader

我有一个c#程序打开adobe reader并为用户打印pdf。它在winxp中运行良好,但不是win7。

经过调查,我发现问题出在CreateProcess函数中。在win7中,CreateProcess无法启动adobe reader。

如果有人知道如何解决,请帮助。

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

3 个答案:

答案 0 :(得分:1)

您不需要执行P / Invoke来执行Acrobat,因为.Net拥有自己的包装器Process

所以你可以这样做:

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();

更好的是,您可以使用shell execute打开PDF阅读器,例如:

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();

答案 1 :(得分:1)

您需要将sInfo.cb设置为结构的大小:

sInfo.cb = Marshal.SizeOf(typeof(STARTUPINFO));

当然,这取决于正确定义结构(我们看不到)。

我建议Rowland Shaw's answer使用内置的.NET包装器Process class

答案 2 :(得分:0)

确保acrobat路径正确无误。它可以包含x86作为例子, C:\ Program Files(x86)\ Adob​​e \ Reader 9.0