Hi

I am using a function InitDocStruct() to initialize the document I want to print through my program.

Definition of the function is

void InitDocStruct( DOCINFO* di, char* docname)
   {
       // Always zero it before using it.
       memset( di, 0, sizeof( DOCINFO ) );
       // Fill in the required members.
       di->cbSize = sizeof( DOCINFO );
       di->lpszDocName = docname;
   }

I am calling the function from main() InitDocStruct( &di, "c:\\YServer.txt"); After I run the program a blank page is printed.

Am I not initializing the document properly.

Kindly advice

Regards
Karan

The InitDocStruct() function seems OK to me. So the problem probably is in the actual printing code you have.

Here is the code

#include <windows.h>
#include <iostream>
#include <limits>
#include <string>
#include <windows.h>
#include <conio.h>
#include <wingdi.h>

using std::string;

void DrawStuff(HDC);
void InitDocStruct( DOCINFO*, char*);
BOOL CALLBACK AbortProc( HDC, int );
HDC GetPrinterDC(void);
void PrintStuff();

int main()
{
    PrintStuff();

     getch();
     return 0;
}
  
  
//void PrintStuff( HWND hWndParent )
void PrintStuff()
{
 
   HDC        hDC;
   DOCINFO    di;

   // Need a printer DC to print to.
   hDC = GetPrinterDC();

   // Did you get a good DC?
   if( !hDC)
   {
       MessageBox(NULL, "Error creating DC", "Error",
                                   MB_APPLMODAL | MB_OK );
       return;
   }

   // You always have to use an AbortProc().
   if( SetAbortProc( hDC, AbortProc ) == SP_ERROR )
   {
       MessageBox( NULL, "Error setting up AbortProc",
                                   "Error", MB_APPLMODAL | MB_OK);
       return;
   }

   // Init the DOCINFO and start the document.
//   InitDocStruct( &di, "MyDoc");
   InitDocStruct( &di, "c:\\YServer.txt");
   
   StartDoc( hDC, &di );

   // Print one page.
   StartPage( hDC );
   DrawStuff( hDC );
   EndPage( hDC );

   // Indicate end of document.
   EndDoc( hDC );

   // Clean up
   DeleteDC( hDC );
   

   
}

/*===============================*/ 
   /* Obtain printer device context */ 
   /* ==============================*/ 
   HDC GetPrinterDC(void)
   {
       PRINTDLG pdlg;

       // Initialize the PRINTDLG structure.
       memset( &pdlg, 0, sizeof( PRINTDLG ) );
       pdlg.lStructSize = sizeof( PRINTDLG );
       // Set the flag to return printer DC.
       pdlg.Flags = PD_RETURNDEFAULT | PD_RETURNDC;

       // Invoke the printer dialog box.
       PrintDlg( &pdlg );
       // hDC member of the PRINTDLG structure contains
       // the printer DC.
       return pdlg.hDC;
   }


/*===============================*/ 
   /* The Abort Procudure           */ 
   /* ==============================*/ 
   BOOL CALLBACK AbortProc( HDC hDC, int Error )
   {
       MSG   msg;
       while( PeekMessage( &msg, NULL, 0, 0, PM_REMOVE ) )
       {
           TranslateMessage( &msg );
           DispatchMessage( &msg );
       }
       return TRUE;
   }

   /*===============================*/ 
   /* Initialize DOCINFO structure  */ 
   /* ==============================*/ 
   void InitDocStruct( DOCINFO* di, char* docname)
   {
       // Always zero it before using it.
       memset( di, 0, sizeof( DOCINFO ) );
       // Fill in the required members.
       di->cbSize = sizeof( DOCINFO );
       di->lpszDocName = docname;
   }

   /*===============================*/ 
   /* Drawing on the DC             */ 
   /* ==============================*/ 
   void DrawStuff( HDC hdc)
   {
       // This is the function that does draws on a given DC.
       // You are printing text here.
       TextOut(hdc, 0,0, "Test Printing", lstrlen( "Test Printing" ) );
   }

Regards
Karan

I'd say it should work as such.

So, the next step might be adding more error checking wherever applicable. I.e. verify that StartDoc/StartPage/EndDoc/EndPage return a value greater than zero, if not, use GetLastError() to get the error code. Also check that TextOut() is not returning FALSE .


[EDIT]
Since you seem to have trouble with the code tags, read http://www.daniweb.com/forums/misc-explaincode.html

Well, you get this code with copy/paste from Miscrosoft support site:
http://support.microsoft.com/kb/139652
There is feedback form on this page - use it.
I think, it's not so interesting work to debug Microsoft official code examples on the forum...

Well, you get this code with copy/paste from Miscrosoft support site:
http://support.microsoft.com/kb/139652

So it seems to be ...

I think, it's not so interesting work to debug Microsoft official code examples on the forum...

I would actually like to know what goes wrong with that piece of code, (especially considering it is a simple MSDN example)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.