Programming with the LEADTOOLS Network Virtual Printer Server Application

Take the following steps to create and run a program that uses the LEADTOOLS Network Virtual Printer Driver.

  1. Start Visual Studio 2008.

  2. From the Main Menu, choose File->New->Project...

  3. In the New Project dialog box, choose Visual C++ in the Projects Types, and choose MFC Application in the Templates.

  4. Type the project name as LEADTOOLS Printer Server Demo in the Project Name field.

  5. In the Location field, use the Browse button to navigate to the Examples subdirectory (such as C:\LEADTOOLS22\Examples\VirtualPrinter\CDLL). Clear both the Create Directory For Solution and the Add To Source check boxes. Click OK.

  6. Click Next> in the MFC Application Wizard.

  7. Select the Dialog-based Application type. Then click Finish. Three folders will be created, entitled: "Header Files", "Resource Files", and "Source Files".

  8. From the Main Menu, choose Project->Properties.

  9. In the Properties dialog box, choose Configuration Properties then select C/C++ then select Preprocessor.

  10. In right side set Preprocessor definitions property to use LTVXX_CONFIG then click OK.

  11. In Solution Explorer, open stdafx.h, and add the following code to the end of the file:

    #include "..\..\..\Include\Ltprinter.h" 

  12. In the Source Files, right-click the folder. Choose Add->New Item. Choose Code in Categories, and C++ File(.cpp) in Templates. Type Imports in the name field and click Add.

  13. Right-click Imports.cpp and click Open. Add the following code:

    #if defined(WIN64) 
    #pragma comment(lib, "..\\..\\..\\Lib"L_VER_DESIGNATOR"\\cdll\\x64\\Ltkrn_x.lib") 
    #pragma comment(lib, "..\\..\\..\\Lib"L_VER_DESIGNATOR"\\cdll\\x64\\Ltprinter_x.lib") 
    #pragma comment(lib, "..\\..\\..\\Lib"L_VER_DESIGNATOR"\\cdll\\x64\\LtDocWrt_x.lib") 
    #pragma comment(lib, "..\\..\\..\\Lib"L_VER_DESIGNATOR"\\cdll\\x64\\Ltdlgfile_x.lib") 
    #pragma comment(lib, "..\\..\\..\\Lib"L_VER_DESIGNATOR"\\cdll\\x64\\Ltdlgkrn_x.lib") 
    #pragma comment(lib, "..\\..\\..\\Lib"L_VER_DESIGNATOR"\\cdll\\x64\\Ltfil_x.lib") 
    #pragma comment(lib, "..\\..\\..\\Lib"L_VER_DESIGNATOR"\\cdll\\x64\\Ltdis_x.lib") 
    #else 
    #pragma comment(lib, "..\\..\\..\\Lib"L_VER_DESIGNATOR"\\cdll\\Win32\\Ltkrn_u.lib") 
    #pragma comment(lib, "..\\..\\..\\Lib"L_VER_DESIGNATOR"\\cdll\\win32\\Ltprinter_u.lib") 
    #pragma comment(lib, "..\\..\\..\\Lib"L_VER_DESIGNATOR"\\cdll\\win32\\LtDocWrt_u.lib") 
    #pragma comment(lib, "..\\..\\..\\Lib"L_VER_DESIGNATOR"\\cdll\\win32\\Ltdlgfile_u.lib") 
    #pragma comment(lib, "..\\..\\..\\Lib"L_VER_DESIGNATOR"\\cdll\\win32\\Ltdlgkrn_u.lib") 
    #pragma comment(lib, "..\\..\\..\\Lib"L_VER_DESIGNATOR"\\cdll\\win32\\Ltfil_u.lib") 
    #pragma comment(lib, "..\\..\\..\\Lib"L_VER_DESIGNATOR"\\cdll\\win32\\Ltdis_u.lib") 
    #endif  // #if defined(WIN64) 

  14. Right-click LEADTOOLS Printer Server DemoDlg.h and click Open. Add the following functions definitions to the CLEADTOOLSPrinterServerDemoDlg class:

    afx_msg void OnBnClickedOk(); 
    static L_INT (EXT_CALLBACK OnEmfRgsProc)( L_WCHAR*, HGLOBAL, L_UINT, L_VOID* ); 
    static L_INT (EXT_CALLBACK OnJobInfoProc)( L_WCHAR*, DWORD, DWORD, L_VOID* ); 
    L_VOID RegisterJobCallBack(); 
    L_VOID RegisterEmfCallBack(); 
    L_INT InstallPrinter(); 

  15. right-click LEADTOOLS Printer Server DemoDlg.cpp and click Open. Add the following function:

    //Locate BEGIN_MESSAGE_MAP(CLEADTOOLSPrinterServerDemoDlg, CDialog) and add the functions 
    ON_BN_CLICKED(IDOK, &CLEADTOOLSPrinterServerDemoDlg::OnBnClickedOk); 
    //Add the following functions implementations 
    void CLEADTOOLSPrinterServerDemoDlg::OnBnClickedOk() 
    { 
       InstallPrinter(); 
       RegisterEmfCallBack(); 
       RegisterJobCallBack(); 
    } 
    L_INT CLEADTOOLSPrinterServerDemoDlg::InstallPrinter() 
    { 
       PRNPRINTERINFO docPrinterInfo; 
       memset(&docPrinterInfo, 0, sizeof(PRNPRINTERINFO)); 
       docPrinterInfo.uStructSize = sizeof(PRNPRINTERINFO); 
       docPrinterInfo.pszRootDir = TEXT("C:\\LEADTOOLS22\\Bin\\Common\\PrinterDriver"); 
       docPrinterInfo.pszPrinterName = TEXT("TestPrinter"); 
       docPrinterInfo.pszPortName = TEXT("TestPrinter"); 
       docPrinterInfo.pszMonitorName = TEXT("TestPrinter"); 
       docPrinterInfo.pszProductName = TEXT("TestPrinter"); 
       docPrinterInfo.pszRegistryKey = TEXT("SOFTWARE\\Printer\\TestPrinter"); 
       docPrinterInfo.pszUrl = TEXT("https://www.leadtools.com"); 
       docPrinterInfo.pszPassword = TEXT("123"); 
       docPrinterInfo.pszPrinterExe = TEXT("c:\\TestDemo.exe"); 
       docPrinterInfo.pszAboutString = TEXT("LEADTOOLSPrinter"); 
       docPrinterInfo.pszAboutIcon = TEXT("c:\\LEADTOOLSPrinter.ico"); 
       L_INT nRet = L_PrnInstallPrinter(&docPrinterInfo, 0); 
       return nRet; 
    } 
    void CLEADTOOLSPrinterServerDemoDlg::RegisterEmfCallBack() 
    { 
       L_PrnRegisterEMFCallback(TEXT("TestPrinter"), OnEmfRgsProc, this); 
    } 
    void CLEADTOOLSPrinterServerDemoDlg::RegisterJobCallBack() 
    { 
       L_PrnRegisterJobCallback(TEXT("TestPrinter"), OnJobInfoProc, this); 
    } 
    L_INT CLEADTOOLSPrinterServerDemoDlg::OnEmfRgsProc(L_WCHAR * /*pszPrinter*/, 
    HGLOBAL hMem, 
    L_UINT  uSize, 
    L_VOID * pData) 
    { 
       static L_INT nCounter = 0; 
       HANDLE hFile = INVALID_HANDLE_VALUE; 
       DWORD     uSizeWritten = 0; 
       L_UCHAR * pEmfData = (L_UCHAR*)GlobalLock(hMem); 
       CString szFileName = TEXT("c:\\1.emf"); 
       if (pEmfData) 
       { 
          hFile = CreateFile(szFileName, GENERIC_WRITE, FILE_SHARE_READ, 
          NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL); 
          if (hFile != INVALID_HANDLE_VALUE) 
          { 
             WriteFile(hFile, pEmfData, uSize, &uSizeWritten, NULL); 
             CloseHandle(hFile); 
          } 
          GlobalUnlock(hMem); 
       } 
       // Do not forget to free the mem. 
       GlobalFree(hMem); 
       return 1; 
    } 
    // Remember, you always need to free hMem. 
    L_INT  CLEADTOOLSPrinterServerDemoDlg::OnJobInfoProc(L_WCHAR * pszPrinter, DWORD dwJobID, 
    DWORD dwFlags, L_VOID * pData) 
    { 
       switch (dwFlags) 
       { 
          case PRN_JOB_START: 
          { 
             //Job was started 
             PRNJOBDATA jobData; 
             L_PrnGetRemoteData(pszPrinter, dwJobID, &jobData); 
             PCHAR pChar = (PCHAR)malloc(jobData.uUserDataSize + 1); 
             PVOID pVoid = GlobalLock(jobData.hUserData); 
             memcpy(pChar, pVoid, jobData.uUserDataSize); 
             GlobalUnlock(jobData.hUserData); 
             GlobalFree(jobData.hUserData); 
             pChar[jobData.uUserDataSize] = NULL; 
             CStringA str; 
             str.Format("Job received with message %s", pChar); 
             MessageBoxA(NULL, str.LockBuffer(), "Printer Server Demo", 0); 
             free(pChar); 
          } 
          break; 
          case PRN_JOB_END: 
          //Job was ended 
          break; 
       } 
       return 1; 
    } 

  16. Compile and run the code to test it. Click OK in the dialog to install a new printer. Then share the printer and connect to it from a client machine. Try to print on the printer that has just been created to receive EMF and Job callback events.

For more information, refer to:

Working with the LEADTOOLS Virtual Printer

See Also

Getting Started

Help Version 22.0.2022.12.7
Products | Support | Contact Us | Intellectual Property Notices
© 1991-2023 LEAD Technologies, Inc. All Rights Reserved.

LEADTOOLS Virtual Printer C API Help
Products | Support | Contact Us | Intellectual Property Notices
© 1991-2023 LEAD Technologies, Inc. All Rights Reserved.