How do I send a string to a process I have opened with the help of createprocess?

I've written something like this with the help of what I've found here in Stackoverflow:

    var
    StartUpInfo:TStartUpInfo;
    ProcessInfo: TProcessInformation;
    Creationflags: Cardinal;

    FillChar(StartUpInfo, sizeOf(TStartupinfo),0);
    StartUpInfo.cb := SizeOf(StartupInfo);
    Creationflags := NORMAL_PRIORITY_CLASS;

    if Createprocess(nil, Pchar('c:\program files\adobe\reader 9.0\reader\acrord32.exe'
    + ' ' + '/h /O /N /T "c:\ps\test2.pdf" "Generic printer"'),nil, nil,False, 
    creationflags, nil, 0 , StartUpInfo ,  ProcessInfo) then

    begin
      WaitForSingleObject(ProcessInfo.HProcess, infinite);
         begin
           //write some information into the process (a window that opens in 
           //acrobatreader) and then I want to press the enter button            
         end;
      CloseHandle(processinfo.hProcess);

    end;

So what I want is to write a line when the process is finished (right after waitforsingleobject) and then press the "enter" button, and finally close the process when it is finished. Does Anyone know how to interact with external processes? I am a total newbie when it comes to programming.

Best Regards

有帮助吗?

解决方案

You appear to be trying programmatically to get Acrobat Read to print your document. Your problem is that you can't persuade it to do so without it asking for user interaction.

Your current code cannot work because you are waiting for the process to terminate. Once you start with a simple call to WaitForSingleObject, none of your code can run until the process terminates. You do not get an opportunity for you code to click that button.

So what you need to do is to use a different waiting strategy. Since this appears to be a fairly crude hack, I'll suggest something I would not normally suggest: polling. Instead of waiting with an infinite timeout you should wait, in a loop, with a timeout value of, say, 250ms. Each time the wait terminates you check to see whether the process has terminated or alternatively whether wait timed out.

When the wait has timed out, you have an opportunity to look for the dialog window that you want to find. You should be able to locate it with FindWindowEx(). You can discover its class name using Spy++. Once you have found the dialog window then you need to locate the window handle for the edit control and the button you want to click: EnumChildWindows() should do the trick.

Once you have the handles of the controls you want to manipulate you need to work out how to manipulate them. If you are lucky you can send the string with a call to SetWindowText if it is an EDIT control or similar. Otherwise you may have to fake the key-presses or maybe send it a WM_PASTE. The button may be more troublesome. Clicking it involves sending a WM_COMMAND to the buttons parent. Again Spy++ can show you what messages are sent when you click a button.

Of course, you may not find your window when you call FindWindowEx() because it hasn't shown yet. When that happens you go round the loop again with a call to WaitForSingleObject().

Finally, a much better way to handle this would be to use a software library dedicated to printing PDF!

其他提示

What you want to do isn't quite clear.

There are several ways to send stuff to a process;

  • OLE Automation (eg: MSWord)
  • via CMDLine (eg: notepad file.txt)
  • via STDIN (eg: cmd apps that only using std io)
  • via manipulation (eg: sending specific wm messages to a certain process window)

As I see it, you need the last approach.

Basically, run the right Windows API functions to get a list of windows from your process. Then use a program like winspy, winspy++ or the one with delphi (forgot what it's called, it is in the Delphi tools menu). Use the said tool to find the class name of the window you want to send messages to, make your code filter out all windows till you find this particular one and then use SendMessage api.

It sounds complicated, and it is (for a beginner). I suggest you search around for some tutorials. The code uses some 5 or more different API calls, plus I'm pretty sure you need to use a callback.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top