NO

Author Topic: Printing to a network printer  (Read 3467 times)

tpekar

  • Guest
Printing to a network printer
« on: April 30, 2014, 04:19:30 PM »
Has anyone tried printing directly to a network printer from a Pelles C Windows program?  Was not sure what Windows functions to use.  Have done it in the past using lpr.  But don't think I have that available to me now.

Offline TimoVJL

  • Global Moderator
  • Member
  • *****
  • Posts: 2091
Re: Printing to a network printer
« Reply #1 on: April 30, 2014, 05:43:42 PM »
Do you mean to Print Server like HP Jet Direct ?
Winsock is your friend.
May the source be with you

Offline jj2007

  • Member
  • *
  • Posts: 536
Re: Printing to a network printer
« Reply #2 on: April 30, 2014, 05:59:33 PM »
Googling for PageSetupDlg StartDoc EndDoc CreateDC also yields some useful links.

Offline DMac

  • Member
  • *
  • Posts: 272
Re: Printing to a network printer
« Reply #3 on: April 30, 2014, 07:35:51 PM »
Here's something I pulled out of a project from long ago.  In that application I was sending a bitmap to the printer.  It is an example showing  how the methods jj2007 mentioned work together.  Feel free to adapt it to your purposes if you find it helpful. :)

Global variables
Code: [Select]
static HINSTANCE ghInstance;
static PAGESETUPDLG m_psd;

Various methods
Code: [Select]
/ * Purpose: DoEvents is a statement that yields execution of the current
  * thread so that the operating system can process other events.
  * This function cleans out the message loop and executes any other pending
  * business.
  */
void DoEvents (void)
{
MSG Msg;
while (PeekMessage(&Msg,NULL,0,0,PM_REMOVE))
{
TranslateMessage(&Msg);
DispatchMessage(&Msg);
}
}

void InitPrintDevMode (void)
{
DEVMODE * pDevMode;

m_hDevMode = GlobalAlloc(GHND, sizeof(DEVMODE)); // allocate a moveable block of memory and initialize it to zeroes.
if (m_hDevMode)
{
pDevMode = (DEVMODE *) GlobalLock(m_hDevMode); // lock the memory and return a pointer to it
if (pDevMode)
{
pDevMode->dmSize = sizeof(DEVMODE); // set the size of the DEVMODE structure
pDevMode->dmFields = DM_ORIENTATION; // tell Windows that I will be setting the dmOrientation field
pDevMode->dmOrientation = DMORIENT_LANDSCAPE; // set the orientation to landscape
}
GlobalUnlock(m_hDevMode); // unlock the memory for other functions to use this
}
// m_hDevMode is now ready to be used in a PAGESETUPDLG structure
}

// Show the Print dialog and Return a HDC for the selected printer.
HDC GetPrinterDC(HWND hwnd)
{
PRINTDLG pdlg;
memset( &pdlg, 0, sizeof( PRINTDLG ));
pdlg.lStructSize = sizeof( PRINTDLG );
pdlg.hDevMode = m_hDevMode;
pdlg.hDevNames = m_psd.hDevNames;
pdlg.hwndOwner = hwnd;
pdlg.Flags = PD_NOPAGENUMS | PD_NOSELECTION | PD_RETURNDC;
PrintDlg( &pdlg );
DoEvents();
return pdlg.hDC;
}

void mnuPrint_Click(HWND hParent, HWND hWnd)
{
HDC     hdcPrinter;
int     iWidth;
int     iHeight;
DOCINFO di;
DIBSECTION ds;

// Do you have a valid window?
if (!IsWindow(hWnd)) return;

// Get HDC for the selected printer.
hdcPrinter = GetPrinterDC(hParent);
if (!hdcPrinter) return;

// Prepare the DOCINFO.
ZeroMemory(&di, sizeof(di));
di.cbSize = sizeof(di);
di.lpszDocName = "Memory Bitmap";

// Initialize the print job.
if (StartDoc(hdcPrinter, &di) > 0)
{

// Prepare to send a page.
if (StartPage(hdcPrinter) > 0)
{
// Retrieve the information describing the surface.
if(Plotter.MemoryBMP !=NULL)
{
GetObject(Plotter.MemoryBMP, sizeof(ds), &ds);

    // Get the resolution of the printer device in square pixels
    iWidth  = GetDeviceCaps(hdcPrinter, HORZRES);
    iHeight = GetDeviceCaps(hdcPrinter, VERTRES);

    // Maintain aspect ratio of printed image
    if (iHeight > iWidth)
{
//
// Page is taller than it is wide (portrait mode)
//
// Scale the height to conform to the client area's size
//

iHeight = iWidth * ds.dsBm.bmHeight / ds.dsBm.bmWidth;
}

// Print the contents of the surface.
StretchDIBits(hdcPrinter,
m_psd.rtMargin.left,
m_psd.rtMargin.top,
iWidth - (m_psd.rtMargin.right+m_psd.rtMargin.left),
iHeight - (m_psd.rtMargin.bottom+m_psd.rtMargin.top),
0, 0, ds.dsBm.bmWidth, ds.dsBm.bmHeight,
ds.dsBm.bmBits,
(LPBITMAPINFO)&ds.dsBmih,
DIB_RGB_COLORS,
SRCCOPY);
}
// Let the driver know the page is done.
EndPage(hdcPrinter);
}
// Let the driver know the document is done.
EndDoc(hdcPrinter);
}
// Clean up the objects you created.
DeleteDC(hdcPrinter);
}

void mnuPageSetup_Click (HWND hwnd)
{
    //Set the structure size
    m_psd.lStructSize = sizeof(PAGESETUPDLG);
    //Set the owner window
    m_psd.hwndOwner = hwnd;
    //Set the application instance
    m_psd.hInstance = ghInstance;
//Set the hDevMode to customized settings
m_psd.hDevMode = m_hDevMode;
    //No extra flags
    m_psd.Flags = PSD_MARGINS;
    //Show the pagesetup dialog
    PageSetupDlg(&m_psd);
}

BOOL FormMain_OnInitDialog(HWND hwnd, HWND hwndFocus, LPARAM lParam)
{
InitPrintDevMode();
return 0;
}
No one cares how much you know,
until they know how much you care.

Offline TimoVJL

  • Global Moderator
  • Member
  • *****
  • Posts: 2091
Re: Printing to a network printer
« Reply #4 on: May 13, 2014, 10:49:45 AM »
Getting HDC for printer:
Code: [Select]
TCHAR szPrinter[255];
DWORD nLen;
if (GetDefaultPrinter( szPrinter, &nLen )) {
//HDC hDCPrn = CreateDC("WINSPOOL", szPrinter, NULL, NULL);
HDC hDCPrn = CreateDC(NULL, szPrinter, NULL, NULL);
//HDC hDCPrn = CreateDC(NULL, "\\\\foo-svr\\foo-printer", NULL, NULL);
...
}
May the source be with you