1 /* Dia -- an diagram creation/manipulation program
2 * Copyright (C) 1998 Alexander Larsson
4 * win32print.c - output file directly to a windoze printer
5 * Don't try it with a *non* PostScript printer !
7 * Copyright (C) 2001 Hans Breuer
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
37 #include "win32print.h"
40 PrintError (const char* s
, DWORD err
)
45 /* get the Windows message */
46 FormatMessage (FORMAT_MESSAGE_ALLOCATE_BUFFER
47 | FORMAT_MESSAGE_FROM_SYSTEM
48 | FORMAT_MESSAGE_IGNORE_INSERTS
,
51 MAKELANGID(LANG_NEUTRAL
, SUBLANG_DEFAULT
),
54 g_print ("%s : %s", s
, lpBuffer
);
60 win32_printer_default (void)
62 static char sName
[1024];
63 GetProfileString("windows", "device", "", sName
, sizeof(sName
));
64 if (strchr (sName
, ','))
65 *strchr (sName
, ',') = 0;
66 else if ( strlen (sName
) < 1)
67 strcpy (sName
, "unknown");
72 static HANDLE hPrinter
= NULL
;
73 static ADDJOB_INFO_1
* pJob
= NULL
;
74 static HANDLE hFile
= INVALID_HANDLE_VALUE
;
77 win32_printer_open (char* sName
)
79 BYTE data
[_MAX_PATH
*2];
81 DWORD dwSizeRequired
=0;
84 if (!OpenPrinter (sName
,
88 g_print ("Failed to open printer : %s\n", sName
);
92 if (!AddJob (hPrinter
, 1, data
, sizeof(data
), &dwSizeRequired
))
94 PrintError ("Failed to add job", GetLastError());
98 pJob
= (ADDJOB_INFO_1
*)data
;
99 hFile
= CreateFile (pJob
->Path
,
102 NULL
, /* default security */
104 FILE_ATTRIBUTE_NORMAL
,
105 NULL
); /* template */
106 if (INVALID_HANDLE_VALUE
== hFile
)
108 PrintError ("Failed to CreateFile", GetLastError());
112 int hnd
= _open_osfhandle ((long)hFile
, _O_APPEND
);
114 return _fdopen (hnd
, "wb");
116 PrintError ("Failed to _open_osfhandle", 0);
124 win32_printer_close (FILE* f
)
129 if (!pJob
|| !ScheduleJob (hPrinter
, pJob
->JobId
))
131 PrintError ("Failed to schedule job", GetLastError());
132 ret
= GetLastError();
138 ClosePrinter (hPrinter
);