Solved

Printing with Win32 api

Posted on 1997-10-10
3
474 Views
Last Modified: 2006-11-17
     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
Comment
Question by:rabit
  • 2
3 Comments
 
LVL 3

Accepted Solution

by:
gaohong earned 120 total points
ID: 1171098
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
 

Author Comment

by:rabit
ID: 1171099
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
 

Author Comment

by:rabit
ID: 1171100
Thanks!!!
0

Featured Post

Enterprise Mobility and BYOD For Dummies

Like “For Dummies” books, you can read this in whatever order you choose and learn about mobility and BYOD; and how to put a competitive mobile infrastructure in place. Developed for SMBs and large enterprises alike, you will find helpful use cases, planning, and implementation.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
C++ assignment question 7 199
Lambda for random numbers problem 7 113
C++ :Change value from  DisableCMD registry 4 60
object oriented programming comparison 5 72
Often, when implementing a feature, you won't know how certain events should be handled at the point where they occur and you'd rather defer to the user of your function or class. For example, a XML parser will extract a tag from the source code, wh…
IntroductionThis article is the second in a three part article series on the Visual Studio 2008 Debugger.  It provides tips in setting and using breakpoints. If not familiar with this debugger, you can find a basic introduction in the EE article loc…
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…

773 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question