Search code examples
c++mfcgdi

GDI Print API StartDoc function is giving inconsistent results


In my application a user can have multiple documents open and print all documents to PDF's using a "Microsoft print to pdf" option when they are prompted at the print dialog window, after which they choose the destination folder. Now what happens is that if there are 30 documents selected for print, an inconsistent amount are successfully printed to pdf in the desired folder each time it is run. Sometimes all are successful and other times not. I found that files with unicode character names like this "敥敥敥敥敥敥敥敥" where being created in the same directory as the file of code that contains this print to pdf process. I can rename these files with a ".pdf" extension and they are working pdf documents.

Here is the part of code concerned:

DOCINFO docInfo;

memset(&docInfo, 0, sizeof(docInfo));
docInfo.cbSize = sizeof(docInfo);
docInfo.lpszDocName = csPlanoName;
docInfo.lpszOutput = csOutputDir + csPlanoName + csExtension;

if(dc.StartDoc(&docInfo) > 0) {

    //printing process continues
}

I found that by stepping through the code, the StartDoc function call would change docInfo.lpszOutput to the same unicode characters "敥敥敥敥敥敥敥敥". This would not happen all the time, and does not happen with specific files when testing. One test a document will print to pdf successfully, another test with the same document and it will create the file with the name "敥敥敥敥敥敥敥敥".

Any help with this would be greatly appreciated.

Regards, Chris


Solution

  • using a string conversion macro seems to have solved my problem, successfully tested outcome 10+ times.

    CString csFullpath = csOutputDir + csPlanoName + csExtension;
    docInfo.lpszOutput = CT2W(csFullpath);