| 1 | /* $Id: initcomdlg32.cpp,v 1.5 2003-04-08 12:43:29 sandervl Exp $ */
|
|---|
| 2 | /*
|
|---|
| 3 | * DLL entry point
|
|---|
| 4 | *
|
|---|
| 5 | * Copyright 1998 Sander van Leeuwen
|
|---|
| 6 | * Copyright 1998 Peter Fitzsimmons
|
|---|
| 7 | *
|
|---|
| 8 | *
|
|---|
| 9 | * Project Odin Software License can be found in LICENSE.TXT
|
|---|
| 10 | *
|
|---|
| 11 | */
|
|---|
| 12 |
|
|---|
| 13 | /*-------------------------------------------------------------*/
|
|---|
| 14 | /* INITERM.C -- Source for a custom dynamic link library */
|
|---|
| 15 | /* initialization and termination (_DLL_InitTerm) */
|
|---|
| 16 | /* function. */
|
|---|
| 17 | /* */
|
|---|
| 18 | /* When called to perform initialization, this sample function */
|
|---|
| 19 | /* gets storage for an array of integers, and initializes its */
|
|---|
| 20 | /* elements with random integers. At termination time, it */
|
|---|
| 21 | /* frees the array. Substitute your own special processing. */
|
|---|
| 22 | /*-------------------------------------------------------------*/
|
|---|
| 23 |
|
|---|
| 24 |
|
|---|
| 25 | /* Include files */
|
|---|
| 26 | #define INCL_DOSMODULEMGR
|
|---|
| 27 | #define INCL_DOSPROCESS
|
|---|
| 28 | #define INCL_WINSHELLDATA
|
|---|
| 29 | #include <os2wrap.h> //Odin32 OS/2 api wrappers
|
|---|
| 30 | #include <stdlib.h>
|
|---|
| 31 | #include <stdio.h>
|
|---|
| 32 | #include <string.h>
|
|---|
| 33 | #include <odin.h>
|
|---|
| 34 | #include <win32type.h>
|
|---|
| 35 | #include <win32api.h>
|
|---|
| 36 | #include <winconst.h>
|
|---|
| 37 | #include <odinlx.h>
|
|---|
| 38 | #include <misc.h> /*PLF Wed 98-03-18 23:18:15*/
|
|---|
| 39 | #include <initdll.h>
|
|---|
| 40 |
|
|---|
| 41 | extern "C" {
|
|---|
| 42 | //Win32 resource table (produced by wrc)
|
|---|
| 43 | extern DWORD comdlg32_PEResTab;
|
|---|
| 44 |
|
|---|
| 45 | BOOL WINAPI COMDLG32_DllEntryPoint(HINSTANCE hInstance, DWORD Reason, LPVOID Reserved);
|
|---|
| 46 | }
|
|---|
| 47 |
|
|---|
| 48 | static HMODULE dllHandle = 0;
|
|---|
| 49 |
|
|---|
| 50 | //******************************************************************************
|
|---|
| 51 | //******************************************************************************
|
|---|
| 52 | BOOL WINAPI LibMainComdlg32(HINSTANCE hinstDLL, DWORD fdwReason, LPVOID fImpLoad)
|
|---|
| 53 | {
|
|---|
| 54 | switch (fdwReason)
|
|---|
| 55 | {
|
|---|
| 56 | case DLL_PROCESS_ATTACH:
|
|---|
| 57 | {
|
|---|
| 58 | /*
|
|---|
| 59 | * Find default printer and write name to win.ini
|
|---|
| 60 | * Format: queuename,driver,portname
|
|---|
| 61 | */
|
|---|
| 62 | char szWinDefPrn[256];
|
|---|
| 63 | szWinDefPrn[0] = '\0';
|
|---|
| 64 |
|
|---|
| 65 | /*
|
|---|
| 66 | * OS/2 stores the default printer as a combination of printerdevice and queue.
|
|---|
| 67 | *
|
|---|
| 68 | * Printer Device is related to one port. It may have multiple printer
|
|---|
| 69 | * drivers because the port may serve multiple queues with different drivers.
|
|---|
| 70 | * The Ports are related to multiple queues.
|
|---|
| 71 | *
|
|---|
| 72 | * So we take the default default printer+queue combination and finds the
|
|---|
| 73 | */
|
|---|
| 74 | char szDefPrnDev[20];
|
|---|
| 75 | char szDefPrnQue[20];
|
|---|
| 76 | if (PrfQueryProfileString(HINI_PROFILE, "PM_SPOOLER", "PRINTER", "",
|
|---|
| 77 | szDefPrnDev, sizeof(szDefPrnDev)) > 1
|
|---|
| 78 | && PrfQueryProfileString(HINI_PROFILE, "PM_SPOOLER", "QUEUE", "",
|
|---|
| 79 | szDefPrnQue, sizeof(szDefPrnQue)) > 1
|
|---|
| 80 | && szDefPrnDev[0]
|
|---|
| 81 | && szDefPrnQue[0]
|
|---|
| 82 | )
|
|---|
| 83 | {
|
|---|
| 84 | char *psz;
|
|---|
| 85 | /* remove everything beyond the first ';' */
|
|---|
| 86 | if ((psz = strchr(szDefPrnDev, ';')) != NULL)
|
|---|
| 87 | *psz = '\0';
|
|---|
| 88 | if ((psz = strchr(szDefPrnQue, ';')) != NULL)
|
|---|
| 89 | *psz = '\0';
|
|---|
| 90 |
|
|---|
| 91 | /*
|
|---|
| 92 | * Now we must lookup the port name from the device settings.
|
|---|
| 93 | * This is a string of this form:
|
|---|
| 94 | * <port>;<driver1>[,<driver2>;<queue1>[,<queue2>];?;?;
|
|---|
| 95 | */
|
|---|
| 96 | ULONG cb = 0;
|
|---|
| 97 | if (PrfQueryProfileSize(HINI_SYSTEMPROFILE, "PM_SPOOLER_PRINTER", szDefPrnDev, &cb)
|
|---|
| 98 | && cb > 0)
|
|---|
| 99 | {
|
|---|
| 100 | char *pszBufD = (char*)malloc(cb + 1);
|
|---|
| 101 | if (pszBufD
|
|---|
| 102 | && PrfQueryProfileString(HINI_SYSTEMPROFILE, "PM_SPOOLER_PRINTER", szDefPrnDev,
|
|---|
| 103 | NULL, pszBufD, cb + 1)
|
|---|
| 104 | > 1
|
|---|
| 105 | )
|
|---|
| 106 | {
|
|---|
| 107 | /*
|
|---|
| 108 | * Now get the Default printer driver for the queue.
|
|---|
| 109 | * This is stored as a ';' separated list of drivers, the first one is the default.
|
|---|
| 110 | */
|
|---|
| 111 | if (PrfQueryProfileSize(HINI_SYSTEMPROFILE, "PM_SPOOLER_QUEUE_DD", szDefPrnQue, &cb)
|
|---|
| 112 | && cb > 0)
|
|---|
| 113 | {
|
|---|
| 114 | char *pszBufQ = (char*)malloc(cb + 1);
|
|---|
| 115 | if (pszBufQ
|
|---|
| 116 | && PrfQueryProfileString(HINI_SYSTEMPROFILE, "PM_SPOOLER_QUEUE_DD", szDefPrnQue,
|
|---|
| 117 | NULL, pszBufQ, cb + 1)
|
|---|
| 118 | > 1
|
|---|
| 119 | )
|
|---|
| 120 | {
|
|---|
| 121 | /*
|
|---|
| 122 | * We got everything now. just find the parts we need.
|
|---|
| 123 | * First printer driver from QUEUE_DD
|
|---|
| 124 | * Port name of the device.
|
|---|
| 125 | */
|
|---|
| 126 | if ((psz = strchr(pszBufQ, ';')) != NULL)
|
|---|
| 127 | *psz = '\0';
|
|---|
| 128 | if ((psz = strchr(pszBufQ, ',')) != NULL) //paranoia! in case comman separated list of some kind.
|
|---|
| 129 | *psz = '\0';
|
|---|
| 130 | if ((psz = strchr(pszBufD, ';')) != NULL)
|
|---|
| 131 | *psz = '\0';
|
|---|
| 132 | if ((psz = strchr(pszBufD, ',')) != NULL) //paranoia in case comman separated list of some kind.
|
|---|
| 133 | *psz = '\0';
|
|---|
| 134 |
|
|---|
| 135 | /*
|
|---|
| 136 | * Now make default printer string for the win.ini.
|
|---|
| 137 | */
|
|---|
| 138 | strcpy(szWinDefPrn, szDefPrnQue);
|
|---|
| 139 | strcat(strcat(szWinDefPrn, ","), pszBufQ);
|
|---|
| 140 | strcat(strcat(szWinDefPrn, ","), pszBufD);
|
|---|
| 141 | dprintf(("LibMainComdlg32: Successfully found default printer.'%s'", szWinDefPrn));
|
|---|
| 142 | free(pszBufQ);
|
|---|
| 143 | }
|
|---|
| 144 | }
|
|---|
| 145 | else
|
|---|
| 146 | {
|
|---|
| 147 | /* OS/2 the device may exist though the default queue is destroyed.
|
|---|
| 148 | * it may still exist even if there are no queues on the system at all!
|
|---|
| 149 | */
|
|---|
| 150 | dprintf(("LibMainComdlg32: no queue driver entry for '%s'.", szDefPrnQue));
|
|---|
| 151 | }
|
|---|
| 152 |
|
|---|
| 153 | free(pszBufD);
|
|---|
| 154 | }
|
|---|
| 155 | }
|
|---|
| 156 | else
|
|---|
| 157 | {
|
|---|
| 158 | /* OS/2 doesn't remove the default settings if the default queue/printer is deleted. */
|
|---|
| 159 | dprintf(("LibMainComdlg32: can't find device settings for '%s'.", szDefPrnDev));
|
|---|
| 160 | }
|
|---|
| 161 | }
|
|---|
| 162 | else
|
|---|
| 163 | {
|
|---|
| 164 | dprintf(("LibMainComdlg32: no default printer? szDefPrnDev='%s' szDefPrnQue='%s'.", szDefPrnDev, szDefPrnQue));
|
|---|
| 165 | }
|
|---|
| 166 |
|
|---|
| 167 | //Now get real printer name the one that will be used in DC calls
|
|---|
| 168 | WriteProfileStringA("windows", "device", szWinDefPrn);
|
|---|
| 169 |
|
|---|
| 170 | return COMDLG32_DllEntryPoint(hinstDLL, fdwReason, fImpLoad);
|
|---|
| 171 | }
|
|---|
| 172 |
|
|---|
| 173 | case DLL_THREAD_ATTACH:
|
|---|
| 174 | case DLL_THREAD_DETACH:
|
|---|
| 175 | case DLL_PROCESS_DETACH:
|
|---|
| 176 | COMDLG32_DllEntryPoint(hinstDLL, fdwReason, fImpLoad);
|
|---|
| 177 | return TRUE;
|
|---|
| 178 | }
|
|---|
| 179 | return FALSE;
|
|---|
| 180 | }
|
|---|
| 181 | /****************************************************************************/
|
|---|
| 182 | /* _DLL_InitTerm is the function that gets called by the operating system */
|
|---|
| 183 | /* loader when it loads and frees this DLL for each process that accesses */
|
|---|
| 184 | /* this DLL. However, it only gets called the first time the DLL is loaded */
|
|---|
| 185 | /* and the last time it is freed for a particular process. The system */
|
|---|
| 186 | /* linkage convention MUST be used because the operating system loader is */
|
|---|
| 187 | /* calling this function. */
|
|---|
| 188 | /****************************************************************************/
|
|---|
| 189 | ULONG APIENTRY inittermComdlg32(ULONG hModule, ULONG ulFlag)
|
|---|
| 190 | {
|
|---|
| 191 | size_t i;
|
|---|
| 192 | APIRET rc;
|
|---|
| 193 |
|
|---|
| 194 | /*-------------------------------------------------------------------------*/
|
|---|
| 195 | /* If ulFlag is zero then the DLL is being loaded so initialization should */
|
|---|
| 196 | /* be performed. If ulFlag is 1 then the DLL is being freed so */
|
|---|
| 197 | /* termination should be performed. */
|
|---|
| 198 | /*-------------------------------------------------------------------------*/
|
|---|
| 199 |
|
|---|
| 200 | switch (ulFlag) {
|
|---|
| 201 | case 0 :
|
|---|
| 202 | dllHandle = RegisterLxDll(hModule, LibMainComdlg32, (PVOID)&comdlg32_PEResTab,
|
|---|
| 203 | COMDLG32_MAJORIMAGE_VERSION, COMDLG32_MINORIMAGE_VERSION,
|
|---|
| 204 | IMAGE_SUBSYSTEM_WINDOWS_GUI);
|
|---|
| 205 | if(dllHandle == 0)
|
|---|
| 206 | return 0UL;
|
|---|
| 207 |
|
|---|
| 208 | break;
|
|---|
| 209 | case 1 :
|
|---|
| 210 | if(dllHandle) {
|
|---|
| 211 | UnregisterLxDll(dllHandle);
|
|---|
| 212 | }
|
|---|
| 213 | break;
|
|---|
| 214 | default :
|
|---|
| 215 | return 0UL;
|
|---|
| 216 | }
|
|---|
| 217 |
|
|---|
| 218 | /***********************************************************/
|
|---|
| 219 | /* A non-zero value must be returned to indicate success. */
|
|---|
| 220 | /***********************************************************/
|
|---|
| 221 | return 1UL;
|
|---|
| 222 | }
|
|---|