1 /* -*- tab-width: 8; c-basic-offset: 4 -*- */
4 * WINE Drivers functions
6 * Copyright 1994 Martin Ayotte
7 * Copyright 1998 Marcus Meissner
8 * Copyright 1999 Eric Pouech
14 #include "wine/winbase16.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
36 char szAliasName[128];
37 /* as usual LPWINE_DRIVER == hDriver32 */
42 DRIVERPROC16 lpDrvProc;
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;
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)
78 if (GetPrivateProfileStringA("drivers", NULL, "", str, sizeof(str), "SYSTEM.INI") < 2) {
79 ERR("Can't find drivers section in system.ini\n");
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)
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) {
109 if (lpDrv->d.d32.hModule == lpNewDrv->d.d32.hModule) {
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)
126 for (lpDrv = lpDrvItemList; lpDrv; lpDrv = lpDrv->lpNextItem) {
127 if (lpDrv->hDriver16 == hDrvr) {
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)
166 case DRV_QUERYCONFIGURE:
168 case DRV_EXITSESSION:
169 case DRV_EXITAPPLICATION:
171 /* lParam1 and lParam2 are not used */
176 /* lParam1 is a NULL terminated string */
177 /* lParam2 is a pointer to an MCI_OPEN_DRIVER_PARMS for an MCI device */
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;
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);
194 *lParam2 = (DWORD)modp32;
200 /* lParam1 is a handle to a window (or not used), lParam2 is a pointer to DRVCONFIGINFO */
202 LPDRVCONFIGINFO dci32 = HeapAlloc(GetProcessHeap(), 0, sizeof(DRVCONFIGINFO));
203 LPDRVCONFIGINFO16 dci16 = PTR_SEG_TO_LIN(*lParam2);
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)
214 *lParam2 = (DWORD)dci32;
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
228 FIXME("Unknown message 0x%04x\n", wMsg);
234 /**************************************************************************
235 * DRIVER_MapMsg16To32 [internal]
237 * UnMap a 16 bit driver message to a 32 bit driver message.
240 * -2 : ko, memory problem
242 static int DRIVER_UnMapMsg16To32(WORD wMsg, DWORD lParam1, DWORD lParam2)
251 case DRV_QUERYCONFIGURE:
253 case DRV_EXITSESSION:
254 case DRV_EXITAPPLICATION:
256 /* lParam1 and lParam2 are not used */
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__);
274 /* lParam1 is a handle to a window (or not used), lParam2 is a pointer to DRVCONFIGINFO, 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__);
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
294 FIXME("Unknown message 0x%04x\n", wMsg);
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)
318 case DRV_QUERYCONFIGURE:
320 case DRV_EXITSESSION:
321 case DRV_EXITAPPLICATION:
323 /* lParam1 and lParam2 are not used */
328 /* lParam1 is a NULL terminated string */
329 /* lParam2 is a pointer to an MCI_OPEN_DRIVER_PARMS for an MCI device */
331 LPSTR str = SEGPTR_STRDUP((LPSTR)*lParam1);
333 *lParam1 = (LPARAM)SEGPTR_GET(str);
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);
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);
355 *lParam2 = (DWORD)SEGPTR_GET(modp16);
361 /* lParam1 is a handle to a window (or not used), lParam2 is a pointer to DRVCONFIGINFO */
363 LPDRVCONFIGINFO16 dci16 = (LPDRVCONFIGINFO16)SEGPTR_ALLOC(sizeof(DRVCONFIGINFO16));
364 LPDRVCONFIGINFO dci32 = (LPDRVCONFIGINFO)(*lParam2);
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__);
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__);
390 *lParam2 = (LPARAM)SEGPTR_GET(dci16);
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
404 FIXME("Unknown message 0x%04x\n", wMsg);
410 /**************************************************************************
411 * DRIVER_UnMapMsg32To16 [internal]
413 * UnMap a 32 bit driver message to a 16 bit driver message.
416 * -2 : ko, memory problem
418 static int DRIVER_UnMapMsg32To16(WORD wMsg, DWORD lParam1, DWORD lParam2)
427 case DRV_QUERYCONFIGURE:
429 case DRV_EXITSESSION:
430 case DRV_EXITAPPLICATION:
432 /* lParam1 and lParam2 are not used */
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__);
453 /* lParam1 is a handle to a window (or not used), lParam2 is a pointer to DRVCONFIGINFO, 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__);
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
474 FIXME("Unknown message 0x%04x\n", wMsg);
480 /**************************************************************************
481 * SendDriverMessage [USER.251]
483 LRESULT WINAPI SendDriverMessage16(HDRVR16 hDriver, UINT16 msg, LPARAM lParam1,
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);
500 mapRet = DRIVER_MapMsg16To32(msg, &lParam1, &lParam2);
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);
506 DRIVER_UnMapMsg16To32(msg, lParam1, lParam2);
513 WARN("Bad driver handle %u\n", hDriver);
516 TRACE("retval = %ld\n", retval);
520 /**************************************************************************
521 * SendDriverMessage [WINMM.19]
523 LRESULT WINAPI SendDriverMessage(HDRVR hDriver, UINT msg, LPARAM lParam1,
530 TRACE("(%04x, %04X, %08lX, %08lX)\n", hDriver, msg, lParam1, lParam2);
532 lpDrv = DRIVER_FindFromHDrvr(hDriver);
535 if (lpDrv->dwFlags & WINE_GDF_16BIT) {
536 mapRet = DRIVER_MapMsg32To16(msg, &lParam1, &lParam2);
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);
543 DRIVER_UnMapMsg32To16(msg, lParam1, lParam2);
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);
554 WARN("Bad driver handle %u\n", hDriver);
556 TRACE("retval = %ld\n", 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;
578 lpDrvItemList = lpDrv->lpNextItem;
579 if (lpDrv->lpNextItem)
580 lpDrv->lpNextItem->lpPrevItem = lpDrv->lpPrevItem;
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);
600 if (SendDriverMessage((HDRVR)lpNewDrv, DRV_ENABLE, 0L, 0L) != DRV_SUCCESS) {
601 TRACE("DRV_ENABLE failed on driver 0x%08lx\n", (DWORD)lpNewDrv);
606 lpNewDrv->lpNextItem = NULL;
607 if (lpDrvItemList == NULL) {
608 lpDrvItemList = lpNewDrv;
609 lpNewDrv->lpPrevItem = NULL;
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 */
620 lpNewDrv->dwDriverID = SendDriverMessage((HDRVR)lpNewDrv, DRV_OPEN, 0L, lParam);
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);
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)
653 SendDriverMessage((HDRVR)lpDrv, DRV_CLOSE, lParam1, lParam2);
655 if (DRIVER_RemoveFromList(lpDrv)) {
656 HeapFree(GetProcessHeap(), 0, lpDrv);
660 WARN("Failed to close driver\n");
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)
675 lpDrv = HeapAlloc(GetProcessHeap(), 0, sizeof(WINE_DRIVER));
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);
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)
703 lpDrv = HeapAlloc(GetProcessHeap(), 0, sizeof(WINE_DRIVER));
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);
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;
732 TRACE("('%s', %08lX, %d);\n", lpFileName, lParam, bCallFrom32);
734 if (strlen(lpFileName) < 1)
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);
744 FreeLibrary16(hModule);
745 TRACE("No DriverProc found\n");
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;
766 TRACE("('%s', %08lX, %d);\n", lpFileName, lParam, bCallFrom32);
768 if (strlen(lpFileName) < 1)
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);
778 FreeLibrary(hModule);
780 TRACE("No DriverProc found\n");
783 TRACE("Unable to load 32 bit module \"%s\"\n", lpFileName);
785 TRACE("=> %p\n", lpDrv);
789 /**************************************************************************
790 * OpenDriver16 [USER.252]
792 HDRVR16 WINAPI OpenDriver16(LPCSTR lpDriverName, LPCSTR lpSectionName, LPARAM lParam)
797 TRACE("('%s', '%s', %08lX);\n", lpDriverName, lpSectionName, lParam);
799 if (lpSectionName == NULL) {
800 hDriver = DRIVER_TryOpenDriver16(lpDriverName, lParam, FALSE);
802 hDriver = DRIVER_TryOpenDriver32(lpDriverName, lParam, FALSE);
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);
814 ERR("Failed to open driver %s from system.ini file, section %s\n", lpDriverName, lpSectionName);
816 TRACE("=> %08x\n", 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)
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);
837 hDriver = DRIVER_TryOpenDriver16(lpDriverName, lParam, TRUE);
840 if (GetPrivateProfileStringA("Drivers32", lpDriverName, "", drvName,
841 sizeof(drvName), "SYSTEM.INI")) {
842 hDriver = DRIVER_TryOpenDriver32(drvName, lParam, TRUE);
847 if (GetPrivateProfileStringA("Drivers", lpDriverName, "", drvName,
848 sizeof(drvName), "SYSTEM.INI")) {
849 hDriver = DRIVER_TryOpenDriver16(drvName, lParam, TRUE);
854 if (GetPrivateProfileStringA(lpSectionName, lpDriverName, "", drvName,
855 sizeof(drvName), "SYSTEM.INI")) {
856 hDriver = DRIVER_TryOpenDriver32(drvName, lParam, TRUE);
858 hDriver = DRIVER_TryOpenDriver16(drvName, lParam, TRUE);
863 ERR("Failed to open driver %s from system.ini file, section %s\n", lpDriverName, lpSectionName);
865 TRACE("=> %08x\n", 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);
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)
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);
921 /**************************************************************************
922 * GetDriverFlags [WINMM.13]
923 * [in] hDrvr handle to the driver
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)
935 TRACE("(%04x)\n", hDrvr);
937 if ((lpDrv = DRIVER_FindFromHDrvr(hDrvr)) != NULL) {
938 ret = lpDrv->dwFlags;
943 /**************************************************************************
944 * GetDriverModuleHandle [WINMM.14]
946 HMODULE WINAPI GetDriverModuleHandle(HDRVR hDrvr)
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);
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);
978 case DRV_QUERYCONFIGURE:
981 MessageBoxA(0, "Driver isn't configurable !", "Wine Driver", MB_OK);
991 /**************************************************************************
992 * GetDriverInfo [USER.256]
994 BOOL16 WINAPI GetDriverInfo16(HDRVR16 hDrvr, LPDRIVERINFOSTRUCT16 lpDrvInfo)
999 TRACE("(%04x, %p);\n", hDrvr, lpDrvInfo);
1001 if (lpDrvInfo == NULL || lpDrvInfo->length != sizeof(DRIVERINFOSTRUCT16))
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));
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);
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)
1035 lpDrv = lpDrvItemList;
1036 if (dwFlags & GND_REVERSE) {
1037 while (lpDrv->lpNextItem)
1038 lpDrv = lpDrv->lpNextItem;
1041 lpDrv = DRIVER_FindFromHDrvr16(hDrvr);
1042 if (lpDrv != NULL) {
1043 if (dwFlags & GND_REVERSE) {
1044 lpDrv = (lpDrv->lpPrevItem) ? lpDrv->lpPrevItem : NULL;
1046 lpDrv = (lpDrv->lpNextItem) ? lpDrv->lpNextItem : NULL;
1051 hRetDrv = (lpDrv) ? lpDrv->hDriver16 : (HDRVR16)0;
1052 TRACE("return %04x !\n", hRetDrv);