Converted comcat.h to IDL.
[wine/multimedia.git] / dlls / setupapi / setupx_main.c
blob4daa621cc3e02600a2c4245fac9b6b3c9aa25c9f
1 /*
2 * SETUPX library
4 * Copyright 1998,2000 Andreas Mohr
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 * FIXME: Rather non-functional functions for now.
22 * See:
23 * http://www.geocities.com/SiliconValley/Network/5317/drivers.html
24 * http://willemer.de/informatik/windows/inf_info.htm (German)
25 * http://www.microsoft.com/ddk/ddkdocs/win98ddk/devinst_12uw.htm
26 * DDK: setupx.h
27 * http://mmatrix.tripod.com/customsystemfolder/infsysntaxfull.html
28 * http://www.rdrop.com/~cary/html/inf_faq.html
29 * http://support.microsoft.com/support/kb/articles/q194/6/40.asp
31 * Stuff tested with:
32 * - rs405deu.exe (German Acroread 4.05 setup)
33 * - ie5setup.exe
34 * - Netmeeting
36 * FIXME:
37 * - string handling is... weird ;) (buflen etc.)
38 * - memory leaks ?
39 * - separate that mess (but probably only when it's done completely)
41 * SETUPX consists of several parts with the following acronyms/prefixes:
42 * Di device installer (devinst.c ?)
43 * Gen generic installer (geninst.c ?)
44 * Ip .INF parsing (infparse.c)
45 * LDD logical device descriptor (ldd.c ?)
46 * LDID logical device ID
47 * SU setup (setup.c ?)
48 * Tp text processing (textproc.c ?)
49 * Vcp virtual copy module (vcp.c ?)
50 * ...
52 * The SETUPX DLL is NOT thread-safe. That's why many installers urge you to
53 * "close all open applications".
54 * All in all the design of it seems to be a bit weak.
55 * Not sure whether my implementation of it is better, though ;-)
58 #include <stdlib.h>
59 #include <stdio.h>
60 #include <string.h>
61 #include "winreg.h"
62 #include "winerror.h"
63 #include "wine/winuser16.h"
64 #include "wownt32.h"
65 #include "setupapi.h"
66 #include "setupx16.h"
67 #include "setupapi_private.h"
68 #include "winerror.h"
69 #include "wine/debug.h"
71 WINE_DEFAULT_DEBUG_CHANNEL(setupapi);
73 /***********************************************************************
74 * SURegOpenKey (SETUPX.47)
76 DWORD WINAPI SURegOpenKey( HKEY hkey, LPCSTR lpszSubKey, PHKEY retkey )
78 FIXME("(%p,%s,%p), semi-stub.\n",hkey,debugstr_a(lpszSubKey),retkey);
79 return RegOpenKeyA( hkey, lpszSubKey, retkey );
82 /***********************************************************************
83 * SURegQueryValueEx (SETUPX.50)
85 DWORD WINAPI SURegQueryValueEx( HKEY hkey, LPSTR lpszValueName,
86 LPDWORD lpdwReserved, LPDWORD lpdwType,
87 LPBYTE lpbData, LPDWORD lpcbData )
89 FIXME("(%p,%s,%p,%p,%p,%ld), semi-stub.\n",hkey,debugstr_a(lpszValueName),
90 lpdwReserved,lpdwType,lpbData,lpcbData?*lpcbData:0);
91 return RegQueryValueExA( hkey, lpszValueName, lpdwReserved, lpdwType,
92 lpbData, lpcbData );
96 * Returns pointer to a string list with the first entry being number
97 * of strings.
99 * Hmm. Should this be InitSubstrData(), GetFirstSubstr() and GetNextSubstr()
100 * instead?
102 static LPSTR *SETUPX_GetSubStrings(LPSTR start, char delimiter)
104 LPSTR p, q;
105 LPSTR *res = NULL;
106 DWORD count = 0;
107 int len;
109 p = start;
111 while (1)
113 /* find beginning of real substring */
114 while ( (*p == ' ') || (*p == '\t') || (*p == '"') ) p++;
116 /* find end of real substring */
117 q = p;
118 while ( (*q)
119 && (*q != ' ') && (*q != '\t') && (*q != '"')
120 && (*q != ';') && (*q != delimiter) ) q++;
121 if (q == p)
122 break;
123 len = (int)q - (int)p;
125 /* alloc entry for new substring in steps of 32 units and copy over */
126 if (count % 32 == 0)
127 { /* 1 for count field + current count + 32 */
128 res = HeapReAlloc(GetProcessHeap(), 0, res, (1+count+32)*sizeof(LPSTR));
130 *(res+1+count) = HeapAlloc(GetProcessHeap(), 0, len+1);
131 strncpy(*(res+1+count), p, len);
132 (*(res+1+count))[len] = '\0';
133 count++;
135 /* we are still within last substring (before delimiter),
136 * so get out of it */
137 while ((*q) && (*q != ';') && (*q != delimiter)) q++;
138 if ((!*q) || (*q == ';'))
139 break;
140 p = q+1;
143 /* put number of entries at beginning of list */
144 *(DWORD *)res = count;
145 return res;
148 static void SETUPX_FreeSubStrings(LPSTR *substr)
150 DWORD count = *(DWORD *)substr;
151 LPSTR *pStrings = substr+1;
152 DWORD n;
154 for (n=0; n < count; n++)
155 HeapFree(GetProcessHeap(), 0, *pStrings++);
157 HeapFree(GetProcessHeap(), 0, substr);
161 /***********************************************************************
162 * InstallHinfSection (SETUPX.527)
164 * hwnd = parent window
165 * hinst = instance of SETUPX.DLL
166 * lpszCmdLine = e.g. "DefaultInstall 132 C:\MYINSTALL\MYDEV.INF"
167 * Here "DefaultInstall" is the .inf file section to be installed (optional).
168 * The 132 value is made of the HOW_xxx flags and sometimes 128 (-> setupx16.h).
170 * nCmdShow = nCmdShow of CreateProcess
172 RETERR16 WINAPI InstallHinfSection16( HWND16 hwnd, HINSTANCE16 hinst, LPCSTR lpszCmdLine, INT16 nCmdShow)
174 LPSTR *pSub;
175 DWORD count;
176 HINF16 hInf = 0;
177 RETERR16 res = OK, tmp;
178 WORD wFlags;
179 BOOL reboot = FALSE;
181 TRACE("(%04x, %04x, %s, %d);\n", hwnd, hinst, lpszCmdLine, nCmdShow);
183 pSub = SETUPX_GetSubStrings((LPSTR)lpszCmdLine, ' ');
185 count = *(DWORD *)pSub;
186 if (count < 2) /* invalid number of arguments ? */
187 goto end;
188 if (IpOpen16(*(pSub+count), &hInf) != OK)
190 res = ERROR_FILE_NOT_FOUND; /* yes, correct */
191 goto end;
193 if (VcpOpen16(NULL, 0))
194 goto end;
195 if (GenInstall16(hInf, *(pSub+count-2), GENINSTALL_DO_ALL) != OK)
196 goto end;
197 wFlags = atoi(*(pSub+count-1)) & ~128;
198 switch (wFlags)
200 case HOW_ALWAYS_SILENT_REBOOT:
201 case HOW_SILENT_REBOOT:
202 reboot = TRUE;
203 break;
204 case HOW_ALWAYS_PROMPT_REBOOT:
205 case HOW_PROMPT_REBOOT:
206 if (MessageBoxA(HWND_32(hwnd), "You must restart Wine before the new settings will take effect.\n\nDo you want to exit Wine now ?", "Systems Settings Change", MB_YESNO|MB_ICONQUESTION) == IDYES)
207 reboot = TRUE;
208 break;
209 default:
210 ERR("invalid flags %d !\n", wFlags);
211 goto end;
214 res = OK;
215 end:
216 tmp = VcpClose16(VCPFL_ALL, NULL);
217 if (tmp != OK)
218 res = tmp;
219 tmp = IpClose16(hInf);
220 if (tmp != OK)
221 res = tmp;
222 SETUPX_FreeSubStrings(pSub);
223 if (reboot)
225 /* FIXME: we should have a means of terminating all wine + wineserver */
226 MESSAGE("Program or user told me to restart. Exiting Wine...\n");
227 ExitProcess(1);
230 return res;
233 typedef struct
235 LPCSTR RegValName;
236 LPCSTR StdString; /* fallback string; sub dir of windows directory */
237 } LDID_DATA;
239 static const LDID_DATA LDID_Data[34] =
241 { /* 0 (LDID_NULL) -- not defined */
242 NULL,
243 NULL
245 { /* 1 (LDID_SRCPATH) = source of installation. hmm, what to do here ? */
246 "SourcePath", /* hmm, does SETUPX have to care about updating it ?? */
247 NULL
249 { /* 2 (LDID_SETUPTEMP) = setup temp dir */
250 "SetupTempDir",
251 NULL
253 { /* 3 (LDID_UNINSTALL) = uninstall backup dir */
254 "UninstallDir",
255 NULL
257 { /* 4 (LDID_BACKUP) = backup dir */
258 "BackupDir",
259 NULL
261 { /* 5 (LDID_SETUPSCRATCH) = setup scratch dir */
262 "SetupScratchDir",
263 NULL
265 { /* 6 -- not defined */
266 NULL,
267 NULL
269 { /* 7 -- not defined */
270 NULL,
271 NULL
273 { /* 8 -- not defined */
274 NULL,
275 NULL
277 { /* 9 -- not defined */
278 NULL,
279 NULL
281 { /* 10 (LDID_WIN) = windows dir */
282 "WinDir",
285 { /* 11 (LDID_SYS) = system dir */
286 "SysDir",
287 NULL /* call GetSystemDirectory() instead */
289 { /* 12 (LDID_IOS) = IOSubSys dir */
290 NULL, /* FIXME: registry string ? */
291 "SYSTEM\\IOSUBSYS"
293 { /* 13 (LDID_CMD) = COMMAND dir */
294 NULL, /* FIXME: registry string ? */
295 "COMMAND"
297 { /* 14 (LDID_CPL) = control panel dir */
298 NULL,
301 { /* 15 (LDID_PRINT) = windows printer dir */
302 NULL,
303 "SYSTEM" /* correct ?? */
305 { /* 16 (LDID_MAIL) = destination mail dir */
306 NULL,
309 { /* 17 (LDID_INF) = INF dir */
310 "SetupScratchDir", /* correct ? */
311 "INF"
313 { /* 18 (LDID_HELP) = HELP dir */
314 NULL, /* ??? */
315 "HELP"
317 { /* 19 (LDID_WINADMIN) = Admin dir */
318 "WinAdminDir",
321 { /* 20 (LDID_FONTS) = Fonts dir */
322 NULL, /* ??? */
323 "FONTS"
325 { /* 21 (LDID_VIEWERS) = Viewers */
326 NULL, /* ??? */
327 "SYSTEM\\VIEWERS"
329 { /* 22 (LDID_VMM32) = VMM32 dir */
330 NULL, /* ??? */
331 "SYSTEM\\VMM32"
333 { /* 23 (LDID_COLOR) = ICM dir */
334 "ICMPath",
335 "SYSTEM\\COLOR"
337 { /* 24 (LDID_APPS) = root of boot drive ? */
338 "AppsDir",
339 "C:\\"
341 { /* 25 (LDID_SHARED) = shared dir */
342 "SharedDir",
345 { /* 26 (LDID_WINBOOT) = Windows boot dir */
346 "WinBootDir",
349 { /* 27 (LDID_MACHINE) = machine specific files */
350 "MachineDir",
351 NULL
353 { /* 28 (LDID_HOST_WINBOOT) = Host Windows boot dir */
354 "HostWinBootDir",
355 NULL
357 { /* 29 -- not defined */
358 NULL,
359 NULL
361 { /* 30 (LDID_BOOT) = Root of boot drive */
362 "BootDir",
363 NULL
365 { /* 31 (LDID_BOOT_HOST) = Root of boot drive host */
366 "BootHost",
367 NULL
369 { /* 32 (LDID_OLD_WINBOOT) = subdir of root */
370 "OldWinBootDir",
371 NULL
373 { /* 33 (LDID_OLD_WIN) = old win dir */
374 "OldWinDir",
375 NULL
377 /* the rest (34-38) isn't too interesting, so I'll forget about it */
381 * LDD == Logical Device Descriptor
382 * LDID == Logical Device ID
384 * The whole LDD/LDID business might go into a separate file named
385 * ldd.c.
386 * At the moment I don't know what the hell these functions are really doing.
387 * That's why I added reporting stubs.
388 * The only thing I do know is that I need them for the LDD/LDID infrastructure.
389 * That's why I implemented them in a way that's suitable for my purpose.
391 static LDD_LIST *pFirstLDD = NULL;
393 static BOOL std_LDDs_done = FALSE;
395 void SETUPX_CreateStandardLDDs(void)
397 HKEY hKey = 0;
398 WORD n;
399 DWORD type, len;
400 LOGDISKDESC_S ldd;
401 char buffer[MAX_PATH];
403 /* has to be here, otherwise loop */
404 std_LDDs_done = TRUE;
406 RegOpenKeyA(HKEY_LOCAL_MACHINE, "SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Setup", &hKey);
408 for (n=0; n < sizeof(LDID_Data)/sizeof(LDID_DATA); n++)
410 buffer[0] = '\0';
412 len = MAX_PATH;
413 if ( (hKey) && (LDID_Data[n].RegValName)
414 && (RegQueryValueExA(hKey, LDID_Data[n].RegValName,
415 NULL, &type, buffer, &len) == ERROR_SUCCESS)
416 && (type == REG_SZ) )
418 TRACE("found value '%s' for LDID %d\n", buffer, n);
420 else
421 switch(n)
423 case LDID_SRCPATH:
424 FIXME("LDID_SRCPATH: what exactly do we have to do here ?\n");
425 strcpy(buffer, "X:\\FIXME");
426 break;
427 case LDID_SYS:
428 GetSystemDirectoryA(buffer, MAX_PATH);
429 break;
430 case LDID_APPS:
431 case LDID_MACHINE:
432 case LDID_HOST_WINBOOT:
433 case LDID_BOOT:
434 case LDID_BOOT_HOST:
435 strcpy(buffer, "C:\\");
436 break;
437 default:
438 if (LDID_Data[n].StdString)
440 DWORD len = GetWindowsDirectoryA(buffer, MAX_PATH);
441 LPSTR p;
442 p = buffer + len;
443 *p++ = '\\';
444 strcpy(p, LDID_Data[n].StdString);
446 break;
448 if (buffer[0])
450 INIT_LDD(ldd, n);
451 ldd.pszPath = buffer;
452 TRACE("LDID %d -> '%s'\n", ldd.ldid, ldd.pszPath);
453 CtlSetLdd16(&ldd);
456 if (hKey) RegCloseKey(hKey);
459 /***********************************************************************
460 * CtlDelLdd (SETUPX.37)
462 * RETURN
463 * ERR_VCP_LDDINVALID if ldid < LDID_ASSIGN_START.
465 RETERR16 SETUPX_DelLdd(LOGDISKID16 ldid)
467 LDD_LIST *pCurr, *pPrev = NULL;
469 TRACE("(%d)\n", ldid);
471 if (!std_LDDs_done)
472 SETUPX_CreateStandardLDDs();
474 if (ldid < LDID_ASSIGN_START)
475 return ERR_VCP_LDDINVALID;
477 pCurr = pFirstLDD;
478 /* search until we find the appropriate LDD or hit the end */
479 while ((pCurr != NULL) && (ldid > pCurr->pldd->ldid))
481 pPrev = pCurr;
482 pCurr = pCurr->next;
484 if ( (pCurr == NULL) /* hit end of list */
485 || (ldid != pCurr->pldd->ldid) )
486 return ERR_VCP_LDDFIND; /* correct ? */
488 /* ok, found our victim: eliminate it */
490 if (pPrev)
491 pPrev->next = pCurr->next;
493 if (pCurr == pFirstLDD)
494 pFirstLDD = NULL;
495 HeapFree(GetProcessHeap(), 0, pCurr);
497 return OK;
500 /***********************************************************************
501 * CtlDelLdd (SETUPX.37)
503 RETERR16 WINAPI CtlDelLdd16(LOGDISKID16 ldid)
505 FIXME("(%d); - please report this!\n", ldid);
506 return SETUPX_DelLdd(ldid);
509 /***********************************************************************
510 * CtlFindLdd (SETUPX.35)
512 * doesn't check pldd ptr validity: crash (W98SE)
514 * RETURN
515 * ERR_VCP_LDDINVALID if pldd->cbSize != structsize
516 * 1 in all other cases ??
519 RETERR16 WINAPI CtlFindLdd16(LPLOGDISKDESC pldd)
521 LDD_LIST *pCurr, *pPrev = NULL;
523 TRACE("(%p)\n", pldd);
525 if (!std_LDDs_done)
526 SETUPX_CreateStandardLDDs();
528 if (pldd->cbSize != sizeof(LOGDISKDESC_S))
529 return ERR_VCP_LDDINVALID;
531 pCurr = pFirstLDD;
532 /* search until we find the appropriate LDD or hit the end */
533 while ((pCurr != NULL) && (pldd->ldid > pCurr->pldd->ldid))
535 pPrev = pCurr;
536 pCurr = pCurr->next;
538 if ( (pCurr == NULL) /* hit end of list */
539 || (pldd->ldid != pCurr->pldd->ldid) )
540 return ERR_VCP_LDDFIND; /* correct ? */
542 memcpy(pldd, pCurr->pldd, pldd->cbSize);
543 /* hmm, we probably ought to strcpy() the string ptrs here */
545 return 1; /* what is this ?? */
548 /***********************************************************************
549 * CtlSetLdd (SETUPX.33)
551 * Set an LDD entry.
553 * RETURN
554 * ERR_VCP_LDDINVALID if pldd.cbSize != sizeof(LOGDISKDESC_S)
557 RETERR16 WINAPI CtlSetLdd16(LPLOGDISKDESC pldd)
559 LDD_LIST *pCurr, *pPrev = NULL;
560 LPLOGDISKDESC pCurrLDD;
561 HANDLE heap;
562 BOOL is_new = FALSE;
564 TRACE("(%p)\n", pldd);
566 if (!std_LDDs_done)
567 SETUPX_CreateStandardLDDs();
569 if (pldd->cbSize != sizeof(LOGDISKDESC_S))
570 return ERR_VCP_LDDINVALID;
572 heap = GetProcessHeap();
573 pCurr = pFirstLDD;
574 /* search until we find the appropriate LDD or hit the end */
575 while ((pCurr != NULL) && (pldd->ldid > pCurr->pldd->ldid))
577 pPrev = pCurr;
578 pCurr = pCurr->next;
580 if (!pCurr || pldd->ldid != pCurr->pldd->ldid)
582 is_new = TRUE;
583 pCurr = HeapAlloc(heap, 0, sizeof(LDD_LIST));
584 pCurr->pldd = HeapAlloc(heap, 0, sizeof(LOGDISKDESC_S));
585 pCurr->next = NULL;
586 pCurrLDD = pCurr->pldd;
588 else
590 pCurrLDD = pCurr->pldd;
591 if (pCurrLDD->pszPath) HeapFree(heap, 0, pCurrLDD->pszPath);
592 if (pCurrLDD->pszVolLabel) HeapFree(heap, 0, pCurrLDD->pszVolLabel);
593 if (pCurrLDD->pszDiskName) HeapFree(heap, 0, pCurrLDD->pszDiskName);
596 memcpy(pCurrLDD, pldd, sizeof(LOGDISKDESC_S));
598 if (pldd->pszPath)
600 pCurrLDD->pszPath = HeapAlloc( heap, 0, strlen(pldd->pszPath)+1 );
601 strcpy( pCurrLDD->pszPath, pldd->pszPath );
603 if (pldd->pszVolLabel)
605 pCurrLDD->pszVolLabel = HeapAlloc( heap, 0, strlen(pldd->pszVolLabel)+1 );
606 strcpy( pCurrLDD->pszVolLabel, pldd->pszVolLabel );
608 if (pldd->pszDiskName)
610 pCurrLDD->pszDiskName = HeapAlloc( heap, 0, strlen(pldd->pszDiskName)+1 );
611 strcpy( pCurrLDD->pszDiskName, pldd->pszDiskName );
614 if (is_new) /* link into list */
616 if (pPrev)
618 pCurr->next = pPrev->next;
619 pPrev->next = pCurr;
621 if (!pFirstLDD)
622 pFirstLDD = pCurr;
625 return OK;
629 /***********************************************************************
630 * CtlAddLdd (SETUPX.36)
632 * doesn't check pldd ptr validity: crash (W98SE)
635 static LOGDISKID16 ldid_to_add = LDID_ASSIGN_START;
636 RETERR16 WINAPI CtlAddLdd16(LPLOGDISKDESC pldd)
638 pldd->ldid = ldid_to_add++;
639 return CtlSetLdd16(pldd);
642 /***********************************************************************
643 * CtlGetLdd (SETUPX.34)
645 * doesn't check pldd ptr validity: crash (W98SE)
646 * What the !@#$%&*( is the difference between CtlFindLdd() and CtlGetLdd() ??
648 * RETURN
649 * ERR_VCP_LDDINVALID if pldd->cbSize != structsize
652 static RETERR16 SETUPX_GetLdd(LPLOGDISKDESC pldd)
654 LDD_LIST *pCurr, *pPrev = NULL;
656 if (!std_LDDs_done)
657 SETUPX_CreateStandardLDDs();
659 if (pldd->cbSize != sizeof(LOGDISKDESC_S))
660 return ERR_VCP_LDDINVALID;
662 pCurr = pFirstLDD;
663 /* search until we find the appropriate LDD or hit the end */
664 while ((pCurr != NULL) && (pldd->ldid > pCurr->pldd->ldid))
666 pPrev = pCurr;
667 pCurr = pCurr->next;
669 if (pCurr == NULL) /* hit end of list */
670 return ERR_VCP_LDDFIND; /* correct ? */
672 memcpy(pldd, pCurr->pldd, pldd->cbSize);
673 /* hmm, we probably ought to strcpy() the string ptrs here */
675 return OK;
678 /**********************************************************************/
680 RETERR16 WINAPI CtlGetLdd16(LPLOGDISKDESC pldd)
682 FIXME("(%p); - please report this!\n", pldd);
683 return SETUPX_GetLdd(pldd);
686 /***********************************************************************
687 * CtlGetLddPath (SETUPX.38)
689 * Gets the path of an LDD.
690 * No crash if szPath == NULL.
691 * szPath has to be at least MAX_PATH_LEN bytes long.
692 * RETURN
693 * ERR_VCP_LDDUNINIT if LDD for LDID not found.
695 RETERR16 WINAPI CtlGetLddPath16(LOGDISKID16 ldid, LPSTR szPath)
697 TRACE("(%d, %p);\n", ldid, szPath);
699 if (szPath)
701 LOGDISKDESC_S ldd;
702 INIT_LDD(ldd, ldid);
703 if (CtlFindLdd16(&ldd) == ERR_VCP_LDDFIND)
704 return ERR_VCP_LDDUNINIT;
705 SETUPX_GetLdd(&ldd);
706 strcpy(szPath, ldd.pszPath);
707 TRACE("ret '%s' for LDID %d\n", szPath, ldid);
709 return OK;
712 /***********************************************************************
713 * CtlSetLddPath (SETUPX.508)
715 * Sets the path of an LDD.
716 * Creates LDD for LDID if not existing yet.
718 RETERR16 WINAPI CtlSetLddPath16(LOGDISKID16 ldid, LPSTR szPath)
720 LOGDISKDESC_S ldd;
721 TRACE("(%d, '%s');\n", ldid, szPath);
723 SetupSetDirectoryIdA( 0, ldid, szPath );
724 INIT_LDD(ldd, ldid);
725 ldd.pszPath = szPath;
726 return CtlSetLdd16(&ldd);