[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 495
  • Last Modified:

Printing with Win32 api

     prn_fh = CreateFile("Trouble.prn", GENERIC_WRITE, FILE_SHARE_WRITE, NULL ,
            OPEN_ALWAYS  , FILE_ATTRIBUTE_SYSTEM, NULL);
      CloseHandle(prn_fh);
    // Fill in the structure with info about this "document"
     DocInfo.pDocName = "Trouble.prn";
     DocInfo.pOutputFile = NULL;
     DocInfo.pDatatype = "RAW";
     // Inform the spooler the document is beginning
     if( (job = StartDocPrinter( hpr, 1, (unsigned char*)&DocInfo )) == 0 )
     {
             stat = GetLastError ();
// stat == 32 sharing violation. What am I doing wrong???
Ps OpenPrinter ( in prevous section) doesn't return an error!
0
rabit
Asked:
rabit
  • 2
1 Solution
 
gaohongCommented:
I think you use conflicting attributes in CreateFile() call.
prn_fh = CreateFile("Trouble.prn", GENERIC_WRITE,
                 0, /*  FILE_SHARE_WRITE, */  NULL ,
 OPEN_ALWAYS , /* FILE_ATTRIBUTE_SYSTEM, */ FILE_ATTRIBUTE_NORMAL, NULL);

stat = GetLastError ();   //call GetLastError here
//here check your returned handle
 
If the function succeeds, the return value is an open handle of the specified file. If the specified file exists before the function call and dwCreationDistribution is CREATE_ALWAYS or OPEN_ALWAYS, a call to GetLastError returns ERROR_ALREADY_EXISTS (even though the function has succeeded). If the file does not exist before the call, GetLastError returns zero.
If the function fails, the return value is INVALID_HANDLE_VALUE.

CloseHandle(prn_fh);    //after GetLastError()

Check out help page to make sure arguments are what you wanted

good luck
gaohong
0
 
rabitAuthor Commented:
I am creating the file ok! was able to write to file in preporation to printing it! changed from FILE_ATTRIBUTE_NORMAL to FILE_ATTRIBUTE_SYSTEM in desperation (I will however try your suggestion). Notice I closed the ^%#$ thing before geting a sharing violation. stepping thru the debugger shows a valid file handle, my problem is the printing of
a file or writing to the printer. Somehow, I've got to get a list of( database) errors to a network printer! P.S. NT 4.0 server
MSVC 4?

0
 
rabitAuthor Commented:
Thanks!!!
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now