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