Fixed crash of WinZip 8.0 due to invalid image list handle.
[wine.git] / windows / driver.c
blob2e9401101b4d69bd074fb3c5a0e42ccbd32eee5d
1 /* -*- tab-width: 8; c-basic-offset: 4 -*- */
3 /*
4 * WINE Drivers functions
6 * Copyright 1994 Martin Ayotte
7 * Copyright 1998 Marcus Meissner
8 * Copyright 1999 Eric Pouech
9 */
11 #include <string.h>
12 #include "heap.h"
13 #include "windef.h"
14 #include "wine/winbase16.h"
15 #include "wingdi.h"
16 #include "winuser.h"
17 #include "ldt.h"
18 #include "mmddk.h"
19 #include "debugtools.h"
21 DEFAULT_DEBUG_CHANNEL(driver);
23 /* The following definitions are WINE internals */
24 /* FIXME: This is a WINE internal struct and should be moved in include/wine directory
25 * Please note that WINE shares 16 and 32 bit drivers on a single list...
26 * Basically, we maintain an external double view on drivers, so that a 16 bit drivers
27 * can be loaded/used... by 32 functions transparently
30 /* Who said goofy boy ? */
31 #define WINE_DI_MAGIC 0x900F1B01
33 typedef struct tagWINE_DRIVER
35 DWORD dwMagic;
36 char szAliasName[128];
37 /* as usual LPWINE_DRIVER == hDriver32 */
38 HDRVR16 hDriver16;
39 union {
40 struct {
41 HMODULE16 hModule;
42 DRIVERPROC16 lpDrvProc;
43 } d16;
44 struct {
45 HMODULE hModule;
46 DRIVERPROC lpDrvProc;
47 } d32;
48 } d;
49 DWORD dwDriverID;
50 DWORD dwFlags;
51 struct tagWINE_DRIVER* lpPrevItem;
52 struct tagWINE_DRIVER* lpNextItem;
53 } WINE_DRIVER, *LPWINE_DRIVER;
55 LPWINE_DRIVER DRIVER_RegisterDriver16(LPCSTR, HMODULE16, DRIVERPROC16, LPARAM, BOOL);
56 LPWINE_DRIVER DRIVER_RegisterDriver32(LPCSTR, HMODULE, DRIVERPROC, LPARAM, BOOL);
58 static LPWINE_DRIVER lpDrvItemList = NULL;
60 /* TODO list :
61 * - LoadModule count and clean up is not handled correctly (it's not a
62 * problem as long as FreeLibrary is not working correctly)
63 * - shoudln't the allocations be done on a per process basis ?
64 * - split 16/32 bit functions between DLLs as windows do (16 bit in USER, 32 bit in WINMM)
67 /* ### start build ### */
68 extern LONG CALLBACK DRIVER_CallTo16_long_lwwll(FARPROC16,LONG,WORD,WORD,LONG,LONG);
69 /* ### stop build ### */
71 /**************************************************************************
72 * LoadStartupDrivers [internal]
74 static void WINE_UNUSED DRIVER_LoadStartupDrivers(void)
76 char str[256];
78 if (GetPrivateProfileStringA("drivers", NULL, "", str, sizeof(str), "SYSTEM.INI") < 2) {
79 ERR("Can't find drivers section in system.ini\n");
80 } else {
81 HDRVR16 hDrv;
82 LPSTR ptr;
84 for (ptr = str; *ptr; ptr += strlen(ptr) + 1) {
85 TRACE("str='%s'\n", ptr);
86 hDrv = OpenDriver16(ptr, "drivers", 0L);
87 TRACE("hDrv=%04x\n", hDrv);
89 TRACE("end of list !\n");
93 /**************************************************************************
94 * DRIVER_GetNumberOfModuleRefs [internal]
96 * Returns the number of open drivers which share the same module.
98 static WORD DRIVER_GetNumberOfModuleRefs(LPWINE_DRIVER lpNewDrv)
100 LPWINE_DRIVER lpDrv;
101 WORD count = 0;
103 for (lpDrv = lpDrvItemList; lpDrv; lpDrv = lpDrv->lpNextItem) {
104 if (lpDrv->dwFlags & WINE_GDF_16BIT) {
105 if (lpDrv->d.d16.hModule == lpNewDrv->d.d16.hModule) {
106 count++;
108 } else {
109 if (lpDrv->d.d32.hModule == lpNewDrv->d.d32.hModule) {
110 count++;
114 return count;
117 /**************************************************************************
118 * DRIVER_FindFromHDrvr16 [internal]
120 * From a hDrvr being 16 bits, returns the WINE internal structure.
122 static LPWINE_DRIVER DRIVER_FindFromHDrvr16(HDRVR16 hDrvr)
124 LPWINE_DRIVER lpDrv;
126 for (lpDrv = lpDrvItemList; lpDrv; lpDrv = lpDrv->lpNextItem) {
127 if (lpDrv->hDriver16 == hDrvr) {
128 break;
131 return lpDrv;
134 /**************************************************************************
135 * DRIVER_FindFromHDrvr [internal]
137 * From a hDrvr (being 16 or 32 bits), returns the WINE internal structure.
139 static LPWINE_DRIVER DRIVER_FindFromHDrvr(HDRVR hDrvr)
141 if (!IsBadWritePtr((void*)hDrvr, sizeof(WINE_DRIVER)) &&
142 ((LPWINE_DRIVER)hDrvr)->dwMagic == WINE_DI_MAGIC) {
143 return (LPWINE_DRIVER)hDrvr;
145 return DRIVER_FindFromHDrvr16(hDrvr);
148 /**************************************************************************
149 * DRIVER_MapMsg16To32 [internal]
151 * Map a 16 bit driver message to a 32 bit driver message.
152 * 1 : ok, some memory allocated, need to call DRIVER_UnMapMsg16To32
153 * 0 : ok, no memory allocated
154 * -1 : ko, unknown message
155 * -2 : ko, memory problem
157 static int DRIVER_MapMsg16To32(WORD wMsg, DWORD* lParam1, DWORD* lParam2)
159 int ret = -1;
161 switch (wMsg) {
162 case DRV_LOAD:
163 case DRV_ENABLE:
164 case DRV_DISABLE:
165 case DRV_FREE:
166 case DRV_QUERYCONFIGURE:
167 case DRV_REMOVE:
168 case DRV_EXITSESSION:
169 case DRV_EXITAPPLICATION:
170 case DRV_POWER:
171 /* lParam1 and lParam2 are not used */
172 ret = 0;
173 break;
174 case DRV_OPEN:
175 case DRV_CLOSE:
176 /* lParam1 is a NULL terminated string */
177 /* lParam2 is a pointer to an MCI_OPEN_DRIVER_PARMS for an MCI device */
178 if (*lParam1)
179 *lParam1 = (DWORD)PTR_SEG_TO_LIN(*lParam1);
180 if (*lParam2 && wMsg == DRV_OPEN) {
181 LPMCI_OPEN_DRIVER_PARMS16 modp16 = PTR_SEG_TO_LIN(*lParam2);
182 char* ptr = HeapAlloc(GetProcessHeap(), 0, sizeof(LPMCI_OPEN_DRIVER_PARMS16) + sizeof(MCI_OPEN_DRIVER_PARMSA));
183 LPMCI_OPEN_DRIVER_PARMSA modp32;
185 if (ptr) {
186 *(LPMCI_OPEN_DRIVER_PARMS16*)ptr = modp16;
187 modp32 = (LPMCI_OPEN_DRIVER_PARMSA)(ptr + sizeof(LPMCI_OPEN_DRIVER_PARMSA));
189 modp32->wDeviceID = modp16->wDeviceID;
190 modp32->lpstrParams = PTR_SEG_TO_LIN(modp16->lpstrParams);
191 } else {
192 return -2;
194 *lParam2 = (DWORD)modp32;
196 ret = 1;
197 break;
198 case DRV_CONFIGURE:
199 case DRV_INSTALL:
200 /* lParam1 is a handle to a window (or not used), lParam2 is a pointer to DRVCONFIGINFO */
201 if (*lParam2) {
202 LPDRVCONFIGINFO dci32 = HeapAlloc(GetProcessHeap(), 0, sizeof(DRVCONFIGINFO));
203 LPDRVCONFIGINFO16 dci16 = PTR_SEG_TO_LIN(*lParam2);
205 if (dci32) {
206 dci32->dwDCISize = sizeof(DRVCONFIGINFO);
207 dci32->lpszDCISectionName = HEAP_strdupAtoW(GetProcessHeap(), 0, PTR_SEG_TO_LIN(dci16->lpszDCISectionName));
208 dci32->lpszDCIAliasName = HEAP_strdupAtoW(GetProcessHeap(), 0, PTR_SEG_TO_LIN(dci16->lpszDCIAliasName));
209 if (dci32->lpszDCISectionName == NULL || dci32->lpszDCIAliasName == NULL)
210 return -2;
211 } else {
212 return -2;
214 *lParam2 = (DWORD)dci32;
215 ret = 1;
216 } else {
217 ret = 0;
219 break;
220 default:
221 if ((wMsg >= 0x800 && wMsg < 0x900) || (wMsg >= 0x4000 && wMsg < 0x4100 )) {
222 /* FIXME: another hack to handle MCI and ICM messages...
223 * should find a *NICE* way to integrate DRIVER_ and
224 * MCI_ mapping/unmapping functions
226 ret = 0;
227 } else {
228 FIXME("Unknown message 0x%04x\n", wMsg);
231 return ret;
234 /**************************************************************************
235 * DRIVER_MapMsg16To32 [internal]
237 * UnMap a 16 bit driver message to a 32 bit driver message.
238 * 0 : ok
239 * -1 : ko
240 * -2 : ko, memory problem
242 static int DRIVER_UnMapMsg16To32(WORD wMsg, DWORD lParam1, DWORD lParam2)
244 int ret = -1;
246 switch (wMsg) {
247 case DRV_LOAD:
248 case DRV_ENABLE:
249 case DRV_DISABLE:
250 case DRV_FREE:
251 case DRV_QUERYCONFIGURE:
252 case DRV_REMOVE:
253 case DRV_EXITSESSION:
254 case DRV_EXITAPPLICATION:
255 case DRV_POWER:
256 /* lParam1 and lParam2 are not used */
257 case DRV_OPEN:
258 case DRV_CLOSE:
259 /* lParam1 is a NULL terminated string */
260 /* lParam2 is a pointer to an MCI_OPEN_DRIVER_PARMS for an MCI device */
261 if (lParam2 && wMsg == DRV_OPEN) {
262 LPMCI_OPEN_DRIVER_PARMSA modp32 = (LPMCI_OPEN_DRIVER_PARMSA)lParam2;
263 LPMCI_OPEN_DRIVER_PARMS16 modp16 = *(LPMCI_OPEN_DRIVER_PARMS16*)(lParam2 - sizeof(LPMCI_OPEN_DRIVER_PARMSA));
265 modp16->wCustomCommandTable = modp32->wCustomCommandTable;
266 modp16->wType = modp32->wType;
267 if (!HeapFree(GetProcessHeap(), 0, modp32))
268 FIXME("bad free line=%d\n", __LINE__);
270 ret = 0;
271 break;
272 case DRV_CONFIGURE:
273 case DRV_INSTALL:
274 /* lParam1 is a handle to a window (or not used), lParam2 is a pointer to DRVCONFIGINFO, lParam2 */
275 if (lParam2) {
276 LPDRVCONFIGINFO dci32 = (LPDRVCONFIGINFO)lParam2;
277 if (!HeapFree(GetProcessHeap(), 0, (LPVOID)dci32->lpszDCISectionName))
278 FIXME("bad free line=%d\n", __LINE__);
279 if (!HeapFree(GetProcessHeap(), 0, (LPVOID)dci32->lpszDCIAliasName))
280 FIXME("bad free line=%d\n", __LINE__);
281 if (!HeapFree(GetProcessHeap(), 0, dci32))
282 FIXME("bad free line=%d\n", __LINE__);
284 ret = 0;
285 break;
286 default:
287 if ((wMsg >= 0x800 && wMsg < 0x900) || (wMsg >= 0x4000 && wMsg < 0x4100 )) {
288 /* FIXME: another hack to handle MCI and ICM messages...
289 * should find a *NICE* way to integrate DRIVER_ and
290 * MCI_ mapping/unmapping functions
292 ret = 0;
293 } else {
294 FIXME("Unknown message 0x%04x\n", wMsg);
297 return ret;
300 /**************************************************************************
301 * DRIVER_MapMsg32To16 [internal]
303 * Map a 32 bit driver message to a 16 bit driver message.
304 * 1 : ok, some memory allocated, need to call DRIVER_UnMapMsg32To16
305 * 0 : ok, no memory allocated
306 * -1 : ko, unknown message
307 * -2 : ko, memory problem
309 static int DRIVER_MapMsg32To16(WORD wMsg, DWORD* lParam1, DWORD* lParam2)
311 int ret = -1;
313 switch (wMsg) {
314 case DRV_LOAD:
315 case DRV_ENABLE:
316 case DRV_DISABLE:
317 case DRV_FREE:
318 case DRV_QUERYCONFIGURE:
319 case DRV_REMOVE:
320 case DRV_EXITSESSION:
321 case DRV_EXITAPPLICATION:
322 case DRV_POWER:
323 /* lParam1 and lParam2 are not used */
324 ret = 0;
325 break;
326 case DRV_OPEN:
327 case DRV_CLOSE:
328 /* lParam1 is a NULL terminated string */
329 /* lParam2 is a pointer to an MCI_OPEN_DRIVER_PARMS for an MCI device */
330 if (*lParam1) {
331 LPSTR str = SEGPTR_STRDUP((LPSTR)*lParam1);
332 if (str) {
333 *lParam1 = (LPARAM)SEGPTR_GET(str);
334 ret = 0;
335 } else {
336 ret = -2;
338 } else {
339 ret = 0;
341 if (*lParam2 && wMsg == DRV_OPEN) {
342 LPMCI_OPEN_DRIVER_PARMS16 modp16;
343 char* ptr = SEGPTR_ALLOC(sizeof(LPMCI_OPEN_DRIVER_PARMSA) + sizeof(MCI_OPEN_DRIVER_PARMS16));
344 LPMCI_OPEN_DRIVER_PARMSA modp32 = (LPMCI_OPEN_DRIVER_PARMSA)(*lParam2);
346 if (ptr) {
347 *(LPMCI_OPEN_DRIVER_PARMSA*)ptr = modp32;
348 modp16 = (LPMCI_OPEN_DRIVER_PARMS16)(ptr + sizeof(LPMCI_OPEN_DRIVER_PARMSA));
350 modp16->wDeviceID = modp32->wDeviceID;
351 modp16->lpstrParams = PTR_SEG_TO_LIN(modp32->lpstrParams);
352 } else {
353 return -2;
355 *lParam2 = (DWORD)SEGPTR_GET(modp16);
356 ret = 1;
358 break;
359 case DRV_CONFIGURE:
360 case DRV_INSTALL:
361 /* lParam1 is a handle to a window (or not used), lParam2 is a pointer to DRVCONFIGINFO */
362 if (*lParam2) {
363 LPDRVCONFIGINFO16 dci16 = (LPDRVCONFIGINFO16)SEGPTR_ALLOC(sizeof(DRVCONFIGINFO16));
364 LPDRVCONFIGINFO dci32 = (LPDRVCONFIGINFO)(*lParam2);
366 if (dci16) {
367 LPSTR str1, str2;
369 dci16->dwDCISize = sizeof(DRVCONFIGINFO16);
371 if ((str1 = HEAP_strdupWtoA(GetProcessHeap(), 0, dci32->lpszDCISectionName)) != NULL &&
372 (str2 = SEGPTR_STRDUP(str1)) != NULL) {
373 dci16->lpszDCISectionName = (LPSTR)SEGPTR_GET(str2);
374 if (!HeapFree(GetProcessHeap(), 0, str1))
375 FIXME("bad free line=%d\n", __LINE__);
376 } else {
377 return -2;
379 if ((str1 = HEAP_strdupWtoA(GetProcessHeap(), 0, dci32->lpszDCIAliasName)) != NULL &&
380 (str2 = SEGPTR_STRDUP(str1)) != NULL) {
381 dci16->lpszDCIAliasName = (LPSTR)SEGPTR_GET(str2);
382 if (!HeapFree(GetProcessHeap(), 0, str1))
383 FIXME("bad free line=%d\n", __LINE__);
384 } else {
385 return -2;
387 } else {
388 return -2;
390 *lParam2 = (LPARAM)SEGPTR_GET(dci16);
391 ret = 1;
392 } else {
393 ret = 0;
395 break;
396 default:
397 if ((wMsg >= 0x800 && wMsg < 0x900) || (wMsg >= 0x4000 && wMsg < 0x4100 )) {
398 /* FIXME: another hack to handle MCI and ICM messages...
399 * should find a *NICE* way to integrate DRIVER_ and
400 * MCI_ mapping/unmapping functions
402 ret = 0;
403 } else {
404 FIXME("Unknown message 0x%04x\n", wMsg);
407 return ret;
410 /**************************************************************************
411 * DRIVER_UnMapMsg32To16 [internal]
413 * UnMap a 32 bit driver message to a 16 bit driver message.
414 * 0 : ok
415 * -1 : ko
416 * -2 : ko, memory problem
418 static int DRIVER_UnMapMsg32To16(WORD wMsg, DWORD lParam1, DWORD lParam2)
420 int ret = -1;
422 switch (wMsg) {
423 case DRV_LOAD:
424 case DRV_ENABLE:
425 case DRV_DISABLE:
426 case DRV_FREE:
427 case DRV_QUERYCONFIGURE:
428 case DRV_REMOVE:
429 case DRV_EXITSESSION:
430 case DRV_EXITAPPLICATION:
431 case DRV_POWER:
432 /* lParam1 and lParam2 are not used */
433 case DRV_OPEN:
434 case DRV_CLOSE:
435 /* lParam1 is a NULL terminated string, lParam2 is unknown => may lead to some problem */
436 /* lParam2 is a pointer to an MCI_OPEN_DRIVER_PARMS for an MCI device */
437 if (lParam1) if (!SEGPTR_FREE(PTR_SEG_TO_LIN(lParam1)))
438 FIXME("bad free line=%d\n", __LINE__);
440 if (lParam2 && wMsg == DRV_OPEN) {
441 LPMCI_OPEN_DRIVER_PARMS16 modp16 = (LPMCI_OPEN_DRIVER_PARMS16)PTR_SEG_TO_LIN(lParam2);
442 LPMCI_OPEN_DRIVER_PARMSA modp32 = *(LPMCI_OPEN_DRIVER_PARMSA*)((char*)modp16 - sizeof(LPMCI_OPEN_DRIVER_PARMSA));
444 modp32->wCustomCommandTable = modp16->wCustomCommandTable;
445 modp32->wType = modp16->wType;
446 if (!SEGPTR_FREE((char*)modp16 - sizeof(LPMCI_OPEN_DRIVER_PARMSA)))
447 FIXME("bad free line=%d\n", __LINE__);
449 ret = 0;
450 break;
451 case DRV_CONFIGURE:
452 case DRV_INSTALL:
453 /* lParam1 is a handle to a window (or not used), lParam2 is a pointer to DRVCONFIGINFO, lParam2 */
454 if (lParam2) {
455 LPDRVCONFIGINFO16 dci16 = (LPDRVCONFIGINFO16)PTR_SEG_TO_LIN(lParam2);
457 if (!SEGPTR_FREE(PTR_SEG_TO_LIN(dci16->lpszDCISectionName)))
458 FIXME("bad free line=%d\n", __LINE__);
459 if (!SEGPTR_FREE(PTR_SEG_TO_LIN(dci16->lpszDCIAliasName)))
460 FIXME("bad free line=%d\n", __LINE__);
461 if (!SEGPTR_FREE(dci16))
462 FIXME("bad free line=%d\n", __LINE__);
464 ret = 0;
465 break;
466 default:
467 if ((wMsg >= 0x800 && wMsg < 0x900) || (wMsg >= 0x4000 && wMsg < 0x4100 )) {
468 /* FIXME: another hack to handle MCI and ICM messages...
469 * should find a *NICE* way to integrate DRIVER_ and
470 * MCI_ mapping/unmapping functions
472 ret = 0;
473 } else {
474 FIXME("Unknown message 0x%04x\n", wMsg);
477 return ret;
480 /**************************************************************************
481 * SendDriverMessage [USER.251]
483 LRESULT WINAPI SendDriverMessage16(HDRVR16 hDriver, UINT16 msg, LPARAM lParam1,
484 LPARAM lParam2)
486 LPWINE_DRIVER lpDrv;
487 LRESULT retval = 0;
488 int mapRet;
490 TRACE("(%04x, %04X, %08lX, %08lX)\n", hDriver, msg, lParam1, lParam2);
492 lpDrv = DRIVER_FindFromHDrvr16(hDriver);
493 if (lpDrv != NULL && lpDrv->hDriver16 == hDriver) {
494 if (lpDrv->dwFlags & WINE_GDF_16BIT) {
495 TRACE("Before CallDriverProc proc=%p driverID=%08lx hDrv=%u wMsg=%04x p1=%08lx p2=%08lx\n",
496 lpDrv->d.d16.lpDrvProc, lpDrv->dwDriverID, hDriver, msg, lParam1, lParam2);
497 retval = DRIVER_CallTo16_long_lwwll((FARPROC16)lpDrv->d.d16.lpDrvProc, lpDrv->dwDriverID,
498 hDriver, msg, lParam1, lParam2);
499 } else {
500 mapRet = DRIVER_MapMsg16To32(msg, &lParam1, &lParam2);
501 if (mapRet >= 0) {
502 TRACE("Before func32 call proc=%p driverID=%08lx hDrv=%u wMsg=%04x p1=%08lx p2=%08lx\n",
503 lpDrv->d.d32.lpDrvProc, lpDrv->dwDriverID, (HDRVR)lpDrv, msg, lParam1, lParam2);
504 retval = lpDrv->d.d32.lpDrvProc(lpDrv->dwDriverID, (HDRVR)lpDrv, msg, lParam1, lParam2);
505 if (mapRet >= 1) {
506 DRIVER_UnMapMsg16To32(msg, lParam1, lParam2);
508 } else {
509 retval = 0;
512 } else {
513 WARN("Bad driver handle %u\n", hDriver);
516 TRACE("retval = %ld\n", retval);
517 return retval;
520 /**************************************************************************
521 * SendDriverMessage [WINMM.19]
523 LRESULT WINAPI SendDriverMessage(HDRVR hDriver, UINT msg, LPARAM lParam1,
524 LPARAM lParam2)
526 LPWINE_DRIVER lpDrv;
527 LRESULT retval = 0;
528 int mapRet;
530 TRACE("(%04x, %04X, %08lX, %08lX)\n", hDriver, msg, lParam1, lParam2);
532 lpDrv = DRIVER_FindFromHDrvr(hDriver);
534 if (lpDrv != NULL) {
535 if (lpDrv->dwFlags & WINE_GDF_16BIT) {
536 mapRet = DRIVER_MapMsg32To16(msg, &lParam1, &lParam2);
537 if (mapRet >= 0) {
538 TRACE("Before CallDriverProc proc=%p driverID=%08lx hDrv=%u wMsg=%04x p1=%08lx p2=%08lx\n",
539 lpDrv->d.d16.lpDrvProc, lpDrv->dwDriverID, lpDrv->hDriver16, msg, lParam1, lParam2);
540 retval = DRIVER_CallTo16_long_lwwll((FARPROC16)lpDrv->d.d16.lpDrvProc, lpDrv->dwDriverID,
541 lpDrv->hDriver16, msg, lParam1, lParam2);
542 if (mapRet >= 1) {
543 DRIVER_UnMapMsg32To16(msg, lParam1, lParam2);
545 } else {
546 retval = 0;
548 } else {
549 TRACE("Before func32 call proc=%p driverID=%08lx hDrv=%u wMsg=%04x p1=%08lx p2=%08lx\n",
550 lpDrv->d.d32.lpDrvProc, lpDrv->dwDriverID, hDriver, msg, lParam1, lParam2);
551 retval = lpDrv->d.d32.lpDrvProc(lpDrv->dwDriverID, hDriver, msg, lParam1, lParam2);
553 } else {
554 WARN("Bad driver handle %u\n", hDriver);
556 TRACE("retval = %ld\n", retval);
558 return retval;
561 /**************************************************************************
562 * DRIVER_RemoveFromList [internal]
564 * Generates all the logic to handle driver closure / deletion
565 * Removes a driver struct to the list of open drivers.
567 static BOOL DRIVER_RemoveFromList(LPWINE_DRIVER lpDrv)
569 lpDrv->dwDriverID = 0;
570 if (DRIVER_GetNumberOfModuleRefs(lpDrv) == 1) {
571 SendDriverMessage((HDRVR)lpDrv, DRV_DISABLE, 0L, 0L);
572 SendDriverMessage((HDRVR)lpDrv, DRV_FREE, 0L, 0L);
575 if (lpDrv->lpPrevItem)
576 lpDrv->lpPrevItem->lpNextItem = lpDrv->lpNextItem;
577 else
578 lpDrvItemList = lpDrv->lpNextItem;
579 if (lpDrv->lpNextItem)
580 lpDrv->lpNextItem->lpPrevItem = lpDrv->lpPrevItem;
582 return TRUE;
585 /**************************************************************************
586 * DRIVER_AddToList [internal]
588 * Adds a driver struct to the list of open drivers.
589 * Generates all the logic to handle driver creation / open.
591 static BOOL DRIVER_AddToList(LPWINE_DRIVER lpNewDrv, LPARAM lParam, BOOL bCallFrom32)
593 lpNewDrv->dwMagic = WINE_DI_MAGIC;
594 /* First driver to be loaded for this module, need to load correctly the module */
595 if (DRIVER_GetNumberOfModuleRefs(lpNewDrv) == 0) {
596 if (SendDriverMessage((HDRVR)lpNewDrv, DRV_LOAD, 0L, 0L) != DRV_SUCCESS) {
597 TRACE("DRV_LOAD failed on driver 0x%08lx\n", (DWORD)lpNewDrv);
598 return FALSE;
600 if (SendDriverMessage((HDRVR)lpNewDrv, DRV_ENABLE, 0L, 0L) != DRV_SUCCESS) {
601 TRACE("DRV_ENABLE failed on driver 0x%08lx\n", (DWORD)lpNewDrv);
602 return FALSE;
606 lpNewDrv->lpNextItem = NULL;
607 if (lpDrvItemList == NULL) {
608 lpDrvItemList = lpNewDrv;
609 lpNewDrv->lpPrevItem = NULL;
610 } else {
611 LPWINE_DRIVER lpDrv = lpDrvItemList; /* find end of list */
612 while (lpDrv->lpNextItem != NULL)
613 lpDrv = lpDrv->lpNextItem;
615 lpDrv->lpNextItem = lpNewDrv;
616 lpNewDrv->lpPrevItem = lpDrv;
618 /* Now just open a new instance of a driver on this module */
619 if (bCallFrom32) {
620 lpNewDrv->dwDriverID = SendDriverMessage((HDRVR)lpNewDrv, DRV_OPEN, 0L, lParam);
621 } else {
622 lpNewDrv->dwDriverID = SendDriverMessage16(lpNewDrv->hDriver16, DRV_OPEN, 0L, lParam);
624 if (lpNewDrv->dwDriverID == 0) {
625 TRACE("DRV_OPEN failed on driver 0x%08lx\n", (DWORD)lpNewDrv);
626 DRIVER_RemoveFromList(lpNewDrv);
627 return FALSE;
630 return TRUE;
633 /**************************************************************************
634 * DRIVER_CreateDrvr16 [internal]
636 * Creates unique ID for 16 bit drivers.
638 static HDRVR16 DRIVER_CreateDrvr16(void)
640 static WORD DRIVER_hDrvr16Counter = 0;
642 while (DRIVER_FindFromHDrvr16(++DRIVER_hDrvr16Counter));
643 return DRIVER_hDrvr16Counter;
646 /**************************************************************************
647 * DRIVER_CloseDriver [internal]
650 BOOL DRIVER_CloseDriver(LPWINE_DRIVER lpDrv, DWORD lParam1, DWORD lParam2)
652 if (lpDrv != NULL) {
653 SendDriverMessage((HDRVR)lpDrv, DRV_CLOSE, lParam1, lParam2);
655 if (DRIVER_RemoveFromList(lpDrv)) {
656 HeapFree(GetProcessHeap(), 0, lpDrv);
657 return TRUE;
660 WARN("Failed to close driver\n");
661 return FALSE;
664 /**************************************************************************
665 * DRIVER_RegisterDriver16 [internal]
667 * Creates all the WINE internal representations for a 16 bit driver.
668 * The driver is also open by sending the correct messages.
670 LPWINE_DRIVER DRIVER_RegisterDriver16(LPCSTR lpName, HMODULE16 hModule, DRIVERPROC16 lpProc,
671 LPARAM lParam, BOOL bCallFrom32)
673 LPWINE_DRIVER lpDrv;
675 lpDrv = HeapAlloc(GetProcessHeap(), 0, sizeof(WINE_DRIVER));
676 if (lpDrv != NULL) {
677 lpDrv->dwFlags = WINE_GDF_EXIST|WINE_GDF_16BIT;
678 lpDrv->dwDriverID = 0;
679 lpDrv->hDriver16 = DRIVER_CreateDrvr16();
680 lstrcpynA(lpDrv->szAliasName, lpName, sizeof(lpDrv->szAliasName));
681 lpDrv->d.d16.hModule = hModule;
682 lpDrv->d.d16.lpDrvProc = lpProc;
684 if (!DRIVER_AddToList(lpDrv, lParam, bCallFrom32)) {
685 HeapFree(GetProcessHeap(), 0, lpDrv);
686 lpDrv = NULL;
689 return lpDrv;
692 /**************************************************************************
693 * DRIVER_RegisterDriver32 [internal]
695 * Creates all the WINE internal representations for a 32 bit driver.
696 * The driver is also open by sending the correct messages.
698 LPWINE_DRIVER DRIVER_RegisterDriver32(LPCSTR lpName, HMODULE hModule, DRIVERPROC lpProc,
699 LPARAM lParam, BOOL bCallFrom32)
701 LPWINE_DRIVER lpDrv;
703 lpDrv = HeapAlloc(GetProcessHeap(), 0, sizeof(WINE_DRIVER));
704 if (lpDrv != NULL) {
705 lpDrv->dwFlags = WINE_GDF_EXIST;
706 lpDrv->dwDriverID = 0;
707 lpDrv->hDriver16 = DRIVER_CreateDrvr16();
708 lstrcpynA(lpDrv->szAliasName, lpName, sizeof(lpDrv->szAliasName));
709 lpDrv->d.d32.hModule = hModule;
710 lpDrv->d.d32.lpDrvProc = lpProc;
712 if (!DRIVER_AddToList(lpDrv, lParam, bCallFrom32)) {
713 HeapFree(GetProcessHeap(), 0, lpDrv);
714 lpDrv = NULL;
717 return lpDrv;
720 /**************************************************************************
721 * DRIVER_TryOpenDriver16 [internal]
723 * Tries to load a 16 bit driver whose DLL's (module) name is lpFileName.
725 static HDRVR16 DRIVER_TryOpenDriver16(LPCSTR lpFileName, LPARAM lParam, BOOL bCallFrom32)
727 LPWINE_DRIVER lpDrv = NULL;
728 LPCSTR lpSFN;
729 HMODULE16 hModule;
730 DRIVERPROC16 lpProc;
732 TRACE("('%s', %08lX, %d);\n", lpFileName, lParam, bCallFrom32);
734 if (strlen(lpFileName) < 1)
735 return 0;
737 lpSFN = strrchr(lpFileName, '\\');
738 lpSFN = (lpSFN) ? (lpSFN + 1) : lpFileName;
740 if ((hModule = LoadModule16(lpFileName, (LPVOID)-1)) >= 32) {
741 if ((lpProc = (DRIVERPROC16)GetProcAddress16(hModule, "DRIVERPROC")) != NULL) {
742 lpDrv = DRIVER_RegisterDriver16(lpSFN, hModule, lpProc, lParam, bCallFrom32);
743 } else {
744 FreeLibrary16(hModule);
745 TRACE("No DriverProc found\n");
746 lpDrv = 0;
748 } else {
749 TRACE("Unable to load 16 bit module (%s): %d\n", lpFileName, hModule);
751 return lpDrv ? lpDrv->hDriver16 : 0;
754 /**************************************************************************
755 * DRIVER_TryOpenDriver32 [internal]
757 * Tries to load a 32 bit driver whose DLL's (module) name is lpFileName.
759 static HDRVR DRIVER_TryOpenDriver32(LPCSTR lpFileName, LPARAM lParam, BOOL bCallFrom32)
761 LPWINE_DRIVER lpDrv = NULL;
762 LPCSTR lpSFN;
763 HMODULE hModule;
764 DRIVERPROC lpProc;
766 TRACE("('%s', %08lX, %d);\n", lpFileName, lParam, bCallFrom32);
768 if (strlen(lpFileName) < 1)
769 return 0;
771 lpSFN = strrchr(lpFileName, '\\');
772 lpSFN = (lpSFN) ? (lpSFN + 1) : lpFileName;
774 if ((hModule = LoadLibraryA(lpFileName)) != 0) {
775 if ((lpProc = GetProcAddress(hModule, "DriverProc")) != NULL) {
776 lpDrv = DRIVER_RegisterDriver32(lpSFN, hModule, lpProc, lParam, bCallFrom32);
777 } else {
778 FreeLibrary(hModule);
779 lpDrv = 0;
780 TRACE("No DriverProc found\n");
782 } else {
783 TRACE("Unable to load 32 bit module \"%s\"\n", lpFileName);
785 TRACE("=> %p\n", lpDrv);
786 return (HDRVR)lpDrv;
789 /**************************************************************************
790 * OpenDriver16 [USER.252]
792 HDRVR16 WINAPI OpenDriver16(LPCSTR lpDriverName, LPCSTR lpSectionName, LPARAM lParam)
794 HDRVR16 hDriver = 0;
795 char drvName[128];
797 TRACE("('%s', '%s', %08lX);\n", lpDriverName, lpSectionName, lParam);
799 if (lpSectionName == NULL) {
800 hDriver = DRIVER_TryOpenDriver16(lpDriverName, lParam, FALSE);
801 if (!hDriver) {
802 hDriver = DRIVER_TryOpenDriver32(lpDriverName, lParam, FALSE);
804 if (!hDriver) {
805 /* in case hDriver is NULL, search in Drivers32 section */
806 lpSectionName = "Drivers";
809 if (!hDriver && GetPrivateProfileStringA(lpSectionName, lpDriverName, "",
810 drvName, sizeof(drvName), "SYSTEM.INI") > 0) {
811 hDriver = DRIVER_TryOpenDriver16(drvName, lParam, FALSE);
813 if (!hDriver)
814 ERR("Failed to open driver %s from system.ini file, section %s\n", lpDriverName, lpSectionName);
815 else
816 TRACE("=> %08x\n", hDriver);
817 return hDriver;
820 /**************************************************************************
821 * OpenDriverA [WINMM.15]
822 * (0,1,DRV_LOAD ,0 ,0)
823 * (0,1,DRV_ENABLE,0 ,0)
824 * (0,1,DRV_OPEN ,buf[256],0)
826 HDRVR WINAPI OpenDriverA(LPCSTR lpDriverName, LPCSTR lpSectionName, LPARAM lParam)
828 HDRVR hDriver = 0;
829 char drvName[128];
831 TRACE("(%s, %s, 0x%08lx);\n", debugstr_a(lpDriverName), debugstr_a(lpSectionName), lParam);
833 if (lpSectionName == NULL) {
834 lstrcpynA(drvName, lpDriverName, sizeof(drvName));
835 hDriver = DRIVER_TryOpenDriver32(lpDriverName, lParam, TRUE);
836 if (!hDriver) {
837 hDriver = DRIVER_TryOpenDriver16(lpDriverName, lParam, TRUE);
839 if (!hDriver) {
840 if (GetPrivateProfileStringA("Drivers32", lpDriverName, "", drvName,
841 sizeof(drvName), "SYSTEM.INI")) {
842 hDriver = DRIVER_TryOpenDriver32(drvName, lParam, TRUE);
846 if (!hDriver) {
847 if (GetPrivateProfileStringA("Drivers", lpDriverName, "", drvName,
848 sizeof(drvName), "SYSTEM.INI")) {
849 hDriver = DRIVER_TryOpenDriver16(drvName, lParam, TRUE);
853 } else {
854 if (GetPrivateProfileStringA(lpSectionName, lpDriverName, "", drvName,
855 sizeof(drvName), "SYSTEM.INI")) {
856 hDriver = DRIVER_TryOpenDriver32(drvName, lParam, TRUE);
857 if (!hDriver) {
858 hDriver = DRIVER_TryOpenDriver16(drvName, lParam, TRUE);
862 if (!hDriver)
863 ERR("Failed to open driver %s from system.ini file, section %s\n", lpDriverName, lpSectionName);
864 else
865 TRACE("=> %08x\n", hDriver);
866 return hDriver;
869 /**************************************************************************
870 * OpenDriverW [WINMM.15]
872 HDRVR WINAPI OpenDriverW(LPCWSTR lpDriverName, LPCWSTR lpSectionName, LPARAM lParam)
874 LPSTR dn = HEAP_strdupWtoA(GetProcessHeap(), 0, lpDriverName);
875 LPSTR sn = HEAP_strdupWtoA(GetProcessHeap(), 0, lpSectionName);
876 HDRVR ret = OpenDriverA(dn, sn, lParam);
878 if (dn) HeapFree(GetProcessHeap(), 0, dn);
879 if (sn) HeapFree(GetProcessHeap(), 0, sn);
880 return ret;
883 /**************************************************************************
884 * CloseDriver16 [USER.253]
886 LRESULT WINAPI CloseDriver16(HDRVR16 hDrvr, LPARAM lParam1, LPARAM lParam2)
888 TRACE("(%04x, %08lX, %08lX);\n", hDrvr, lParam1, lParam2);
890 return DRIVER_CloseDriver(DRIVER_FindFromHDrvr16(hDrvr), lParam1, lParam2);
893 /**************************************************************************
894 * CloseDriver [WINMM.4]
896 LRESULT WINAPI CloseDriver(HDRVR hDrvr, LPARAM lParam1, LPARAM lParam2)
898 TRACE("(%04x, %08lX, %08lX);\n", hDrvr, lParam1, lParam2);
900 return DRIVER_CloseDriver(DRIVER_FindFromHDrvr(hDrvr), lParam1, lParam2);
903 /**************************************************************************
904 * GetDriverModuleHandle [USER.254]
906 HMODULE16 WINAPI GetDriverModuleHandle16(HDRVR16 hDrvr)
908 LPWINE_DRIVER lpDrv;
909 HMODULE16 hModule = 0;
911 TRACE("(%04x);\n", hDrvr);
913 lpDrv = DRIVER_FindFromHDrvr16(hDrvr);
914 if (lpDrv != NULL && lpDrv->hDriver16 == hDrvr && (lpDrv->dwFlags & WINE_GDF_16BIT)) {
915 hModule = lpDrv->d.d16.hModule;
917 TRACE("=> %d\n", hModule);
918 return hModule;
921 /**************************************************************************
922 * GetDriverFlags [WINMM.13]
923 * [in] hDrvr handle to the driver
925 * Returns:
926 * 0x00000000 if hDrvr is an invalid handle
927 * 0x80000000 if hDrvr is a valid 32 bit driver
928 * 0x90000000 if hDrvr is a valid 16 bit driver
930 DWORD WINAPI GetDriverFlags(HDRVR hDrvr)
932 LPWINE_DRIVER lpDrv;
933 DWORD ret = 0;
935 TRACE("(%04x)\n", hDrvr);
937 if ((lpDrv = DRIVER_FindFromHDrvr(hDrvr)) != NULL) {
938 ret = lpDrv->dwFlags;
940 return ret;
943 /**************************************************************************
944 * GetDriverModuleHandle [WINMM.14]
946 HMODULE WINAPI GetDriverModuleHandle(HDRVR hDrvr)
948 LPWINE_DRIVER lpDrv;
949 HMODULE hModule = 0;
951 TRACE("(%04x);\n", hDrvr);
953 lpDrv = DRIVER_FindFromHDrvr(hDrvr);
954 if (lpDrv != NULL && !(lpDrv->dwFlags & WINE_GDF_16BIT)) {
955 hModule = lpDrv->d.d32.hModule;
957 TRACE("=> %d\n", hModule);
958 return hModule;
961 /**************************************************************************
962 * DefDriverProc16 [USER.255]
964 LRESULT WINAPI DefDriverProc16(DWORD dwDevID, HDRVR16 hDriv, UINT16 wMsg,
965 LPARAM lParam1, LPARAM lParam2)
967 TRACE("devID=0x%08lx hDrv=0x%04x wMsg=%04x lP1=0x%08lx lP2=0x%08lx\n",
968 dwDevID, hDriv, wMsg, lParam1, lParam2);
970 switch(wMsg) {
971 case DRV_LOAD:
972 case DRV_FREE:
973 case DRV_ENABLE:
974 case DRV_DISABLE:
975 return (LRESULT)1L;
976 case DRV_OPEN:
977 case DRV_CLOSE:
978 case DRV_QUERYCONFIGURE:
979 return (LRESULT)0L;
980 case DRV_CONFIGURE:
981 MessageBoxA(0, "Driver isn't configurable !", "Wine Driver", MB_OK);
982 return (LRESULT)0L;
983 case DRV_INSTALL:
984 case DRV_REMOVE:
985 return DRV_SUCCESS;
986 default:
987 return (LRESULT)0L;
991 /**************************************************************************
992 * GetDriverInfo [USER.256]
994 BOOL16 WINAPI GetDriverInfo16(HDRVR16 hDrvr, LPDRIVERINFOSTRUCT16 lpDrvInfo)
996 LPWINE_DRIVER lpDrv;
997 BOOL16 ret = FALSE;
999 TRACE("(%04x, %p);\n", hDrvr, lpDrvInfo);
1001 if (lpDrvInfo == NULL || lpDrvInfo->length != sizeof(DRIVERINFOSTRUCT16))
1002 return FALSE;
1004 lpDrv = DRIVER_FindFromHDrvr16(hDrvr);
1005 if (lpDrv != NULL && lpDrv->hDriver16 == hDrvr &&
1006 (lpDrv->dwFlags & WINE_GDF_16BIT)) {
1007 lpDrvInfo->hDriver = lpDrv->hDriver16;
1008 lpDrvInfo->hModule = lpDrv->d.d16.hModule;
1009 lstrcpynA(lpDrvInfo->szAliasName, lpDrv->szAliasName, sizeof(lpDrvInfo->szAliasName));
1010 ret = TRUE;
1013 return ret;
1016 /**************************************************************************
1017 * GetNextDriver [USER.257]
1019 HDRVR16 WINAPI GetNextDriver16(HDRVR16 hDrvr, DWORD dwFlags)
1021 HDRVR16 hRetDrv = 0;
1022 LPWINE_DRIVER lpDrv;
1024 TRACE("(%04x, %08lX);\n", hDrvr, dwFlags);
1026 if (hDrvr == 0) {
1027 if (lpDrvItemList == NULL) {
1028 FIXME("drivers list empty !\n");
1029 /* FIXME: code was using DRIVER_LoadStartupDrivers(); before ?
1030 * I (EPP) don't quite understand this
1032 if (lpDrvItemList == NULL)
1033 return 0;
1035 lpDrv = lpDrvItemList;
1036 if (dwFlags & GND_REVERSE) {
1037 while (lpDrv->lpNextItem)
1038 lpDrv = lpDrv->lpNextItem;
1040 } else {
1041 lpDrv = DRIVER_FindFromHDrvr16(hDrvr);
1042 if (lpDrv != NULL) {
1043 if (dwFlags & GND_REVERSE) {
1044 lpDrv = (lpDrv->lpPrevItem) ? lpDrv->lpPrevItem : NULL;
1045 } else {
1046 lpDrv = (lpDrv->lpNextItem) ? lpDrv->lpNextItem : NULL;
1051 hRetDrv = (lpDrv) ? lpDrv->hDriver16 : (HDRVR16)0;
1052 TRACE("return %04x !\n", hRetDrv);
1053 return hRetDrv;