سؤال

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;
}
هل كانت مفيدة؟

المحلول

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

نصائح أخرى

You need to set sInfo.cb to the size of the structure:

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

Of course, this depends on having defined the struct correctly (which we can't see).

I would recommend Rowland Shaw's answer of using the built-in .NET wrapper, the Process class.

make sure acrobat path is correct. It can contain x86 as an example, C:\Program Files (x86)\Adobe\Reader 9.0

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top