DotNet4 application packaging tool-> Upgrade [2] Installation Tool Analysis

Source: Internet
Author: User

In the previous article of this series,

We provide specific ideas for this tool.

I got feedback from many friends!

Based on comments from friends,

Without changing the principle of the Tool

I upgraded this program.

For example:

As you can see,

Now this packaging tool can package dotNet2.0 3.5 4

Even all applications that have added registry entries in the Registry

Next we will start to analyze the Installation Tool (that is, the image you saw above)

--------------------------

Entry function:

int WinMain(HINSTANCE hInstance,HINSTANCE hPreInstance,LPSTR lpCmdLine,int nCmdShow){DialogBox(hInstance,MAKEINTRESOURCE(MainWinDL),0,DlgProc);return 0;}
 

Well, the entry function is very simple. it just creates a form and registers the window procedure function.

-----------------------------

Window Process

// Bool callback DlgProc (HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam) {switch (message) {case WM_INITDIALOG: OnInitDlg (hDlg); return TRUE; case WM_COMMAND: switch (LOWORD (wParam) {case IDC_STATIC_Name: ShellExecute (hDlg, "open", "http://www.cnblogs.com/liulun", NULL, NULL, SW_SHOWNORMAL); break; case IDC_BUTTON1: GetFile (hDlg, IDC_EDIT1); break; case IDC_BUTTON3: GetFile (hDlg, IDC_EDIT3); break; case IDC_RADIO1: CheckRadio (hDlg, IDC_RADIO1, "SOFTWARE \ Microsoft \ NET Framework Setup \ NDP \ v2.0.50727"); break; case IDC_RADIO2: CheckRadio (hDlg, IDC_RADIO2, "SOFTWARE \ Microsoft \ NET Framework Setup \ NDP \ v3.5"); break; case IDC_RADIO3: CheckRadio (hDlg, IDC_RADIO3, "SOFTWARE \ Microsoft \ NET Framework Setup \ NDP \ v4"); break; case IDC_RADIO_ELSE: CheckRadio (hDlg, IDC_RADIO_ELSE, ""); break; case IDOK: releaseTar (hDlg); ReplaceICO (hDlg); BagTar (hDlg, IDC_EDIT3); BagTar (hDlg, IDC_EDIT1); BagStr (hDlg); Alert ("package successful"); break; case IDCANCEL: EndDialog (hDlg, 0); return TRUE;} break;} return FALSE ;}
 

In this process function

Each received message executes one or more functions.

So let's talk about a function.

-------------------------------------------------------

Window initialization message

The dotNet4 radio button is selected by default.

void OnInitDlg(HWND hwnd){HWND cld = ::GetDlgItem(hwnd,IDC_RADIO3);::SendMessage(cld,BM_SETCHECK,1,0);::SetDlgItemText(hwnd,IDC_EDIT2,"SOFTWARE\\Microsoft\\NET Framework Setup\\NDP\\v4");LastCheckRdioId = IDC_RADIO3;}
 

---------------------------------------------------------

Click events of four single-choice buttons

Set the content of the text box,

The selected radio button is recorded.

void CheckRadio(HWND hwnd,int rdioID,LPCSTR val){if(rdioID == LastCheckRdioId){return;}::SetDlgItemText(hwnd,IDC_EDIT2,val);LastCheckRdioId = rdioID;}
 

------------------------------------------------------------

Select File

Assign the selected file path to the corresponding text box

// Obtain the file void GetFile (HWND hwnd, int EDITId) {char szFile [MAX_PATH] = {0}; OPENFILENAME ofn; memset (& ofn, 0, sizeof (OPENFILENAME )); ofn. lStructSize = sizeof (OPENFILENAME); ofn. lpstrFile = szFile; ofn. nMaxFile = MAX_PATH; ofn. lpstrFilter = "application (.exe) \ 0 *. exe \ 0 \ 0 "; ofn. lpstrDefExt = "exe"; ofn. lpstrTitle = "select exe file"; ofn. nFilterIndex = 1; ofn. lpstrFileTitle = NULL; ofn. nMaxFileTitle = 0; ofn. lpstrInitialDir = NULL; if (GetOpenFileName (& ofn) {SetDlgItemText (hwnd, EDITId, szFile );}}
 

--------------------------------------------------------------------

Read the host program from the resource and release it to the current directory according to the specified file name.

// Release the resource int ReleaseTar (HWND hwnd) {: GetDlgItemText (hwnd, IDC_EDIT1, szFilePath, MAX_PATH);: strcat (szFilePath, ".bag.exe"); HMODULE hInstance = :: getModuleHandle (NULL); HRSRC hResID =: FindResource (hInstance, (LPCSTR) IDR_BIN1, "bin"); 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 ;}
 

---------------------------------------------------------------------------

Replace the ICO icon resource of the Host Program

Here we need to focus on the following:

To update the resources of an application

You must first know the ID of this resource

The GetIcoIndex function is used to obtain the resource ID.

Generally, two application ICON resources are available.

Therefore, the IDs of two chart resources are obtained.

Other winapis are not explained much ~~

int GetIcoIndex(HMODULE hExe,int index[]){HRSRC hRes;int iLoop;int i = 0;for(iLoop = 1;;iLoop++)    {        hRes = FindResource(hExe, MAKEINTRESOURCE(iLoop), RT_ICON); if (NULL == hRes){if(iLoop == 60){break;}continue ; }        else{index[i]  = iLoop;i +=1;if(i == 2){break;}}    }return 1;}int ReplaceICO(HWND hwnd){HMODULE hSrcExe,hDestExe;HANDLE hUpdateRes;HRSRC hRes;HRSRC hResLoad;char *lpResLock;int result;char szFile[MAX_PATH+1] = {0};int hSrcIndex[2] = {0};int hDestIndex[2] = {0};::GetDlgItemText(hwnd,IDC_EDIT1,szFile,MAX_PATH);hSrcExe = LoadLibrary(szFile);hDestExe = LoadLibrary(szFilePath);GetIcoIndex(hSrcExe,hSrcIndex);GetIcoIndex(hDestExe,hDestIndex);for(int i=0;i<2;i++){hRes = FindResource(hSrcExe, MAKEINTRESOURCE(hSrcIndex[i]), RT_ICON); hResLoad=(HRSRC)LoadResource(hSrcExe,hRes);lpResLock=(char*)LockResource(hResLoad);FreeLibrary(hDestExe);hUpdateRes=BeginUpdateResource(szFilePath,FALSE);result=UpdateResource(hUpdateRes,RT_ICON,MAKEINTRESOURCE(hDestIndex[i]),MAKELANGID(LANG_NEUTRAL, SUBLANG_NEUTRAL),lpResLock,SizeofResource(hSrcExe,hRes));EndUpdateResource(hUpdateRes, FALSE);}FreeLibrary(hSrcExe);return result;}
 

---------------------------------------

Add the target program resources and dotNet installation package resources to the Host Program

The added resources also need to be marked with IDs.

Because the Host Program will obtain these resources according to the agreed ID

The EditId parameter is the ID of these resources.

int BagTar(HWND hwnd,int EditId){HANDLE hFile;DWORD dwFileSize,dwBytesRead;LPBYTE lpBuffer;char szFile[MAX_PATH+1] = {0};::GetDlgItemText(hwnd,EditId,szFile,MAX_PATH);hFile = CreateFile(szFile,GENERIC_READ,0,NULL,OPEN_EXISTING,FILE_ATTRIBUTE_NORMAL,NULL);dwFileSize = GetFileSize(hFile, NULL);lpBuffer = new BYTE[dwFileSize];ReadFile(hFile, lpBuffer, dwFileSize, &dwBytesRead, NULL);HANDLE hResource = BeginUpdateResource(szFilePath, FALSE);UpdateResource(hResource,RT_RCDATA,MAKEINTRESOURCE(EditId),MAKELANGID(LANG_NEUTRAL,SUBLANG_DEFAULT),(LPVOID)lpBuffer,dwFileSize);EndUpdateResource(hResource, FALSE);delete [] lpBuffer; CloseHandle(hFile);return 1;}
 

--------------------------------------------------------

Package the registry entry path as a resource into the host Program.

We agree that the resource ID is 1039

int BagStr(HWND hwnd){char szFile[MAX_PATH+1] = {0};::GetDlgItemText(hwnd,IDC_EDIT2,szFile,MAX_PATH);HANDLE hUpdateRes=BeginUpdateResource(szFilePath,FALSE);int result=UpdateResource(hUpdateRes,RT_RCDATA,MAKEINTRESOURCE(1039),MAKELANGID(LANG_NEUTRAL, SUBLANG_NEUTRAL),(LPVOID)szFile,::strlen(szFile));EndUpdateResource(hUpdateRes, FALSE);return 0;}
 

-------------------------------------------------------------

Other code is as follows:

# Include <Windows. h> # include <ShlObj. h> # include "resource. h "TCHAR szFilePath [MAX_PATH + 1]; int LastCheckRdioId; // prompt void Alert (LPCSTR msg) {MessageBox (NULL, msg," system prompt ", MB_ OK );}
 

---------------------------------------------------------------

Postscript:

No error-tolerant code is written ~

The naming rules are not followed ~

Sorry ~

Please recommend my article

Because your support is my motivation-> -> ->

Executable file compiled by this tool: BagDotNet.zip

(Because you do not need to package the dotNet4 installer, there are only a few dozen K! How light !)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.