-
Notifications
You must be signed in to change notification settings - Fork 1
/
devNames.cpp
80 lines (65 loc) · 2.72 KB
/
devNames.cpp
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
/*
* $Header: /Book/devNames.cpp 13 3.06.02 10:44 Oslph312 $
*
* Consider including this, or some of it, in Document class?
* Would give document-specific printer selection.
*/
#include "precomp.h"
#include "persistence.h"
#include "devNames.h"
DEFINE_PERSISTENT_STRING( "Printer", Driver, "" );
DEFINE_PERSISTENT_STRING( "Printer", Device, "" );
DEFINE_PERSISTENT_STRING( "Printer", Port , "" );
HGLOBAL getDevNames( LPCTSTR pszPrinter, LPCTSTR pszDriver, LPCTSTR pszPort ) {
String strDriver = getDriver();
String strDevice = getDevice();
String strPort = getPort ();
if ( 0 != pszPrinter ) {
strDevice = pszPrinter;
}
if ( 0 != pszDriver ) {
strDriver = pszDriver;
}
if ( 0 != pszPort ) {
strPort = pszPort;
}
const int nChars = strDriver.length() + strDevice.length() + strPort.length();
if ( 0 == nChars ) {
return 0; // This is OK; will get default initialization.
}
const int charsIncludingNulls = nChars + 4;
const int bytesIncludingNulls = charsIncludingNulls * sizeof( TCHAR );
HGLOBAL hDevNames = GlobalAlloc( GHND, sizeof DEVNAMES + bytesIncludingNulls );
DEVNAMES *pDevNames = reinterpret_cast< DEVNAMES * >( GlobalLock( hDevNames ) );
if ( 0 == pDevNames ) {
return 0; // This is OK; will get default initialization.
}
#ifdef trace
trace( _T( "sizeof( DEVNAMES ) = %d\n" ), sizeof( DEVNAMES ) );
#endif
LPTSTR stringStart = reinterpret_cast< LPTSTR >( pDevNames ) + sizeof( DEVNAMES ) / sizeof( TCHAR );
LPTSTR psz = stringStart;
_tcscpy_s( psz, stringStart + charsIncludingNulls - psz, strDriver.c_str() );
pDevNames->wDriverOffset = psz - reinterpret_cast< LPTSTR >( pDevNames );
psz += _tcsclen( psz ) + 1;
_tcscpy_s( psz, stringStart + charsIncludingNulls - psz, strDevice.c_str() );
pDevNames->wDeviceOffset = psz - reinterpret_cast< LPTSTR >( pDevNames );
psz += _tcsclen( psz ) + 1;
_tcscpy_s( psz, stringStart + charsIncludingNulls - psz, strPort.c_str() );
pDevNames->wOutputOffset = psz - reinterpret_cast< LPTSTR >( pDevNames );
GlobalUnlock( hDevNames );
return hDevNames;
}
void setDevNames( HGLOBAL hDevNames ) {
if ( 0 != hDevNames ) {
DEVNAMES *pDevNames = reinterpret_cast< DEVNAMES * >( GlobalLock( hDevNames ) );
LPCTSTR psz = reinterpret_cast< LPCTSTR >( pDevNames ) + pDevNames->wDriverOffset;
setDriver( reinterpret_cast< LPCTSTR >( psz ) );
psz = reinterpret_cast< LPCTSTR >( pDevNames ) + pDevNames->wDeviceOffset;
setDevice( reinterpret_cast< LPCTSTR >( psz ) );
psz = reinterpret_cast< LPCTSTR >( pDevNames ) + pDevNames->wOutputOffset;
setPort( reinterpret_cast< LPCTSTR >( psz ) );
GlobalUnlock( hDevNames );
}
}
// end of file