Index
DotNet4 application packaging tool-> upgraded version [3] Host Program Analysis + download all source code
DotNet4 application packaging tool-> Upgrade [2] Installation Tool Analysis
DotNet4 application packaging tool (package the DotNet4 installer into your application; WINAPI development and smooth execution without the dotNet environment) [1] Overall Thinking
Speak nonsense
Entry Function
Entry functions are a collection of all logic
Int WinMain (HINSTANCE hInstance, HINSTANCE hPreInstance, LPSTR lpCmdLine, int nCmdShow) {int result; LPCTSTR netName = ".net4.exe"; LPCTSTR tarName = ".tar.exe"; result = CheckDotNet4 (); if (result = 0) {result = FreeRC (MAKEINTRESOURCE (1012), netName); if (result = 0) {Alert ("the pre-installed tool is not released successfully ~ Installation failed "); return 0;} result = CreateInstallProcess (netName); if (result = 0) {Alert (" the pre-installed tool process is not started successfully ~ Installation failed "); ClearFile (netName); return 0 ;}// currently does not support Silent Installation // result = instal#(); result = CheckDotNet4 (); if (result = 0) {Alert ("the pre-installed tool is not successfully installed ~ Installation failed "); ClearFile (netName); return 0 ;}} result = FreeRC (MAKEINTRESOURCE (1001), tarName); if (result = 0) {Alert ("the application is not released successfully ~ Installation failed "); ClearFile (netName); ClearFile (tarName); return 0;} result = CreateInstallProcess (tarName); if (result = 0) {Alert ("application process not started successfully ~ Installation failed ");} ClearFile (netName); ClearFile (tarName); return 0 ;}
Determine whether the target environment has related registry items
Extracts the registry key from the resource.
Then judge
Int CheckDotNet4 () {HMODULE hInstance =: GetModuleHandle (NULL); HRSRC hResID =: FindResource (hInstance, MAKEINTRESOURCE (1039), RT_RCDATA); HGLOBAL hRes = :: loadResource (hInstance, hResID); LPVOID pRes =: LockResource (hRes); DWORD dwResSize =: SizeofResource (hInstance, hResID); int length = MAX_PATH + 1; if (! DwResSize) {return 0;} length = (int) dwResSize; TCHAR regeditStr [MAX_PATH + 1]; memcpy (regeditStr, pRes, dwResSize); regeditStr [dwResSize] = 0; HKEY ck; int result = 1; if (ERROR_SUCCESS! = RegOpenKeyEx (HKEY_LOCAL_MACHINE, regeditStr, 0, KEY_ALL_ACCESS, & ck )) {Alert ("your system lacks application execution environment \ n now starts to build this environment for you"); result = 0 ;} RegCloseKey (ck); return result ;}
If no related registry key exists
Release the program to be preinstalled
Read Resources
And write resources to files.
int FreeRC(LPCTSTR resourceID,LPCTSTR resourceName){ HMODULE hInstance = ::GetModuleHandle(NULL);TCHAR szFilePath[MAX_PATH + 1];GetPath(szFilePath,resourceName,hInstance); HRSRC hResID = ::FindResource(hInstance,resourceID,RT_RCDATA); HGLOBAL hRes = ::LoadResource(hInstance,hResID); LPVOID pRes = ::LockResource(hRes); DWORD dwResSize = ::SizeofResource(hInstance,hResID);if(!dwResSize){return 0;} HANDLE hResFile = CreateFile(szFilePath,GENERIC_WRITE,0,NULL,CREATE_ALWAYS,FILE_ATTRIBUTE_NORMAL,NULL); DWORD dwWritten = 0; WriteFile(hResFile,pRes,dwResSize,&dwWritten,NULL); CloseHandle(hResFile); if(dwResSize == dwWritten);{return 1;}return 0;}
After successful release
Run this program.
Note the WINAPI of WaitForSingleObject.
Is waiting for the process to exit
// Create process int CreateInstallProcess (LPCTSTR resourceName) {STARTUPINFO stinfo; ZeroMemory (void *) & stinfo, sizeof (STARTUPINFO); PROCESS_INFORMATION ProcessInfo; stinfo. cb = sizeof (STARTUPINFO); stinfo. dwFlags = STARTF_USESHOWWINDOW; stinfo. wShowWindow = SW_SHOW; // SW_SHOW SW_HIDETCHAR szFilePath [MAX_PATH + 1]; GetPath (szFilePath, resourceName); // .dotnet4.exe int result = CreateProcess (szFilePath, NULL, false, 0, NULL, NULL, & stinfo, & ProcessInfo); WaitForSingleObject (ProcessInfo. hProcess, INFINITE); return result ;}
If the process is not started successfully
The released preinstallation program will be deleted.
int ClearFile(LPCTSTR resourceName){TCHAR szFilePath[MAX_PATH + 1];GetPath(szFilePath,resourceName);//.dotnet4.exeint result = ::DeleteFile(szFilePath);return result;}
Next, check the registry key again.
Result = CheckDotNet4 ();
If no related registry key exists
If the Pre-installation program is not successfully installed, exit the program.
---------------------
Now, the check has been completed
Remaining code in the entry function
Is to execute the work of the Target Program
Do not explain more
Result = FreeRC (MAKEINTRESOURCE (1001), tarName); if (result = 0) {Alert ("the application is not released successfully ~ Installation failed "); ClearFile (netName); ClearFile (tarName); return 0;} result = CreateInstallProcess (tarName); if (result = 0) {Alert ("application process not started successfully ~ Installation failed ");} ClearFile (netName); ClearFile (tarName );
---------------------------
Download the packaging tool source code: BagDotNet_code.zip
Download the source code of the Host Program: CheckDotNet_code.zip
--------------------------------------------------------
If you like my articles, please recommend them ~ Thank you ~