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"
20 #include "debugtools.h"
22 DEFAULT_DEBUG_CHANNEL(driver);
24 /* The following definitions are WINE internals */
25 /* FIXME: This is a WINE internal struct and should be moved in include/wine directory
26 * Please note that WINE shares 16 and 32 bit drivers on a single list...
27 * Basically, we maintain an external double view on drivers, so that a 16 bit drivers
28 * can be loaded/used... by 32 functions transparently
31 /* Who said goofy boy ? */
32 #define WINE_DI_MAGIC 0x900F1B01
34 typedef struct tagWINE_DRIVER
37 char szAliasName[128];
38 /* as usual LPWINE_DRIVER == hDriver32 */
43 DRIVERPROC16 lpDrvProc;
52 struct tagWINE_DRIVER* lpPrevItem;
53 struct tagWINE_DRIVER* lpNextItem;
54 } WINE_DRIVER, *LPWINE_DRIVER;
56 LPWINE_DRIVER DRIVER_RegisterDriver16(LPCSTR, HMODULE16, DRIVERPROC16, LPARAM, BOOL);
57 LPWINE_DRIVER DRIVER_RegisterDriver32(LPCSTR, HMODULE, DRIVERPROC, LPARAM, BOOL);
59 static LPWINE_DRIVER lpDrvItemList = NULL;
62 * - LoadModule count and clean up is not handled correctly (it's not a
63 * problem as long as FreeLibrary is not working correctly)
64 * - shoudln't the allocations be done on a per process basis ?
65 * - split 16/32 bit functions between DLLs as windows do (16 bit in USER, 32 bit in WINMM)
68 /* ### start build ### */
69 extern LONG CALLBACK DRIVER_CallTo16_long_lwwll(FARPROC16,LONG,WORD,WORD,LONG,LONG);
70 /* ### stop build ### */
72 /**************************************************************************
73 * LoadStartupDrivers [internal]
75 static void WINE_UNUSED DRIVER_LoadStartupDrivers(void)
79 if (GetPrivateProfileStringA("drivers", NULL, "", str, sizeof(str), "SYSTEM.INI") < 2) {
80 ERR("Can't find drivers section in system.ini\n");
85 for (ptr = str; *ptr; ptr += strlen(ptr) + 1) {
86 TRACE("str='%s'\n", ptr);
87 hDrv = OpenDriver16(ptr, "drivers", 0L);
88 TRACE("hDrv=%04x\n", hDrv);
90 TRACE("end of list !\n");
94 /**************************************************************************
95 * DRIVER_GetNumberOfModuleRefs [internal]
97 * Returns the number of open drivers which share the same module.
99 static WORD DRIVER_GetNumberOfModuleRefs(LPWINE_DRIVER lpNewDrv)
104 for (lpDrv = lpDrvItemList; lpDrv; lpDrv = lpDrv->lpNextItem) {
105 if (lpDrv->dwFlags & WINE_GDF_16BIT) {
106 if (lpDrv->d.d16.hModule == lpNewDrv->d.d16.hModule) {
110 if (lpDrv->d.d32.hModule == lpNewDrv->d.d32.hModule) {
118 /**************************************************************************
119 * DRIVER_FindFromHDrvr16 [internal]
121 * From a hDrvr being 16 bits, returns the WINE internal structure.
123 static LPWINE_DRIVER DRIVER_FindFromHDrvr16(HDRVR16 hDrvr)
127 for (lpDrv = lpDrvItemList; lpDrv; lpDrv = lpDrv->lpNextItem) {
128 if (lpDrv->hDriver16 == hDrvr) {
135 /**************************************************************************
136 * DRIVER_FindFromHDrvr [internal]
138 * From a hDrvr (being 16 or 32 bits), returns the WINE internal structure.
140 static LPWINE_DRIVER DRIVER_FindFromHDrvr(HDRVR hDrvr)
142 if (!IsBadWritePtr((void*)hDrvr, sizeof(WINE_DRIVER)) &&
143 ((LPWINE_DRIVER)hDrvr)->dwMagic == WINE_DI_MAGIC) {
144 return (LPWINE_DRIVER)hDrvr;
146 return DRIVER_FindFromHDrvr16(hDrvr);
149 /**************************************************************************
150 * DRIVER_MapMsg16To32 [internal]
152 * Map a 16 bit driver message to a 32 bit driver message.
153 * 1 : ok, some memory allocated, need to call DRIVER_UnMapMsg16To32
154 * 0 : ok, no memory allocated
155 * -1 : ko, unknown message
156 * -2 : ko, memory problem
158 static int DRIVER_MapMsg16To32(WORD wMsg, DWORD* lParam1, DWORD* lParam2)
167 case DRV_QUERYCONFIGURE:
169 case DRV_EXITSESSION:
170 case DRV_EXITAPPLICATION:
172 /* lParam1 and lParam2 are not used */
177 /* lParam1 is a NULL terminated string */
178 /* lParam2 is a pointer to an MCI_OPEN_DRIVER_PARMS for an MCI device */
180 *lParam1 = (DWORD)PTR_SEG_TO_LIN(*lParam1);
181 if (*lParam2 && wMsg == DRV_OPEN) {
182 LPMCI_OPEN_DRIVER_PARMS16 modp16 = PTR_SEG_TO_LIN(*lParam2);
183 char* ptr = HeapAlloc(GetProcessHeap(), 0, sizeof(LPMCI_OPEN_DRIVER_PARMS16) + sizeof(MCI_OPEN_DRIVER_PARMSA));
184 LPMCI_OPEN_DRIVER_PARMSA modp32;
187 *(LPMCI_OPEN_DRIVER_PARMS16*)ptr = modp16;
188 modp32 = (LPMCI_OPEN_DRIVER_PARMSA)(ptr + sizeof(LPMCI_OPEN_DRIVER_PARMSA));
190 modp32->wDeviceID = modp16->wDeviceID;
191 modp32->lpstrParams = PTR_SEG_TO_LIN(modp16->lpstrParams);
195 *lParam2 = (DWORD)modp32;
201 /* lParam1 is a handle to a window (or not used), lParam2 is a pointer to DRVCONFIGINFO */
203 LPDRVCONFIGINFO dci32 = HeapAlloc(GetProcessHeap(), 0, sizeof(DRVCONFIGINFO));
204 LPDRVCONFIGINFO16 dci16 = PTR_SEG_TO_LIN(*lParam2);
207 dci32->dwDCISize = sizeof(DRVCONFIGINFO);
208 dci32->lpszDCISectionName = HEAP_strdupAtoW(GetProcessHeap(), 0, PTR_SEG_TO_LIN(dci16->lpszDCISectionName));
209 dci32->lpszDCIAliasName = HEAP_strdupAtoW(GetProcessHeap(), 0, PTR_SEG_TO_LIN(dci16->lpszDCIAliasName));
210 if (dci32->lpszDCISectionName == NULL || dci32->lpszDCIAliasName == NULL)
215 *lParam2 = (DWORD)dci32;
222 if ((wMsg >= 0x800 && wMsg < 0x900) || (wMsg >= 0x4000 && wMsg < 0x4100 )) {
223 /* FIXME: another hack to handle MCI and ICM messages...
224 * should find a *NICE* way to integrate DRIVER_ and
225 * MCI_ mapping/unmapping functions
229 FIXME("Unknown message 0x%04x\n", wMsg);
235 /**************************************************************************
236 * DRIVER_MapMsg16To32 [internal]
238 * UnMap a 16 bit driver message to a 32 bit driver message.
241 * -2 : ko, memory problem
243 static int DRIVER_UnMapMsg16To32(WORD wMsg, DWORD lParam1, DWORD lParam2)
252 case DRV_QUERYCONFIGURE:
254 case DRV_EXITSESSION:
255 case DRV_EXITAPPLICATION:
257 /* lParam1 and lParam2 are not used */
260 /* lParam1 is a NULL terminated string */
261 /* lParam2 is a pointer to an MCI_OPEN_DRIVER_PARMS for an MCI device */
262 if (lParam2 && wMsg == DRV_OPEN) {
263 LPMCI_OPEN_DRIVER_PARMSA modp32 = (LPMCI_OPEN_DRIVER_PARMSA)lParam2;
264 LPMCI_OPEN_DRIVER_PARMS16 modp16 = *(LPMCI_OPEN_DRIVER_PARMS16*)(lParam2 - sizeof(LPMCI_OPEN_DRIVER_PARMSA));
266 modp16->wCustomCommandTable = modp32->wCustomCommandTable;
267 modp16->wType = modp32->wType;
268 if (!HeapFree(GetProcessHeap(), 0, modp32))
269 FIXME("bad free line=%d\n", __LINE__);
275 /* lParam1 is a handle to a window (or not used), lParam2 is a pointer to DRVCONFIGINFO, lParam2 */
277 LPDRVCONFIGINFO dci32 = (LPDRVCONFIGINFO)lParam2;
278 if (!HeapFree(GetProcessHeap(), 0, (LPVOID)dci32->lpszDCISectionName))
279 FIXME("bad free line=%d\n", __LINE__);
280 if (!HeapFree(GetProcessHeap(), 0, (LPVOID)dci32->lpszDCIAliasName))
281 FIXME("bad free line=%d\n", __LINE__);
282 if (!HeapFree(GetProcessHeap(), 0, dci32))
283 FIXME("bad free line=%d\n", __LINE__);
288 if ((wMsg >= 0x800 && wMsg < 0x900) || (wMsg >= 0x4000 && wMsg < 0x4100 )) {
289 /* FIXME: another hack to handle MCI and ICM messages...
290 * should find a *NICE* way to integrate DRIVER_ and
291 * MCI_ mapping/unmapping functions
295 FIXME("Unknown message 0x%04x\n", wMsg);
301 /**************************************************************************
302 * DRIVER_MapMsg32To16 [internal]
304 * Map a 32 bit driver message to a 16 bit driver message.
305 * 1 : ok, some memory allocated, need to call DRIVER_UnMapMsg32To16
306 * 0 : ok, no memory allocated
307 * -1 : ko, unknown message
308 * -2 : ko, memory problem
310 static int DRIVER_MapMsg32To16(WORD wMsg, DWORD* lParam1, DWORD* lParam2)
319 case DRV_QUERYCONFIGURE:
321 case DRV_EXITSESSION:
322 case DRV_EXITAPPLICATION:
324 /* lParam1 and lParam2 are not used */
329 /* lParam1 is a NULL terminated string */
330 /* lParam2 is a pointer to an MCI_OPEN_DRIVER_PARMS for an MCI device */
332 LPSTR str = SEGPTR_STRDUP((LPSTR)*lParam1);
334 *lParam1 = (LPARAM)SEGPTR_GET(str);
342 if (*lParam2 && wMsg == DRV_OPEN) {
343 LPMCI_OPEN_DRIVER_PARMS16 modp16;
344 char* ptr = SEGPTR_ALLOC(sizeof(LPMCI_OPEN_DRIVER_PARMSA) + sizeof(MCI_OPEN_DRIVER_PARMS16));
345 LPMCI_OPEN_DRIVER_PARMSA modp32 = (LPMCI_OPEN_DRIVER_PARMSA)(*lParam2);
348 *(LPMCI_OPEN_DRIVER_PARMSA*)ptr = modp32;
349 modp16 = (LPMCI_OPEN_DRIVER_PARMS16)(ptr + sizeof(LPMCI_OPEN_DRIVER_PARMSA));
351 modp16->wDeviceID = modp32->wDeviceID;
352 modp16->lpstrParams = PTR_SEG_TO_LIN(modp32->lpstrParams);
356 *lParam2 = (DWORD)SEGPTR_GET(modp16);
362 /* lParam1 is a handle to a window (or not used), lParam2 is a pointer to DRVCONFIGINFO */
364 LPDRVCONFIGINFO16 dci16 = (LPDRVCONFIGINFO16)SEGPTR_ALLOC(sizeof(DRVCONFIGINFO16));
365 LPDRVCONFIGINFO dci32 = (LPDRVCONFIGINFO)(*lParam2);
370 dci16->dwDCISize = sizeof(DRVCONFIGINFO16);
372 if ((str1 = HEAP_strdupWtoA(GetProcessHeap(), 0, dci32->lpszDCISectionName)) != NULL &&
373 (str2 = SEGPTR_STRDUP(str1)) != NULL) {
374 dci16->lpszDCISectionName = (LPSTR)SEGPTR_GET(str2);
375 if (!HeapFree(GetProcessHeap(), 0, str1))
376 FIXME("bad free line=%d\n", __LINE__);
380 if ((str1 = HEAP_strdupWtoA(GetProcessHeap(), 0, dci32->lpszDCIAliasName)) != NULL &&
381 (str2 = SEGPTR_STRDUP(str1)) != NULL) {
382 dci16->lpszDCIAliasName = (LPSTR)SEGPTR_GET(str2);
383 if (!HeapFree(GetProcessHeap(), 0, str1))
384 FIXME("bad free line=%d\n", __LINE__);
391 *lParam2 = (LPARAM)SEGPTR_GET(dci16);
398 if ((wMsg >= 0x800 && wMsg < 0x900) || (wMsg >= 0x4000 && wMsg < 0x4100 )) {
399 /* FIXME: another hack to handle MCI and ICM messages...
400 * should find a *NICE* way to integrate DRIVER_ and
401 * MCI_ mapping/unmapping functions
405 FIXME("Unknown message 0x%04x\n", wMsg);
411 /**************************************************************************
412 * DRIVER_UnMapMsg32To16 [internal]
414 * UnMap a 32 bit driver message to a 16 bit driver message.
417 * -2 : ko, memory problem
419 static int DRIVER_UnMapMsg32To16(WORD wMsg, DWORD lParam1, DWORD lParam2)
428 case DRV_QUERYCONFIGURE:
430 case DRV_EXITSESSION:
431 case DRV_EXITAPPLICATION:
433 /* lParam1 and lParam2 are not used */
436 /* lParam1 is a NULL terminated string, lParam2 is unknown => may lead to some problem */
437 /* lParam2 is a pointer to an MCI_OPEN_DRIVER_PARMS for an MCI device */
438 if (lParam1) if (!SEGPTR_FREE(PTR_SEG_TO_LIN(lParam1)))
439 FIXME("bad free line=%d\n", __LINE__);
441 if (lParam2 && wMsg == DRV_OPEN) {
442 LPMCI_OPEN_DRIVER_PARMS16 modp16 = (LPMCI_OPEN_DRIVER_PARMS16)PTR_SEG_TO_LIN(lParam2);
443 LPMCI_OPEN_DRIVER_PARMSA modp32 = *(LPMCI_OPEN_DRIVER_PARMSA*)((char*)modp16 - sizeof(LPMCI_OPEN_DRIVER_PARMSA));
445 modp32->wCustomCommandTable = modp16->wCustomCommandTable;
446 modp32->wType = modp16->wType;
447 if (!SEGPTR_FREE((char*)modp16 - sizeof(LPMCI_OPEN_DRIVER_PARMSA)))
448 FIXME("bad free line=%d\n", __LINE__);
454 /* lParam1 is a handle to a window (or not used), lParam2 is a pointer to DRVCONFIGINFO, lParam2 */
456 LPDRVCONFIGINFO16 dci16 = (LPDRVCONFIGINFO16)PTR_SEG_TO_LIN(lParam2);
458 if (!SEGPTR_FREE(PTR_SEG_TO_LIN(dci16->lpszDCISectionName)))
459 FIXME("bad free line=%d\n", __LINE__);
460 if (!SEGPTR_FREE(PTR_SEG_TO_LIN(dci16->lpszDCIAliasName)))
461 FIXME("bad free line=%d\n", __LINE__);
462 if (!SEGPTR_FREE(dci16))
463 FIXME("bad free line=%d\n", __LINE__);
468 if ((wMsg >= 0x800 && wMsg < 0x900) || (wMsg >= 0x4000 && wMsg < 0x4100 )) {
469 /* FIXME: another hack to handle MCI and ICM messages...
470 * should find a *NICE* way to integrate DRIVER_ and
471 * MCI_ mapping/unmapping functions
475 FIXME("Unknown message 0x%04x\n", wMsg);
481 /**************************************************************************
482 * SendDriverMessage [USER.251]
484 LRESULT WINAPI SendDriverMessage16(HDRVR16 hDriver, UINT16 msg, LPARAM lParam1,
491 TRACE("(%04x, %04X, %08lX, %08lX)\n", hDriver, msg, lParam1, lParam2);
493 lpDrv = DRIVER_FindFromHDrvr16(hDriver);
494 if (lpDrv != NULL && lpDrv->hDriver16 == hDriver) {
495 if (lpDrv->dwFlags & WINE_GDF_16BIT) {
496 TRACE("Before CallDriverProc proc=%p driverID=%08lx hDrv=%u wMsg=%04x p1=%08lx p2=%08lx\n",
497 lpDrv->d.d16.lpDrvProc, lpDrv->dwDriverID, hDriver, msg, lParam1, lParam2);
498 retval = DRIVER_CallTo16_long_lwwll((FARPROC16)lpDrv->d.d16.lpDrvProc, lpDrv->dwDriverID,
499 hDriver, msg, lParam1, lParam2);
501 mapRet = DRIVER_MapMsg16To32(msg, &lParam1, &lParam2);
503 TRACE("Before func32 call proc=%p driverID=%08lx hDrv=%u wMsg=%04x p1=%08lx p2=%08lx\n",
504 lpDrv->d.d32.lpDrvProc, lpDrv->dwDriverID, (HDRVR)lpDrv, msg, lParam1, lParam2);
505 retval = lpDrv->d.d32.lpDrvProc(lpDrv->dwDriverID, (HDRVR)lpDrv, msg, lParam1, lParam2);
507 DRIVER_UnMapMsg16To32(msg, lParam1, lParam2);
514 WARN("Bad driver handle %u\n", hDriver);
517 TRACE("retval = %ld\n", retval);
521 /**************************************************************************
522 * SendDriverMessage [WINMM.19]
524 LRESULT WINAPI SendDriverMessage(HDRVR hDriver, UINT msg, LPARAM lParam1,
531 TRACE("(%04x, %04X, %08lX, %08lX)\n", hDriver, msg, lParam1, lParam2);
533 lpDrv = DRIVER_FindFromHDrvr(hDriver);
536 if (lpDrv->dwFlags & WINE_GDF_16BIT) {
537 mapRet = DRIVER_MapMsg32To16(msg, &lParam1, &lParam2);
539 TRACE("Before CallDriverProc proc=%p driverID=%08lx hDrv=%u wMsg=%04x p1=%08lx p2=%08lx\n",
540 lpDrv->d.d16.lpDrvProc, lpDrv->dwDriverID, lpDrv->hDriver16, msg, lParam1, lParam2);
541 retval = DRIVER_CallTo16_long_lwwll((FARPROC16)lpDrv->d.d16.lpDrvProc, lpDrv->dwDriverID,
542 lpDrv->hDriver16, msg, lParam1, lParam2);
544 DRIVER_UnMapMsg32To16(msg, lParam1, lParam2);
550 TRACE("Before func32 call proc=%p driverID=%08lx hDrv=%u wMsg=%04x p1=%08lx p2=%08lx\n",
551 lpDrv->d.d32.lpDrvProc, lpDrv->dwDriverID, hDriver, msg, lParam1, lParam2);
552 retval = lpDrv->d.d32.lpDrvProc(lpDrv->dwDriverID, hDriver, msg, lParam1, lParam2);
555 WARN("Bad driver handle %u\n", hDriver);
557 TRACE("retval = %ld\n", retval);
562 /**************************************************************************
563 * DRIVER_RemoveFromList [internal]
565 * Generates all the logic to handle driver closure / deletion
566 * Removes a driver struct to the list of open drivers.
568 static BOOL DRIVER_RemoveFromList(LPWINE_DRIVER lpDrv)
570 lpDrv->dwDriverID = 0;
571 if (DRIVER_GetNumberOfModuleRefs(lpDrv) == 1) {
572 SendDriverMessage((HDRVR)lpDrv, DRV_DISABLE, 0L, 0L);
573 SendDriverMessage((HDRVR)lpDrv, DRV_FREE, 0L, 0L);
576 if (lpDrv->lpPrevItem)
577 lpDrv->lpPrevItem->lpNextItem = lpDrv->lpNextItem;
579 lpDrvItemList = lpDrv->lpNextItem;
580 if (lpDrv->lpNextItem)
581 lpDrv->lpNextItem->lpPrevItem = lpDrv->lpPrevItem;
586 /**************************************************************************
587 * DRIVER_AddToList [internal]
589 * Adds a driver struct to the list of open drivers.
590 * Generates all the logic to handle driver creation / open.
592 static BOOL DRIVER_AddToList(LPWINE_DRIVER lpNewDrv, LPARAM lParam, BOOL bCallFrom32)
594 lpNewDrv->dwMagic = WINE_DI_MAGIC;
595 /* First driver to be loaded for this module, need to load correctly the module */
596 if (DRIVER_GetNumberOfModuleRefs(lpNewDrv) == 0) {
597 if (SendDriverMessage((HDRVR)lpNewDrv, DRV_LOAD, 0L, 0L) != DRV_SUCCESS) {
598 TRACE("DRV_LOAD failed on driver 0x%08lx\n", (DWORD)lpNewDrv);
601 if (SendDriverMessage((HDRVR)lpNewDrv, DRV_ENABLE, 0L, 0L) != DRV_SUCCESS) {
602 TRACE("DRV_ENABLE failed on driver 0x%08lx\n", (DWORD)lpNewDrv);
607 lpNewDrv->lpNextItem = NULL;
608 if (lpDrvItemList == NULL) {
609 lpDrvItemList = lpNewDrv;
610 lpNewDrv->lpPrevItem = NULL;
612 LPWINE_DRIVER lpDrv = lpDrvItemList; /* find end of list */
613 while (lpDrv->lpNextItem != NULL)
614 lpDrv = lpDrv->lpNextItem;
616 lpDrv->lpNextItem = lpNewDrv;
617 lpNewDrv->lpPrevItem = lpDrv;
619 /* Now just open a new instance of a driver on this module */
621 lpNewDrv->dwDriverID = SendDriverMessage((HDRVR)lpNewDrv, DRV_OPEN, 0L, lParam);
623 lpNewDrv->dwDriverID = SendDriverMessage16(lpNewDrv->hDriver16, DRV_OPEN, 0L, lParam);
625 if (lpNewDrv->dwDriverID == 0) {
626 TRACE("DRV_OPEN failed on driver 0x%08lx\n", (DWORD)lpNewDrv);
627 DRIVER_RemoveFromList(lpNewDrv);
634 /**************************************************************************
635 * DRIVER_CreateDrvr16 [internal]
637 * Creates unique ID for 16 bit drivers.
639 static HDRVR16 DRIVER_CreateDrvr16(void)
641 static WORD DRIVER_hDrvr16Counter = 0;
643 while (DRIVER_FindFromHDrvr16(++DRIVER_hDrvr16Counter));
644 return DRIVER_hDrvr16Counter;
647 /**************************************************************************
648 * DRIVER_CloseDriver [internal]
651 BOOL DRIVER_CloseDriver(LPWINE_DRIVER lpDrv, DWORD lParam1, DWORD lParam2)
654 SendDriverMessage((HDRVR)lpDrv, DRV_CLOSE, lParam1, lParam2);
656 if (DRIVER_RemoveFromList(lpDrv)) {
657 HeapFree(GetProcessHeap(), 0, lpDrv);
661 WARN("Failed to close driver\n");
665 /**************************************************************************
666 * DRIVER_RegisterDriver16 [internal]
668 * Creates all the WINE internal representations for a 16 bit driver.
669 * The driver is also open by sending the correct messages.
671 LPWINE_DRIVER DRIVER_RegisterDriver16(LPCSTR lpName, HMODULE16 hModule, DRIVERPROC16 lpProc,
672 LPARAM lParam, BOOL bCallFrom32)
676 lpDrv = HeapAlloc(GetProcessHeap(), 0, sizeof(WINE_DRIVER));
678 lpDrv->dwFlags = WINE_GDF_EXIST|WINE_GDF_16BIT;
679 lpDrv->dwDriverID = 0;
680 lpDrv->hDriver16 = DRIVER_CreateDrvr16();
681 lstrcpynA(lpDrv->szAliasName, lpName, sizeof(lpDrv->szAliasName));
682 lpDrv->d.d16.hModule = hModule;
683 lpDrv->d.d16.lpDrvProc = lpProc;
685 if (!DRIVER_AddToList(lpDrv, lParam, bCallFrom32)) {
686 HeapFree(GetProcessHeap(), 0, lpDrv);
693 /**************************************************************************
694 * DRIVER_RegisterDriver32 [internal]
696 * Creates all the WINE internal representations for a 32 bit driver.
697 * The driver is also open by sending the correct messages.
699 LPWINE_DRIVER DRIVER_RegisterDriver32(LPCSTR lpName, HMODULE hModule, DRIVERPROC lpProc,
700 LPARAM lParam, BOOL bCallFrom32)
704 lpDrv = HeapAlloc(GetProcessHeap(), 0, sizeof(WINE_DRIVER));
706 lpDrv->dwFlags = WINE_GDF_EXIST;
707 lpDrv->dwDriverID = 0;
708 lpDrv->hDriver16 = DRIVER_CreateDrvr16();
709 lstrcpynA(lpDrv->szAliasName, lpName, sizeof(lpDrv->szAliasName));
710 lpDrv->d.d32.hModule = hModule;
711 lpDrv->d.d32.lpDrvProc = lpProc;
713 if (!DRIVER_AddToList(lpDrv, lParam, bCallFrom32)) {
714 HeapFree(GetProcessHeap(), 0, lpDrv);
721 /**************************************************************************
722 * DRIVER_TryOpenDriver16 [internal]
724 * Tries to load a 16 bit driver whose DLL's (module) name is lpFileName.
726 static HDRVR16 DRIVER_TryOpenDriver16(LPCSTR lpFileName, LPARAM lParam, BOOL bCallFrom32)
728 LPWINE_DRIVER lpDrv = NULL;
733 TRACE("('%s', %08lX, %d);\n", lpFileName, lParam, bCallFrom32);
735 if (strlen(lpFileName) < 1)
738 lpSFN = strrchr(lpFileName, '\\');
739 lpSFN = (lpSFN) ? (lpSFN + 1) : lpFileName;
741 if ((hModule = LoadModule16(lpFileName, (LPVOID)-1)) >= 32) {
742 if ((lpProc = (DRIVERPROC16)WIN32_GetProcAddress16(hModule, "DRIVERPROC")) != NULL) {
743 lpDrv = DRIVER_RegisterDriver16(lpSFN, hModule, lpProc, lParam, bCallFrom32);
745 FreeLibrary16(hModule);
746 TRACE("No DriverProc found\n");
750 TRACE("Unable to load 16 bit module (%s): %d\n", lpFileName, hModule);
752 return lpDrv ? lpDrv->hDriver16 : 0;
755 /**************************************************************************
756 * DRIVER_TryOpenDriver32 [internal]
758 * Tries to load a 32 bit driver whose DLL's (module) name is lpFileName.
760 static HDRVR DRIVER_TryOpenDriver32(LPCSTR lpFileName, LPARAM lParam, BOOL bCallFrom32)
762 LPWINE_DRIVER lpDrv = NULL;
767 TRACE("('%s', %08lX, %d);\n", lpFileName, lParam, bCallFrom32);
769 if (strlen(lpFileName) < 1)
772 lpSFN = strrchr(lpFileName, '\\');
773 lpSFN = (lpSFN) ? (lpSFN + 1) : lpFileName;
775 if ((hModule = LoadLibraryA(lpFileName)) != 0) {
776 if ((lpProc = GetProcAddress(hModule, "DriverProc")) != NULL) {
777 lpDrv = DRIVER_RegisterDriver32(lpSFN, hModule, lpProc, lParam, bCallFrom32);
779 FreeLibrary(hModule);
781 TRACE("No DriverProc found\n");
784 TRACE("Unable to load 32 bit module \"%s\"\n", lpFileName);
786 TRACE("=> %p\n", lpDrv);
790 /**************************************************************************
791 * OpenDriver16 [USER.252]
793 HDRVR16 WINAPI OpenDriver16(LPCSTR lpDriverName, LPCSTR lpSectionName, LPARAM lParam)
798 TRACE("('%s', '%s', %08lX);\n", lpDriverName, lpSectionName, lParam);
800 if (lpSectionName == NULL) {
801 hDriver = DRIVER_TryOpenDriver16(lpDriverName, lParam, FALSE);
803 hDriver = DRIVER_TryOpenDriver32(lpDriverName, lParam, FALSE);
806 /* in case hDriver is NULL, search in Drivers32 section */
807 lpSectionName = "Drivers";
810 if (!hDriver && GetPrivateProfileStringA(lpSectionName, lpDriverName, "",
811 drvName, sizeof(drvName), "SYSTEM.INI") > 0) {
812 hDriver = DRIVER_TryOpenDriver16(drvName, lParam, FALSE);
815 ERR("Failed to open driver %s from system.ini file, section %s\n", lpDriverName, lpSectionName);
817 TRACE("=> %08x\n", hDriver);
821 /**************************************************************************
822 * OpenDriverA [WINMM.15]
823 * (0,1,DRV_LOAD ,0 ,0)
824 * (0,1,DRV_ENABLE,0 ,0)
825 * (0,1,DRV_OPEN ,buf[256],0)
827 HDRVR WINAPI OpenDriverA(LPCSTR lpDriverName, LPCSTR lpSectionName, LPARAM lParam)
832 TRACE("(%s, %s, 0x%08lx);\n", debugstr_a(lpDriverName), debugstr_a(lpSectionName), lParam);
834 if (lpSectionName == NULL) {
835 lstrcpynA(drvName, lpDriverName, sizeof(drvName));
836 hDriver = DRIVER_TryOpenDriver32(lpDriverName, lParam, TRUE);
838 hDriver = DRIVER_TryOpenDriver16(lpDriverName, lParam, TRUE);
841 if (GetPrivateProfileStringA("Drivers32", lpDriverName, "", drvName,
842 sizeof(drvName), "SYSTEM.INI")) {
843 hDriver = DRIVER_TryOpenDriver32(drvName, lParam, TRUE);
848 if (GetPrivateProfileStringA("Drivers", lpDriverName, "", drvName,
849 sizeof(drvName), "SYSTEM.INI")) {
850 hDriver = DRIVER_TryOpenDriver16(drvName, lParam, TRUE);
855 if (GetPrivateProfileStringA(lpSectionName, lpDriverName, "", drvName,
856 sizeof(drvName), "SYSTEM.INI")) {
857 hDriver = DRIVER_TryOpenDriver32(drvName, lParam, TRUE);
859 hDriver = DRIVER_TryOpenDriver16(drvName, lParam, TRUE);
864 ERR("Failed to open driver %s from section %s\n", lpDriverName, lpSectionName);
866 TRACE("=> %08x\n", hDriver);
870 /**************************************************************************
871 * OpenDriverW [WINMM.15]
873 HDRVR WINAPI OpenDriverW(LPCWSTR lpDriverName, LPCWSTR lpSectionName, LPARAM lParam)
875 LPSTR dn = HEAP_strdupWtoA(GetProcessHeap(), 0, lpDriverName);
876 LPSTR sn = HEAP_strdupWtoA(GetProcessHeap(), 0, lpSectionName);
877 HDRVR ret = OpenDriverA(dn, sn, lParam);
879 if (dn) HeapFree(GetProcessHeap(), 0, dn);
880 if (sn) HeapFree(GetProcessHeap(), 0, sn);
884 /**************************************************************************
885 * CloseDriver16 [USER.253]
887 LRESULT WINAPI CloseDriver16(HDRVR16 hDrvr, LPARAM lParam1, LPARAM lParam2)
889 TRACE("(%04x, %08lX, %08lX);\n", hDrvr, lParam1, lParam2);
891 return DRIVER_CloseDriver(DRIVER_FindFromHDrvr16(hDrvr), lParam1, lParam2);
894 /**************************************************************************
895 * CloseDriver [WINMM.4]
897 LRESULT WINAPI CloseDriver(HDRVR hDrvr, LPARAM lParam1, LPARAM lParam2)
899 TRACE("(%04x, %08lX, %08lX);\n", hDrvr, lParam1, lParam2);
901 return DRIVER_CloseDriver(DRIVER_FindFromHDrvr(hDrvr), lParam1, lParam2);
904 /**************************************************************************
905 * GetDriverModuleHandle [USER.254]
907 HMODULE16 WINAPI GetDriverModuleHandle16(HDRVR16 hDrvr)
910 HMODULE16 hModule = 0;
912 TRACE("(%04x);\n", hDrvr);
914 lpDrv = DRIVER_FindFromHDrvr16(hDrvr);
915 if (lpDrv != NULL && lpDrv->hDriver16 == hDrvr && (lpDrv->dwFlags & WINE_GDF_16BIT)) {
916 hModule = lpDrv->d.d16.hModule;
918 TRACE("=> %d\n", hModule);
922 /**************************************************************************
923 * GetDriverFlags [WINMM.13]
924 * [in] hDrvr handle to the driver
927 * 0x00000000 if hDrvr is an invalid handle
928 * 0x80000000 if hDrvr is a valid 32 bit driver
929 * 0x90000000 if hDrvr is a valid 16 bit driver
931 DWORD WINAPI GetDriverFlags(HDRVR hDrvr)
936 TRACE("(%04x)\n", hDrvr);
938 if ((lpDrv = DRIVER_FindFromHDrvr(hDrvr)) != NULL) {
939 ret = lpDrv->dwFlags;
944 /**************************************************************************
945 * GetDriverModuleHandle [WINMM.14]
947 HMODULE WINAPI GetDriverModuleHandle(HDRVR hDrvr)
952 TRACE("(%04x);\n", hDrvr);
954 lpDrv = DRIVER_FindFromHDrvr(hDrvr);
955 if (lpDrv != NULL && !(lpDrv->dwFlags & WINE_GDF_16BIT)) {
956 hModule = lpDrv->d.d32.hModule;
958 TRACE("=> %d\n", hModule);
962 /**************************************************************************
963 * DefDriverProc16 [USER.255]
965 LRESULT WINAPI DefDriverProc16(DWORD dwDevID, HDRVR16 hDriv, UINT16 wMsg,
966 LPARAM lParam1, LPARAM lParam2)
968 TRACE("devID=0x%08lx hDrv=0x%04x wMsg=%04x lP1=0x%08lx lP2=0x%08lx\n",
969 dwDevID, hDriv, wMsg, lParam1, lParam2);
979 case DRV_QUERYCONFIGURE:
982 MessageBoxA(0, "Driver isn't configurable !", "Wine Driver", MB_OK);
992 /**************************************************************************
993 * GetDriverInfo [USER.256]
995 BOOL16 WINAPI GetDriverInfo16(HDRVR16 hDrvr, LPDRIVERINFOSTRUCT16 lpDrvInfo)
1000 TRACE("(%04x, %p);\n", hDrvr, lpDrvInfo);
1002 if (lpDrvInfo == NULL || lpDrvInfo->length != sizeof(DRIVERINFOSTRUCT16))
1005 lpDrv = DRIVER_FindFromHDrvr16(hDrvr);
1006 if (lpDrv != NULL && lpDrv->hDriver16 == hDrvr &&
1007 (lpDrv->dwFlags & WINE_GDF_16BIT)) {
1008 lpDrvInfo->hDriver = lpDrv->hDriver16;
1009 lpDrvInfo->hModule = lpDrv->d.d16.hModule;
1010 lstrcpynA(lpDrvInfo->szAliasName, lpDrv->szAliasName, sizeof(lpDrvInfo->szAliasName));
1017 /**************************************************************************
1018 * GetNextDriver [USER.257]
1020 HDRVR16 WINAPI GetNextDriver16(HDRVR16 hDrvr, DWORD dwFlags)
1022 HDRVR16 hRetDrv = 0;
1023 LPWINE_DRIVER lpDrv;
1025 TRACE("(%04x, %08lX);\n", hDrvr, dwFlags);
1028 if (lpDrvItemList == NULL) {
1029 FIXME("drivers list empty !\n");
1030 /* FIXME: code was using DRIVER_LoadStartupDrivers(); before ?
1031 * I (EPP) don't quite understand this
1033 if (lpDrvItemList == NULL)
1036 lpDrv = lpDrvItemList;
1037 if (dwFlags & GND_REVERSE) {
1038 while (lpDrv->lpNextItem)
1039 lpDrv = lpDrv->lpNextItem;
1042 lpDrv = DRIVER_FindFromHDrvr16(hDrvr);
1043 if (lpDrv != NULL) {
1044 if (dwFlags & GND_REVERSE) {
1045 lpDrv = (lpDrv->lpPrevItem) ? lpDrv->lpPrevItem : NULL;
1047 lpDrv = (lpDrv->lpNextItem) ? lpDrv->lpNextItem : NULL;
1052 hRetDrv = (lpDrv) ? lpDrv->hDriver16 : (HDRVR16)0;
1053 TRACE("return %04x !\n", hRetDrv);