+ return list;
+}
+
+static void writeFileList(filelist_t*file, const char*filename)
+{
+ FILE*fi = fopen(filename, "wb");
+ fprintf(fi, "[%s installed files]\r\n", SOFTWARE_NAME);
+ if(!fi) {
+ char buf[1024];
+ sprintf(buf, "Couldn't write file %s", filename);
+ MessageBox(0, buf, INSTALLER_NAME, MB_OK|MB_ICONERROR);
+ do_abort=1;
+ exit(1);
+ }
+ while(file) {
+ fprintf(fi, "%c %s\r\n", file->type, file->filename);
+ file = file->next;
+ }
+ fclose(fi);
+}
+
+static filelist_t*installedFiles = 0;
+
+static void addFile(const char*filename)
+{
+ installedFiles = registerFile(installedFiles, filename, 'F');
+}
+static void addDir(const char*filename)
+{
+ installedFiles = registerFile(installedFiles, filename, 'D');
+}
+
+static void handleTemplateFile(const char*filename)
+{
+ FILE*fi = fopen(filename, "rb");
+ fseek(fi, 0, SEEK_END);
+ int len = ftell(fi);
+ fseek(fi, 0, SEEK_SET);
+ char*file = malloc(len+1);
+ fread(file, len, 1, fi);
+ fclose(fi);
+ int l = strlen(install_path);
+ fi = fopen(filename, "wb");
+ char*pos = file;
+ char*lastpos = file;
+ while(1) {
+ pos = strstr(pos, "%%PATH%%");
+ if(!pos) {
+ pos = &file[len];
+ break;
+ }
+ if(pos!=lastpos)
+ fwrite(lastpos, pos-lastpos, 1, fi);
+ fwrite(install_path, l, 1, fi);
+ pos+=8; // length of "%%PATH%%"
+ lastpos = pos;
+ }
+ fwrite(lastpos, pos-lastpos, 1, fi);
+ fclose(fi);
+ free(file);
+}
+
+static int setRegistryEntry(char*key,char*value)
+{
+ HKEY hkey1;
+ HKEY hkey2;
+ int ret1 = -1, ret2= -1;
+ ret1 = RegCreateKey(HKEY_CURRENT_USER, key, &hkey1);
+ if(config_forAllUsers) {
+ ret2 = RegCreateKey(HKEY_LOCAL_MACHINE, key, &hkey2);
+ }
+
+ if(ret1 && ret2) {
+ fprintf(stderr, "registry: CreateKey %s failed\n", key);
+ return 0;
+ }
+ if(ret1) {
+ installedFiles = registerFile(installedFiles, key, 'k');
+ }
+ if(ret2) {
+ installedFiles = registerFile(installedFiles, key, 'K');
+ }
+
+ if(!ret1)
+ ret1 = RegSetValue(hkey1, NULL, REG_SZ, value, strlen(value)+1);
+ if(!ret2)
+ ret2 = RegSetValue(hkey2, NULL, REG_SZ, value, strlen(value)+1);
+ if(ret1 && ret2) {
+ fprintf(stderr, "registry: SetValue %s failed\n", key);
+ return 0;
+ }
+ return 1;
+}
+
+
+static char* getRegistryEntry(char*path)
+{
+ int res = 0;
+ HKEY key;
+ long rc;
+ long size = 0;
+ DWORD type;
+ char*buf;
+ rc = RegOpenKeyEx(HKEY_LOCAL_MACHINE, path, 0, KEY_ALL_ACCESS/* KEY_READ*/, &key);
+ if (rc != ERROR_SUCCESS) {
+ fprintf(stderr, "RegOpenKeyEx failed\n");
+ return 0;
+ }
+ rc = RegQueryValueEx(key, NULL, 0, 0, 0, (LPDWORD)&size) ;
+ if(rc != ERROR_SUCCESS) {
+ fprintf(stderr, "RegQueryValueEx(1) failed: %d\n", rc);
+ return 0;
+ }
+ buf = (char*)malloc(size+1);
+ rc = RegQueryValueEx(key, NULL, 0, &type, (BYTE*)buf, (LPDWORD)&size);
+ if(rc != ERROR_SUCCESS) {
+ fprintf(stderr, "RegQueryValueEx(2) failed: %d\n", rc);
+ return 0;
+ }
+ if(type == REG_SZ || type == REG_EXPAND_SZ) {
+ while(size && buf[size-1] == '\0')
+ --size;
+ buf[size] = 0;
+ /* TODO: convert */
+ return buf;
+ } else if(type == REG_BINARY) {
+ return buf;
+ }
+}
+
+static int has_full_access = 0;
+static char hasFullAccess()
+{
+ /* find out whether we can write keys in HKEY_LOCAL_MACHINE */
+ HKEY hKey;
+ int ret = RegOpenKeyEx(HKEY_LOCAL_MACHINE, "Software\\Microsoft\\Windows\\CurrentVersion\\Uninstall", 0,
+ KEY_CREATE_SUB_KEY, &hKey);
+ if(!ret) {
+ RegCloseKey(hKey);
+ return 1;
+ } else {
+ return 0;
+ }
+}
+
+void processMessages()
+{
+ MSG msg;
+ while(PeekMessage(&msg,NULL,0,0,0))
+ {
+ GetMessage(&msg, NULL, 0, 0);
+ TranslateMessage(&msg);
+ DispatchMessage(&msg);
+ }
+}
+
+int addRegistryEntries(char*install_dir)
+{
+ int ret;
+ ret = setRegistryEntry(registry_path, install_dir);
+ if(!ret) return 0;
+ return 1;
+}
+
+int CreateShortcut(char*path, char*description, char*filename, char*arguments, int iconindex, char*iconpath, char*workdir)
+{
+ printf("Creating %s -> %s\n", filename, path);
+ WCHAR wszFilename[MAX_PATH];
+ IShellLink *ps1 = NULL;
+ IPersistFile *pPf = NULL;
+ HRESULT hr;
+ hr = CoCreateInstance(&CLSID_ShellLink, NULL, CLSCTX_INPROC_SERVER, &IID_IShellLink, (void*)&ps1);
+ if(FAILED(hr)) return 0;
+ hr = ps1->lpVtbl->QueryInterface(ps1, &IID_IPersistFile, (void **)&pPf);
+ if(FAILED(hr)) return 0;
+ hr = ps1->lpVtbl->SetPath(ps1, path);
+ if(FAILED(hr)) return 0;
+ hr = ps1->lpVtbl->SetDescription(ps1, description);
+
+ if (arguments) {
+ hr = ps1->lpVtbl->SetArguments(ps1, arguments);
+ if(FAILED(hr)) return 0;
+ }
+ if (iconpath) {
+ hr = ps1->lpVtbl->SetIconLocation(ps1, iconpath, iconindex);
+ if (FAILED(hr)) return 0;
+ }
+ if (workdir) {
+ hr = ps1->lpVtbl->SetWorkingDirectory(ps1, workdir);
+ if (FAILED(hr)) return 0;
+ }
+ MultiByteToWideChar(CP_ACP, 0, filename, -1, wszFilename, MAX_PATH);
+ hr = pPf->lpVtbl->Save(pPf, wszFilename, TRUE);
+ if(FAILED(hr)) {
+ return 0;
+ }
+ pPf->lpVtbl->Release(pPf);
+ ps1->lpVtbl->Release(ps1);
+ addFile(filename);
+ return 1;
+}