A C++ example to convert Postscript files to PDF files simultaneously

This is a VC++ source code example, this example does call ps2pdf.exe in a multiple threads and convert more PS files to PDF files concurrently,

 

#include <stdio.h>

#include <tchar.h>

#include <windows.h>

 

DWORD WINAPI PS2PDF_Thread(LPVOID lpParameter)

{

    int threadNum = (int)lpParameter;

 

    printf("Thread %d started\n", threadNum);

 

    PROCESS_INFORMATION pi = {0};

    char commandLine[4096] = {0};

    char threadNumStr[16] = {0};

   

    STARTUPINFO StartupInfo = {0};

    StartupInfo.cb = sizeof(STARTUPINFO);

    StartupInfo.dwFlags = STARTF_USESHOWWINDOW;

    StartupInfo.wShowWindow = SW_HIDE;

 

    _itoa(threadNum, threadNumStr, 10);

 

    strcpy(commandLine, "ps2pdf.exe -debug");

    strcat(commandLine, " \"pdftest");

    strcat(commandLine, threadNumStr);

    strcat(commandLine, ".ps\"");

 

    printf("Thread %d process starting %S\n", threadNum, commandLine);

 

    BOOL cpSuccess = CreateProcess(NULL, commandLine, NULL, NULL, TRUE, NORMAL_PRIORITY_CLASS, NULL, NULL, &StartupInfo, &pi);

   

    if (cpSuccess == FALSE)

    {

        printf("Thread %d process failed to start\n", threadNum);

    }

 

    printf("Thread %d process started\n", threadNum);

 

    DWORD waitResult = WaitForSingleObject(pi.hProcess, INFINITE);

   

    printf("Thread %d process finished\n", threadNum);

 

    if (waitResult != WAIT_OBJECT_0)

    {

        printf("Thread %d process wait = %d\n", threadNum, waitResult);

    }

 

    if (pi.hProcess != NULL)

    {

        CloseHandle (pi.hProcess);

        pi.hProcess = NULL;

    }

 

    if (pi.hThread != NULL)

    {

        CloseHandle (pi.hThread);

        pi.hThread = NULL;

    }

 

    printf("Thread %d finished\n", threadNum);

 

    return 0;

}

 

int _tmain(int argc, _TCHAR* argv[])

{

    int numThreads = 10;

    HANDLE* hThread = NULL;

 

    printf("Running %d threads\n", numThreads);

 

    for (int i = 0; i < numThreads; i++)

    {

        wchar_t pdfFileName[32] = {0};

        wchar_t threadNumStr[16] = {0};

 

        _itow(0, threadNumStr, 10);

 

        wcscpy(pdfFileName, L"pdftest");

        wcscat(pdfFileName, threadNumStr);

        wcscat(pdfFileName, L".pdf");

 

        DeleteFileW(pdfFileName);

    }

 

    hThread = new HANDLE[numThreads];

 

    for (i = 0; i < numThreads; i++)

    {

        hThread[i] = ::CreateThread(NULL, 0, PS2PDF_Thread, (LPVOID)i, CREATE_SUSPENDED, NULL);

    }

 

    for (int j = 0; j < numThreads; j++)

    {

        ResumeThread(hThread[j]);

    }

 

    WaitForMultipleObjects(numThreads, hThread, TRUE, INFINITE);

 

    if (hThread != NULL)

    {

        delete []hThread;

        hThread = NULL;

    }

   

    printf("Press [Enter] to exit.\n");

    getchar();

 

    return 0;

}

 

 

VN:F [1.9.20_1166]
Rating: 8.0/10 (1 vote cast)
VN:F [1.9.20_1166]
Rating: -1 (from 1 vote)
A C++ example to convert Postscript files to PDF files simultaneously, 8.0 out of 10 based on 1 rating

Related Posts

Leave a Reply

Your email address will not be published. Required fields are marked *


Verify Code   If you cannot see the CheckCode image,please refresh the page again!