Components for Developers & PDF Software → Virtual Printer C/C++ Tutorials

How to create Virtual Printer using C/C++

 

How to print files programmatically using the ShellExecute function

The code sample below demonstrates how to print files programmatically on either a physical or a virtual printer using the ShellExecute function. It also demonstrates how to change the default system printer.

void PrintDocumentUsingShellExecute (char *szPrinter, char * szDocumentPath)
{
    char szDefaultPrinter[MAX_PATH];
    DWORD bufferSize = MAX_PATH;

    // get the default printer 
    GetDefaultPrinter(szDefaultPrinter, &dwBufSize);

    //change the default printer
    if(strcmp(szPrinter, szDefaultPrinter) != 0)
    {
 	SetDefaultPrinter(szPrinter);
    }

    // send the document  to the print
    ShellExecute(NULL, "print", szDocumentPath, NULL, NULL, SW_HIDE); 

    // set default printer back to original
    if(strcmp(szPrinter, szDefaultPrinter) != 0)
    {
        SetDefaultPrinter(szDefaultPrinter);
    }
}

Then it's necessary to call these functions with the required parameters. For example, you can print MS Word and PDF documents this way:

PrintDocumentUsingShellExecute("Your Virtual Printer", 
						"c:\Documents\AnyDocument.doc")
PrintDocumentUsingShellExecute("Your Virtual Printer", 
						"c:\ Documents \AnyDocument.pdf")

Virtual Printer menu

Latest news

April 27 2010
Now we have a new service: the development of POS-printer.

Send us your comments

We're really eager to hear from you. Please send us your suggestions in one of two ways: