[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 SHFILEOPSTRUCT fileop;
30 DWORD len = wcslen(lpszDir);
31 WCHAR *pszFrom = HeapAlloc(GetProcessHeap(), 0, (len + 2) * sizeof(WCHAR));
32 int ret;
33
34 wcscpy(pszFrom, lpszDir);
35 pszFrom[len] = 0;
36 pszFrom[len+1] = 0;
37
38 fileop.hwnd = NULL;
39 fileop.wFunc = FO_DELETE;
40 fileop.pFrom = pszFrom;
41 fileop.pTo = NULL;
42 fileop.fFlags = FOF_NOCONFIRMATION|FOF_SILENT;
43 fileop.fAnyOperationsAborted = FALSE;
44 fileop.lpszProgressTitle = NULL;
45 fileop.hNameMappings = NULL;
46
47 ret = SHFileOperation(&fileop);
48 HeapFree(GetProcessHeap(), 0, &pszFrom);
49 return (ret == 0);
50 }
51
52 VOID GetSystemDrive(LPWSTR lpszDrive)
53 {
54 WCHAR szWindir[MAX_PATH];
55 GetWindowsDirectoryW(szWindir, MAX_PATH);
56 _wsplitpath(szWindir, lpszDrive, NULL, NULL, NULL);
57 wcscat(lpszDrive, L"\\");
58 }
59
60 int APIENTRY wWinMain(HINSTANCE hInstance,
61 HINSTANCE hPrevInstance,
62 LPWSTR lpCmdLine,
63 int nCmdShow)
64 {
65 WCHAR szSetupPath[MAX_PATH];
66 WCHAR szFileName[MAX_PATH];
67 WCHAR szCabFileName[MAX_PATH];
68 WCHAR szCabFilePath[MAX_PATH];
69 WCHAR szTempDirName[50];
70 WCHAR szCmdLine[MAX_PATH];
71 WCHAR szTempCmdLine[MAX_PATH];
72 WCHAR szTempPath[MAX_PATH];
73 WCHAR szFullTempPath[MAX_PATH];
74 WCHAR szDrive[4];
75 DWORD dwAttrib;
76 PROCESS_INFORMATION processInfo;
77 STARTUPINFO startupInfo;
78 UNREFERENCED_PARAMETER(hPrevInstance);
79 UNREFERENCED_PARAMETER(nCmdShow);
80
81 GetCurrentDirectory(MAX_PATH, szSetupPath);
82 wcscat(szSetupPath, L"\\");
83 wcscpy(szFileName, szSetupPath);
84 wcscat(szFileName, L"setup.lst");
85
86 if (GetFileAttributes(szFileName) == INVALID_FILE_ATTRIBUTES)
87 {
88 MessageBoxW(0, L"Cannot find Setup.lst file", L"Error", MB_OK | MB_ICONERROR);
89 return 1;
90 }
91
92 /* read information from setup.lst */
93 GetPrivateProfileStringW(NT_PARAMS, L"CabinetFile", NULL, szCabFileName, MAX_PATH, szFileName);
94 GetPrivateProfileStringW(NT_PARAMS, L"TmpDirName", NULL, szTempDirName, 50, szFileName);
95 GetPrivateProfileStringW(NT_PARAMS, L"CmdLine", NULL, szCmdLine, MAX_PATH, szFileName);
96
97 wcscpy(szCabFilePath, szSetupPath);
98 wcscat(szCabFilePath, szCabFileName);
99
100 /* ceate temp directory */
101 GetSystemDrive(szDrive);
102 wcscpy(szTempPath, szDrive);
103 wcscat(szTempPath, MSSETUP_PATH);
104 wcscpy(szFullTempPath, szTempPath);
105 wcscat(szFullTempPath, szTempDirName);
106 wcscat(szFullTempPath, L"\\");
107
108 if (SHCreateDirectoryEx(0, szFullTempPath, NULL) != ERROR_SUCCESS)
109 {
110 MessageBoxW(0, L"Could not create Temp Directory.", L"Error", MB_OK | MB_ICONERROR);
111 return 1;
112 }
113
114 dwAttrib = GetFileAttributes(szTempPath);
115 SetFileAttributes(szTempPath, dwAttrib | FILE_ATTRIBUTE_HIDDEN);
116
117 /* extract files */
118 if (!SetupIterateCabinetW(szCabFilePath, 0, ExtCabCallback, szFullTempPath))
119 {
120 MessageBoxW(0, L"Could not extract cab file", L"Error", MB_OK | MB_ICONERROR);
121 DeleteDirectory(szTempPath);
122 return 1;
123 }
124
125 /* prepare command line */
126 wsprintf(szTempCmdLine, szCmdLine, szFullTempPath, lpCmdLine);
127 wcscpy(szCmdLine, szFullTempPath);
128 wcscat(szCmdLine, szTempCmdLine);
129
130 /* execute the 32-Bit installer */
131 ZeroMemory(&processInfo, sizeof(processInfo));
132 ZeroMemory(&startupInfo, sizeof(startupInfo));
133 startupInfo.cb = sizeof(startupInfo);
134 if (CreateProcessW(NULL, szCmdLine, NULL, NULL, FALSE, NORMAL_PRIORITY_CLASS, NULL, szFullTempPath, &startupInfo, &processInfo))
135 {
136 WaitForSingleObject(processInfo.hProcess, INFINITE);
137 CloseHandle(processInfo.hProcess);
138 CloseHandle(processInfo.hThread);
139 }
140 else
141 {
142 WCHAR szMsg[MAX_PATH];
143 wsprintf(szMsg, L"Failed to load the installer. Error %d", GetLastError());
144 MessageBoxW(0, szMsg, L"Error", MB_OK | MB_ICONERROR);
145 DeleteDirectory(szTempPath);
146 return 1;
147 }
148
149 /* cleanup */
150 DeleteDirectory(szTempPath);
151
152 return 0;
153 }
154
155 /* EOF */