[setup16]
[reactos.git] / reactos / base / applications / setup16 / main.c
1 #include <windows.h>
2 #include <shlobj.h>
3 #include <setupapi.h>
4
5 #define NT_PARAMS L"NT3.51 Intel Params"
6 #define MSSETUP_PATH L"~MSSETUP.T\\"
7
8 static UINT WINAPI ExtCabCallback(PVOID Context, UINT Notification, UINT_PTR Param1, UINT_PTR Param2)
9 {
10 FILE_IN_CABINET_INFO_W *pInfo;
11 FILEPATHS_W *pFilePaths;
12
13 switch(Notification)
14 {
15 case SPFILENOTIFY_FILEINCABINET:
16 pInfo = (FILE_IN_CABINET_INFO_W*)Param1;
17 wcscpy(pInfo->FullTargetName, (LPCWSTR)Context);
18 wcscat(pInfo->FullTargetName, pInfo->NameInCabinet);
19 return FILEOP_DOIT;
20 case SPFILENOTIFY_FILEEXTRACTED:
21 pFilePaths = (FILEPATHS_W*)Param1;
22 return NO_ERROR;
23 }
24 return NO_ERROR;
25 }
26
27 BOOL DeleteDirectory(LPWSTR lpszDir)
28 {
29 DWORD len = wcslen(lpszDir);
30 WCHAR *pszFrom = HeapAlloc(GetProcessHeap(), 0, (len + 2) * sizeof(WCHAR));
31 wcscpy(pszFrom, lpszDir);
32 pszFrom[len] = 0;
33 pszFrom[len+1] = 0;
34
35 SHFILEOPSTRUCT fileop;
36 fileop.hwnd = NULL;
37 fileop.wFunc = FO_DELETE;
38 fileop.pFrom = pszFrom;
39 fileop.pTo = NULL;
40 fileop.fFlags = FOF_NOCONFIRMATION|FOF_SILENT;
41 fileop.fAnyOperationsAborted = FALSE;
42 fileop.lpszProgressTitle = NULL;
43 fileop.hNameMappings = NULL;
44
45 int ret = SHFileOperation(&fileop);
46 HeapFree(GetProcessHeap(), 0, &pszFrom);
47 return (ret == 0);
48 }
49
50 VOID GetSystemDrive(LPWSTR lpszDrive)
51 {
52 WCHAR szWindir[MAX_PATH];
53 GetWindowsDirectoryW(szWindir, MAX_PATH);
54 _wsplitpath(szWindir, lpszDrive, NULL, NULL, NULL);
55 wcscat(lpszDrive, L"\\");
56 }
57
58 int APIENTRY wWinMain(HINSTANCE hInstance,
59 HINSTANCE hPrevInstance,
60 LPWSTR lpCmdLine,
61 int nCmdShow)
62 {
63 UNREFERENCED_PARAMETER(hPrevInstance);
64 UNREFERENCED_PARAMETER(nCmdShow);
65
66 WCHAR szSetupPath[MAX_PATH];
67 WCHAR szFileName[MAX_PATH];
68 WCHAR szCabFileName[MAX_PATH];
69 WCHAR szCabFilePath[MAX_PATH];
70 WCHAR szTempDirName[50];
71 WCHAR szCmdLine[MAX_PATH];
72 WCHAR szTempCmdLine[MAX_PATH];
73 WCHAR szTempPath[MAX_PATH];
74 WCHAR szFullTempPath[MAX_PATH];
75 WCHAR szDrive[4];
76 DWORD dwAttrib;
77 PROCESS_INFORMATION processInfo;
78 STARTUPINFO startupInfo;
79
80 GetCurrentDirectory(MAX_PATH, szSetupPath);
81 wcscat(szSetupPath, L"\\");
82 wcscpy(szFileName, szSetupPath);
83 wcscat(szFileName, L"setup.lst");
84
85 /* read information from setup.lst */
86 GetPrivateProfileStringW(NT_PARAMS, L"CabinetFile", NULL, szCabFileName, MAX_PATH, szFileName);
87 GetPrivateProfileStringW(NT_PARAMS, L"TmpDirName", NULL, szTempDirName, 50, szFileName);
88 GetPrivateProfileStringW(NT_PARAMS, L"CmdLine", NULL, szCmdLine, MAX_PATH, szFileName);
89
90 wcscpy(szCabFilePath, szSetupPath);
91 wcscat(szCabFilePath, szCabFileName);
92
93 /* ceate temp directory */
94 GetSystemDrive(szDrive);
95 wcscpy(szTempPath, szDrive);
96 wcscat(szTempPath, MSSETUP_PATH);
97 wcscpy(szFullTempPath, szTempPath);
98 wcscat(szFullTempPath, szTempDirName);
99 wcscat(szFullTempPath, L"\\");
100
101 if (SHCreateDirectoryEx(0, szFullTempPath, NULL) != ERROR_SUCCESS)
102 {
103 MessageBoxW(0, L"Could not create Temp Directory.", L"Error", MB_OK | MB_ICONERROR);
104 return 1;
105 }
106
107 dwAttrib = GetFileAttributes(szTempPath);
108 SetFileAttributes(szTempPath, dwAttrib | FILE_ATTRIBUTE_HIDDEN);
109
110 /* extract files */
111 if (!SetupIterateCabinetW(szCabFilePath, 0, ExtCabCallback, szFullTempPath))
112 {
113 MessageBoxW(0, L"Could not extract cab file", L"Error", MB_OK | MB_ICONERROR);
114 DeleteDirectory(szTempPath);
115 return 1;
116 }
117
118 /* prepare command line */
119 wsprintf(szTempCmdLine, szCmdLine, szFullTempPath, lpCmdLine);
120 wcscpy(szCmdLine, szFullTempPath);
121 wcscat(szCmdLine, szTempCmdLine);
122
123 /* execute the 32-Bit installer */
124 ZeroMemory(&processInfo, sizeof(processInfo));
125 ZeroMemory(&startupInfo, sizeof(startupInfo));
126 startupInfo.cb = sizeof(startupInfo);
127 if (CreateProcessW(NULL, szCmdLine, NULL, NULL, FALSE, NORMAL_PRIORITY_CLASS, NULL, szFullTempPath, &startupInfo, &processInfo))
128 {
129 WaitForSingleObject(processInfo.hProcess, INFINITE);
130 CloseHandle(processInfo.hProcess);
131 CloseHandle(processInfo.hThread);
132 }
133 else
134 {
135 WCHAR szMsg[MAX_PATH];
136 wsprintf(szMsg, L"Failed to load the installer. Error %d", GetLastError());
137 MessageBoxW(0, szMsg, L"Error", MB_OK | MB_ICONERROR);
138 DeleteDirectory(szTempPath);
139 return 1;
140 }
141
142 /* cleanup */
143 DeleteDirectory(szTempPath);
144
145 return 0;
146 }
147
148 /* EOF */