Add an assert if both arguments to DD_STRUCT_COPY_BYSIZE are equal.
[wine] / dlls / advpack / reg.c
1 /*
2  * Advpack registry functions
3  *
4  * Copyright 2004 Huw D M Davies
5  *
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.
10  *
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.
15  *
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
19  */
20
21 #include <stdarg.h>
22 #include "windef.h"
23 #include "winbase.h"
24 #include "winreg.h"
25 #include "winerror.h"
26 #include "winuser.h"
27 #include "winternl.h"
28 #include "setupapi.h"
29 #include "advpub.h"
30 #include "wine/unicode.h"
31 #include "wine/debug.h"
32
33 WINE_DEFAULT_DEBUG_CHANNEL(advpack);
34
35 static const WCHAR REGINST[] = {'R','E','G','I','N','S','T',0};
36 static const WCHAR Strings[] = {'S','t','r','i','n','g','s',0};
37 static const WCHAR MOD_PATH[] = {'_','M','O','D','_','P','A','T','H',0};
38 static const WCHAR SYS_MOD_PATH[] = {'_','S','Y','S','_','M','O','D','_','P','A','T','H',0};
39 static const WCHAR SystemRoot[] = {'S','y','s','t','e','m','R','o','o','t',0};
40 static const WCHAR escaped_SystemRoot[] = {'%','S','y','s','t','e','m','R','o','o','t','%',0};
41
42 static BOOL get_temp_ini_path(LPWSTR name)
43 {
44     WCHAR tmp_dir[MAX_PATH];
45     WCHAR prefix[] = {'a','v','p',0};
46
47     if(!GetTempPathW(sizeof(tmp_dir)/sizeof(WCHAR), tmp_dir))
48        return FALSE;
49
50     if(!GetTempFileNameW(tmp_dir, prefix, 0, name))
51         return FALSE;
52     return TRUE;
53 }
54
55 static BOOL create_tmp_ini_file(HMODULE hm, WCHAR *ini_file)
56 {
57     HRSRC hrsrc;
58     HGLOBAL hmem = NULL;
59     DWORD rsrc_size, bytes_written;
60     VOID *rsrc_data;
61     HANDLE hf = INVALID_HANDLE_VALUE;
62
63     if(!get_temp_ini_path(ini_file)) {
64         ERR("Can't get temp ini file path\n");
65         goto error;
66     }
67
68     if(!(hrsrc = FindResourceW(hm, REGINST, REGINST))) {
69         ERR("Can't find REGINST resource\n");
70         goto error;
71     }
72
73     rsrc_size = SizeofResource(hm, hrsrc);
74     hmem = LoadResource(hm, hrsrc);
75     rsrc_data = LockResource(hmem);
76
77     if(!rsrc_data || !rsrc_size) {
78         ERR("Can't load REGINST resource\n");
79         goto error;
80     }       
81
82     if((hf = CreateFileW(ini_file, GENERIC_WRITE, 0, NULL, CREATE_ALWAYS,
83                          FILE_ATTRIBUTE_NORMAL, NULL)) == INVALID_HANDLE_VALUE) {
84         ERR("Unable to create temp ini file\n");
85         goto error;
86     }
87     if(!WriteFile(hf, rsrc_data, rsrc_size, &bytes_written, NULL) || rsrc_size != bytes_written) {
88         ERR("Write failed\n");
89         goto error;
90     }
91     FreeResource(hmem);
92     CloseHandle(hf);
93     return TRUE;
94 error:
95     if(hmem) FreeResource(hmem);
96     if(hf != INVALID_HANDLE_VALUE) CloseHandle(hf);
97     return FALSE;
98 }
99
100 /***********************************************************************
101  *          RegInstall (advpack.@)
102  *
103  */
104 HRESULT WINAPI RegInstall(HMODULE hm, LPCSTR pszSection, LPCSTRTABLE pstTable)
105 {
106     int i;
107     WCHAR tmp_ini_path[MAX_PATH];
108     WCHAR mod_path[MAX_PATH + 2], sys_mod_path[MAX_PATH + 2], sys_root[MAX_PATH];
109     HINF hinf;
110     WCHAR quote[] = {'\"',0};
111     UNICODE_STRING section;
112
113     TRACE("(%p %s %p)\n", hm, pszSection, pstTable);
114
115     if (pstTable) for(i = 0; i < pstTable->cEntries; i++)
116         TRACE("%d: %s -> %s\n", i, pstTable->pse[i].pszName,
117              pstTable->pse[i].pszValue);
118
119     if(!create_tmp_ini_file(hm, tmp_ini_path))
120         return E_FAIL;
121
122     /* Write a couple of pre-defined strings */
123     mod_path[0] = '\"';
124     GetModuleFileNameW(hm, mod_path + 1, sizeof(mod_path)/sizeof(WCHAR) - 2);
125     strcatW(mod_path, quote);
126     WritePrivateProfileStringW(Strings, MOD_PATH, mod_path, tmp_ini_path);
127     
128     *sys_root = '\0';
129     GetEnvironmentVariableW(SystemRoot, sys_root, sizeof(sys_root)/sizeof(WCHAR));
130     if(!strncmpiW(sys_root, mod_path + 1, strlenW(sys_root))) {
131         sys_mod_path[0] = '\"';
132         strcpyW(sys_mod_path + 1, escaped_SystemRoot);
133         strcatW(sys_mod_path, mod_path + 1 + strlenW(sys_root));
134     } else {
135         FIXME("SYS_MOD_PATH needs more work\n");
136         strcpyW(sys_mod_path, mod_path);
137     }
138     WritePrivateProfileStringW(Strings, SYS_MOD_PATH, sys_mod_path, tmp_ini_path);
139
140     /* Write the additional string table */
141     if (pstTable) for(i = 0; i < pstTable->cEntries; i++) {
142         char tmp_value[MAX_PATH + 2];
143         UNICODE_STRING name, value;
144         tmp_value[0] = '\"';
145         strcpy(tmp_value + 1, pstTable->pse[i].pszValue);
146         strcat(tmp_value, "\"");
147         RtlCreateUnicodeStringFromAsciiz(&name, pstTable->pse[i].pszName);
148         RtlCreateUnicodeStringFromAsciiz(&value, tmp_value);
149         WritePrivateProfileStringW(Strings, name.Buffer, value.Buffer, tmp_ini_path);
150         RtlFreeUnicodeString(&name);
151         RtlFreeUnicodeString(&value);
152     }
153     /* flush cache */
154     WritePrivateProfileStringW(NULL, NULL, NULL, tmp_ini_path);
155
156
157     if((hinf = SetupOpenInfFileW(tmp_ini_path, NULL, INF_STYLE_WIN4, NULL)) ==
158        INVALID_HANDLE_VALUE) {
159         ERR("Setupapi can't open inf\n");
160         return E_FAIL;
161     }
162
163     /* append any layout files */
164     SetupOpenAppendInfFileW(NULL, hinf, NULL);
165
166     /* Need to do a lot more here */
167     RtlCreateUnicodeStringFromAsciiz(&section, pszSection);
168     SetupInstallFromInfSectionW(NULL, hinf, section.Buffer,
169                                 SPINST_INIFILES | SPINST_REGISTRY | SPINST_PROFILEITEMS,
170                                 HKEY_LOCAL_MACHINE, NULL, 0, NULL, NULL, NULL, NULL);
171     RtlFreeUnicodeString(&section);
172
173     
174     SetupCloseInfFile(hinf);
175     DeleteFileW(tmp_ini_path);
176
177     return S_OK;
178 }