f6cd5e875b51f1cac42908c91112bc684e784a71
[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 /* read information from setup.lst */
87 GetPrivateProfileStringW(NT_PARAMS, L"CabinetFile", NULL, szCabFileName, MAX_PATH, szFileName);
88 GetPrivateProfileStringW(NT_PARAMS, L"TmpDirName", NULL, szTempDirName, 50, szFileName);
89 GetPrivateProfileStringW(NT_PARAMS, L"CmdLine", NULL, szCmdLine, MAX_PATH, szFileName);
90
91 wcscpy(szCabFilePath, szSetupPath);
92 wcscat(szCabFilePath, szCabFileName);
93
94 /* ceate temp directory */
95 GetSystemDrive(szDrive);
96 wcscpy(szTempPath, szDrive);
97 wcscat(szTempPath, MSSETUP_PATH);
98 wcscpy(szFullTempPath, szTempPath);
99 wcscat(szFullTempPath, szTempDirName);
100 wcscat(szFullTempPath, L"\\");
101
102 if (SHCreateDirectoryEx(0, szFullTempPath, NULL) != ERROR_SUCCESS)
103 {
104 MessageBoxW(0, L"Could not create Temp Directory.", L"Error", MB_OK | MB_ICONERROR);
105 return 1;
106 }
107
108 dwAttrib = GetFileAttributes(szTempPath);
109 SetFileAttributes(szTempPath, dwAttrib | FILE_ATTRIBUTE_HIDDEN);
110
111 /* extract files */
112 if (!SetupIterateCabinetW(szCabFilePath, 0, ExtCabCallback, szFullTempPath))
113 {
114 MessageBoxW(0, L"Could not extract cab file", L"Error", MB_OK | MB_ICONERROR);
115 DeleteDirectory(szTempPath);
116 return 1;
117 }
118
119 /* prepare command line */
120 wsprintf(szTempCmdLine, szCmdLine, szFullTempPath, lpCmdLine);
121 wcscpy(szCmdLine, szFullTempPath);
122 wcscat(szCmdLine, szTempCmdLine);
123
124 /* execute the 32-Bit installer */
125 ZeroMemory(&processInfo, sizeof(processInfo));
126 ZeroMemory(&startupInfo, sizeof(startupInfo));
127 startupInfo.cb = sizeof(startupInfo);
128 if (CreateProcessW(NULL, szCmdLine, NULL, NULL, FALSE, NORMAL_PRIORITY_CLASS, NULL, szFullTempPath, &startupInfo, &processInfo))
129 {
130 WaitForSingleObject(processInfo.hProcess, INFINITE);
131 CloseHandle(processInfo.hProcess);
132 CloseHandle(processInfo.hThread);
133 }
134 else
135 {
136 WCHAR szMsg[MAX_PATH];
137 wsprintf(szMsg, L"Failed to load the installer. Error %d", GetLastError());
138 MessageBoxW(0, szMsg, L"Error", MB_OK | MB_ICONERROR);
139 DeleteDirectory(szTempPath);
140 return 1;
141 }
142
143 /* cleanup */
144 DeleteDirectory(szTempPath);
145
146 return 0;
147 }
148
149 /* EOF */