1. Rick Domogalik
  2. PowerBuilder
  3. Monday, 2 March 2020 16:05 PM UTC

I have an application that uses ShellExecute and Printto to Batch print documents.  The program needs to be very specific and print them in order.  The issue I am having is the order.  When I send the batch print, based on what native application is opening the file, they go out of order.  For example, it takes slightly longer to open PDF than a word doc, so word will print 3-4 in the time that it takes adobe to open and print.  

I have found some examples in visual studio of how to handle this by waiting on a return code that the print has been sent to the queue, I am struggling getting the process to return the handle to tell me it is done.

Here is the Visual Studio example:

/////////////////////////////////////////////////////////////////
      SHELLEXECUTEINFO ShellInfo;
      ZeroMemory( & ShellInfo, sizeof( SHELLEXECUTEINFO ) );
      ShellInfo.cbSize = sizeof( SHELLEXECUTEINFO );
      ShellInfo.lpVerb = TEXT( "printto" );
      ShellInfo.lpFile = pszFile;
      ShellInfo.lpParameters = szPrinter;
      ShellInfo.nShow = SW_SHOWMINIMIZED;
      ShellInfo.fMask = SEE_MASK_FLAG_DDEWAIT
                                  | SEE_MASK_NOCLOSEPROCESS ;
      if( ShellExecuteEx( & ShellInfo ) )
      {
         if( (int) ShellInfo.hInstApp > 32 )
         {
            if( ShellInfo.hProcess )
            {
               DWORD dwExitCode = STILL_ACTIVE;
               while( dwExitCode == STILL_ACTIVE )
               {
                  if( ! GetExitCodeProcess( ShellInfo.hProcess, & dwExitCode ) )
                  {
                     dwExitCode = 0;
                  }
               }
               CloseHandle( ShellInfo.hProcess );
            }
         }
      }
/////////////////////////////////////////////////////////////////

 

And this is my PowerBuilder code:

I have defined this Local External Function:

FUNCTION long ShellExecute( ulong hWnd, string Operation, string lpFile, string  lpParameters, string lpDirectory, int nShowCmd ) LIBRARY "shell32.dll" ALIAS FOR "ShellExecuteW"

Here is a copy of my script:

string ls_path, ls_doc, ls_recnum
long ll_count, ll_tot
integer   li_return_code
string     ls_null
long       ll_null
SetNull(ls_null)
SetNull(ll_null)
//loop through list
ll_count = 1
dw_print_prod.settrans(sqlca)
dw_print_prod.retrieve(is_current_prod)
ll_tot = dw_print_prod.rowcount()
do while ll_count <= ll_tot
 ls_path = dw_print_prod.getItemstring(ll_count, "doc_path")
 
 ls_doc = dw_print_prod.getItemstring(ll_count, "doc_id")
  li_return_code = ShellExecute(ll_null, "printto", ls_path, ls_null, ls_null, 0)
 
  if li_return_code < 32 then
  messagebox('Error ' + string(li_return_code), 'Error sending print - ' + ls_doc + '.')
 end if
 
  // lf_activity_add(ls_recnum, 'Printed - Bulk', '', '')
 ll_count = ll_count + 1
loop
 
The Visual studio example said to use ShellexecuteEx and look for the handle SEE_MASK_FLAG_DDEWAIT.  I am out of my depth here for converting this to work in PowerBuilder. 
 
Anyone printed like this before?  And can give me some guidance?  
 
Thanks.
 
Rick
Accepted Answer
Roland Smith Accepted Answer Pending Moderation
  1. Monday, 2 March 2020 16:15 PM UTC
  2. PowerBuilder
  3. # Permalink
2
Votes
Undo

Try my RunAndWait example:

https://www.topwizprogramming.com/freecode_runandwait.html

It uses ShellExecute but adds WaitForSingleObject and GetExitCodeProcess.

 

Comment
The of_get_shellexecute function only returns a command line string. Try using of_ShellRun:



ll_return = in_rwait.of_ShellRun(ls_filename, "PrintTo", Hide!)

  1. Roland Smith
  2. Monday, 2 March 2020 17:46 PM UTC
It supports Hide!, Normal!, Maximized!, and Minimized! for the third argument.
  1. Roland Smith
  2. Monday, 2 March 2020 17:48 PM UTC
Hi Roland, its been almost a year since I was working on this code. Covid derailed out dev team for quite some time. But we are now getting back into our development cycle. We are revisiting this printing project and have run into an issue. We are able to send our prints using the run and wait code, but it hangs with each print. Basically we are sending all PDF files. The code will open the first document, send the print and hang in a loop. We see Acrobat open and minimize to the task bar (even though we have used the Hide! argument), and print the file, then close the document, but Acrobat stays open with no document loaded. If we bring up acrobat and close the program, the loop will continue to the next document and print. Then hang as mentioned above.

So to complete the bulk print, the user has to click close on acrobat after each print. With potentially thousands of docs needing printed in order in these batches, not going to work.



We have done some testing and see that the run and wait function does not time out or return until the user closes acrobat. It stays in the called function no matter how long we wait.



Have you seen this type of behavior with run and wait and Acrobat?



Any help would be greatly appreciated.
  1. Rick Domogalik
  2. Friday, 26 February 2021 15:56 PM UTC
There are no comments made yet.
Rick Domogalik Accepted Answer Pending Moderation
  1. Friday, 26 February 2021 16:21 PM UTC
  2. PowerBuilder
  3. # 1
0
Votes
Undo

Hi Roland, its been almost a year since I was working on this code. Covid derailed out dev team for quite some time. But we are now getting back into our development cycle. We are revisiting this printing project and have run into an issue. We are able to send our prints using the run and wait code, but it hangs with each print. Basically we are sending all PDF files. The code will open the first document, send the print and hang in a loop. We see Acrobat open and minimize to the task bar (even though we have used the Hide! argument), and print the file, then close the document, but Acrobat stays open with no document loaded. If we bring up acrobat and close the program, the loop will continue to the next document and print. Then hang as mentioned above.

So to complete the bulk print, the user has to click close on acrobat after each print. With potentially thousands of docs needing printed in order in these batches, not going to work.



We have done some testing and see that the run and wait function does not time out or return until the user closes acrobat. It stays in the called function no matter how long we wait.



Have you seen this type of behavior with run and wait and Acrobat?



Any help would be greatly appreciated.

Comment
You could try a different PDF viewer, Foxit for example.
  1. Roland Smith
  2. Friday, 26 February 2021 17:00 PM UTC
There are no comments made yet.
  • Page :
  • 1


There are no replies made for this question yet.
However, you are not allowed to reply to this question.