Printing using the printer API

DevO 21 Reputation points
2021-08-06T02:32:34.987+00:00

Hello, I have an inquiry in the process of printing the printer.

Below are three of my program descriptions.

[My Application info]

  1. Hook the APIs related to printing, such as StartDoc, StartPage, ExtTextOut, EndPage, and EndDoc.
  2. After checking the entire string obtained from ExtTextOut in EndDoc API during printing, when a specific string is detected, the printer is deleted and the approval page is viewed.
  3. After receiving approval, I would like to reprint the existing printer.
    (JOB_CONTROL_RESUME will not be used in my program. You must print again after deleting unconditionally.)
  4. Don't show the dialog again when reprinting.
    With all the property information of the print that was started at the beginning in the DocumentProperties API, the same print is restarted with that information.

[Current Re-Print Logic]

This is my current status.
If you delete Print and proceed with printing again (this is the logic to run StartDoc, StartPage, Endpage, EndDoc that was previously in progress after creating a new printer DC), in a specific document (textbox of Excel, etc.), normal output is not possible.
I checked MSDN and it was confirmed that there is no API to output a specific print name in Windows, so I wrote the code as above.

[PowerShell Command]

Also, for printing, I tried the following command using PowerShell to print several files (ppt, xls, doc ...etc) including txt files.
PowerShell > Start-Process -FilePath "C:\Users\DeveloperO\Desktop\1234.docx" -Verb print
The above command worked, but the result is not what I want because a new process has been started.

Starting a new process and starting a print doesn't work for me.

[Please Answer]

Question 0)
Is there a command to print an open process without starting the process using powershell?
(Currently, I know that only txt files can be printed.)

Question 1)
I've been working in C++ language, is there an API to programmatically request a printer using C++?

Question 2)
I've also tried the ScheduleJob API. But this keeps giving 3002 Error. (Cannot find spool file.)

The code for this is below, can you give me some advice?
////////////////////////////////////////
/////////ScheduleJob Code//////////
////////////////////////////////////////

BOOL bResult = FALSE;
HANDLE hPrinter = NULL;
DWORD dNeedNum = 0;
CHAR szFullPath[MAX_PATH] = {0,};
DWORD size = 4096;
ADDJOB_INFO_1 * JobInfo = {0,};

W2M(g_wszFullPath, szFullPath);

bResult = OpenPrinter("Printer Name", &hPrinter, NULL );
if(bResult == FALSE)
{
printf("Error = %d",GetLastError());
}

JobInfo=(ADDJOB_INFO_1 *)LocalAlloc((LMEM_FIXED/LMEM_ZEROINIT),size);
bResult = AddJob(hPrinter,1,(BYTE *)JobInfo,size,&dNeedNum);

if(bResult == FALSE)
{
printf("Error = %d",GetLastError());
}

ZeroMemory(JobInfo->Path, sizeof(JobInfo->Path));
StringCbCopy(JobInfo->Path, sizeof(szFullPath), szFullPath);
bResult = ScheduleJob(hPrinter,JobInfo->JobId);
if(bResult == FALSE)
{
printf("Error = %d",GetLastError()); // <- Error Point 3002 Error
}
LocalFree(JobInfo);
ClosePrinter(hPrinter);

Windows development Windows API - Win32
Windows for business Windows Server User experience Print jobs
Developer technologies C++
{count} votes

1 answer

Sort by: Most helpful
  1. Limitless Technology 39,916 Reputation points
    2021-08-12T08:03:00.917+00:00

    Hello,

    Thank you for your question and reaching out.

    Please Follow these steps :

    basic steps, the printing process involves:

    1-Search and Connect:
    Find and enumerate all visible devices (consider USB, BT and Wi-Fi connections)
    Identify the desired output device
    Connect/pair with the device
    2-Initialize:
    Check for printer readiness (connection, media, open panels, etc.)
    Initialize printer
    Configure parameters, invoke template, etc.
    3-Print:
    Send raw ZPL data, text, images, templates
    Check results and status

    If the reply was helpful, please don't forget to upvote or accept as answer.
    Thanks, Aradhya


Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.