0

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;
}
Bobrovsky
  • 13,789
  • 19
  • 80
  • 130
Manson
  • 37
  • 1
  • 6
  • 1
    What do you mean by _not work_? Any exception or error message? – Soner Gönül Jan 08 '14 at 09:13
  • what is the value of `acrobatLoc`? Is your Windows 7 box running a 64-bit architecture? What value does the debugger tell you `isAcrobatExsists(acrobatLoc)` returns? – Rowland Shaw Jan 08 '14 at 09:15
  • I check the path is correct. I am using adobe reader 8. The path is c:\Program Files (x86)\Adobe\Reader 8.0\Reader\AcroRd32.exe. I use reader 11 before, I tried to uninstall the reader and install reader 8 again which is same as the client machine. – Manson Jan 08 '14 at 09:41
  • I am using win7 64bit version – Manson Jan 08 '14 at 09:43
  • CreateProcess always return false in win7. How can I get more error message? Thanks! – Manson Jan 08 '14 at 09:52
  • Why are you using P/Invoke instead of the .Net wrappers? – Rowland Shaw Jan 08 '14 at 10:29

3 Answers3

1

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();
Rowland Shaw
  • 37,700
  • 14
  • 97
  • 166
  • Thank for your help, I can start the process now. However, how can I get the process id. Before I use the pInfo.dwProcessId; – Manson Jan 09 '14 at 03:31
1

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.

Community
  • 1
  • 1
Bradley Grainger
  • 27,458
  • 4
  • 91
  • 108
0

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

realnero
  • 994
  • 6
  • 12