1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141
|
/* Dia -- an diagram creation/manipulation program
* Copyright (C) 1998 Alexander Larsson
*
* win32print.c - output file directly to a windoze printer
* Don't try it with a *non* PostScript printer !
*
* Copyright (C) 2001 Hans Breuer
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/
#include <config.h>
#define WIN32_LEAN_AND_MEAN
#include <windows.h>
#include <winspool.h>
#include <stdlib.h>
#include <stdio.h>
#include <fcntl.h>
#include <io.h>
#include <glib.h>
#include "win32print.h"
static void
PrintError (const char* s, DWORD err)
{
if (0 != err)
{
char* lpBuffer;
/* get the Windows message */
FormatMessage (FORMAT_MESSAGE_ALLOCATE_BUFFER
| FORMAT_MESSAGE_FROM_SYSTEM
| FORMAT_MESSAGE_IGNORE_INSERTS,
NULL,
err,
MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
(char*)&lpBuffer,
0,NULL);
g_printerr ("%s : %s", s, lpBuffer);
LocalFree (lpBuffer);
}
}
const char*
win32_printer_default (void)
{
static char sName[1024];
GetProfileString("windows", "device", "", sName, sizeof(sName));
if (strchr (sName, ','))
*strchr (sName, ',') = 0;
else if ( strlen (sName) < 1)
strcpy (sName, "unknown");
return sName;
}
static HANDLE hPrinter = NULL;
static ADDJOB_INFO_1* pJob = NULL;
static HANDLE hFile = INVALID_HANDLE_VALUE;
FILE*
win32_printer_open (char* sName)
{
BYTE data [_MAX_PATH*2];
DWORD dwID = 0;
DWORD dwSizeRequired=0;
int err = 0;
if (!OpenPrinter (sName,
&hPrinter,
NULL))
{
g_printerr ("Failed to open printer : %s\n", sName);
return NULL;
}
if (!AddJob (hPrinter, 1, data, sizeof(data), &dwSizeRequired))
{
PrintError ("Failed to add job", GetLastError());
}
else
{
pJob = (ADDJOB_INFO_1*)data;
hFile = CreateFile (pJob->Path,
GENERIC_WRITE,
0, /* no share */
NULL, /* default security */
OPEN_ALWAYS,
FILE_ATTRIBUTE_NORMAL,
NULL); /* template */
if (INVALID_HANDLE_VALUE == hFile)
{
PrintError ("Failed to CreateFile", GetLastError());
}
else
{
int hnd = _open_osfhandle ((long)hFile, _O_APPEND);
if (-1 != hnd)
return _fdopen (hnd, "wb");
else
PrintError ("Failed to _open_osfhandle", 0);
}
}
return NULL;
}
int
win32_printer_close (FILE* f)
{
int ret = 0;
fflush (f);
if (!pJob || !ScheduleJob (hPrinter, pJob->JobId))
{
PrintError ("Failed to schedule job", GetLastError());
ret = GetLastError();
}
if (f)
fclose (f);
CloseHandle (hFile);
ClosePrinter (hPrinter);
return ret;
}
|