C # detect Framework installation when the application is running

Source: Internet
Author: User

Because the program runs in a Ukey (USB flash drive) instead of a Setup package, you cannot directly run the. net exe startup program if the Framework is not installed at startup,

Solution:

Startup.exe written by cup.exe is used as the startup program. It also checks whether the Framework is installed on the local machine. If not, c ++ calls start installation. After the Framework is installed, it starts the C # application.

Among them, the C ++ detection and installation startup program code is as follows: VC ++ 6.0 implements a hidden form:

 

[Cpp]
// StartUpDlg. cpp: implementation file
//
 
# Include "stdafx. h"
# Include "StartUp. h"
# Include "StartUpDlg. h"
// # Include "WinBase. h"
// # Include "shlobj. h"
// # Include "shellapi. h"
# Include "Tlhelp32.h"
 
# Ifdef _ DEBUG
# Define new DEBUG_NEW
# Undef THIS_FILE
Static char THIS_FILE [] = _ FILE __;
# Endif
 
//////////////////////////////////////// /////////////////////////////////////
// CStartUpDlg dialog
 
CStartUpDlg: CStartUpDlg (CWnd * pParent/* = NULL */)
: CDialog (CStartUpDlg: IDD, pParent)
{
// {AFX_DATA_INIT (CStartUpDlg)
// NOTE: the ClassWizard will add member initialization here
//} AFX_DATA_INIT
// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
M_hIcon = AfxGetApp ()-> LoadIcon (IDR_MAINFRAME );
}
 
Void CStartUpDlg: DoDataExchange (CDataExchange * pDX)
{
CDialog: DoDataExchange (pDX );
// {AFX_DATA_MAP (CStartUpDlg)
// NOTE: the ClassWizard will add DDX and DDV cballs here
//} AFX_DATA_MAP
}
 
BEGIN_MESSAGE_MAP (CStartUpDlg, CDialog)
// {AFX_MSG_MAP (CStartUpDlg)
ON_WM_PAINT ()
ON_WM_QUERYDRAGICON ()
ON_BN_CLICKED (btnCheckFramework, OnbtnCheckFramework)
//} AFX_MSG_MAP
END_MESSAGE_MAP ()
 
//////////////////////////////////////// /////////////////////////////////////
// CStartUpDlg message handlers
 
BOOL CStartUpDlg: OnInitDialog ()
{
CDialog: OnInitDialog ();
 
// Set the icon for this dialog. The framework does this automatically
// When the application's main window is not a dialog
SetIcon (m_hIcon, TRUE); // Set big icon
SetIcon (m_hIcon, FALSE); // Set small icon
OnbtnCheckFramework ();
// TODO: Add extra initialization here

Return TRUE; // return TRUE unless you set the focus to a control
}
 
// If you add a minimize button to your dialog, you will need the code below
// To draw the icon. For MFC applications using the document/view model,
// This is automatically done for you by the framework.
 
Void CStartUpDlg: OnPaint ()
{
If (IsIconic ())
{
// CPaintDC (this); // device context for painting
 
// SendMessage (WM_ICONERASEBKGND, (WPARAM) dc. GetSafeHdc (), 0 );
 
// Center icon in client rectangle
// Int cxIcon = GetSystemMetrics (SM_CXICON );
// Int cyIcon = GetSystemMetrics (SM_CYICON );
// CRect rect;
// GetClientRect (& rect );
// Int x = (rect. Width ()-cxIcon + 1)/2;
// Int y = (rect. Height ()-cyIcon + 1)/2;
 
// Draw the icon
// Dc. DrawIcon (x, y, m_hIcon );
}
Else
{
// CDialog: OnPaint ();
}
Static int I = 2;
If (I> 0)
{
I --;
ShowWindow (SW_HIDE );
}
Else
{
CDialog: OnPaint ();
}
}
 
// The system callthis to obtain the cursor to display while the user drags
// The minimized window.
HCURSOR CStartUpDlg: OnQueryDragIcon ()
{
Return (HCURSOR) m_hIcon;
}
 
// Detect the FrameWork Version
LPSTR regeditVision [] = {// "SOFTWARE \ Microsoft \ NET Framework Setup \ NDP \ v2.0 ",
// "SOFTWARE \ Microsoft \ NET Framework Setup \ NDP \ v2.0.50727 ",
// "SOFTWARE \ Microsoft \ NET Framework Setup \ NDP \ v3.0 ",
// "SOFTWARE \ Microsoft \ NET Framework Setup \ NDP \ v3.5 ",
"SOFTWARE \ Microsoft \ NET Framework Setup \ NDP \ v4.0 "};
 
// Check the registry
Bool CheckFrameworkRegedit ()
{
Bool result = TRUE;
// Determine whether the registry exists
// For (int I = 0; I <4; I ++)
//{
HKEY ck; // registry key

// Check whether the key value exists in the Registry
If (ERROR_SUCCESS = RegOpenKeyEx (HKEY_LOCAL_MACHINE,
RegeditVision [0], 0, KEY_ALL_ACCESS, & ck ))
{
RegCloseKey (ck); // closes the Registry
Result = TRUE;
// Break;
}
Else
{
Result = FALSE;
}
//}
Return result;
}
 
// Check the process based on the program name (the name in the Task Manager). If no value is found, 0 is returned and process number is returned.
DWORD GetProcessIdFromName (LPCTSTR name)
{
PROCESSENTRY32 pe;
DWORD id = 0;
HANDLE hSnapshot = createconlhelp32snapshot (TH32CS_SNAPPROCESS, 0 );
Pe. dwSize = sizeof (PROCESSENTRY32 );
If (! Process32First (hSnapshot, & pe ))
Return 0;
While (1)
{
Pe. dwSize = sizeof (PROCESSENTRY32 );
If (Process32Next (hSnapshot, & pe) = FALSE)
Break;
If (strcmp (pe. szExeFile, name) = 0)
{
Id = pe. th32ProcessID;
Break;
}
}
CloseHandle (hSnapshot );
Return id;
}
 
// Check whether. Net Framework 4.0 is installed. If not, call the installation package.
Void CStartUpDlg: OnbtnCheckFramework ()
{
// Check the registry to see if it complies with Framework4.0
If (CheckFrameworkRegedit ())
{
If (WinExec ("xxx.exe", SW_SHOWNORMAL) = ERROR_FILE_NOT_FOUND)
MessageBox ("failed to start xxx program! "," Prompt ", MB_ OK );
Else
{
// The process is suspended for 10 s to monitor the running status of xxx. if the process is closed, the monitoring program is terminated.
Sleep (10000 );
While (GetProcessIdFromName ("xxx.exe")> 0)
{
Sleep (1000 );
}
Sleep (2000 );
SendMessage (WM_CLOSE );
}
}
Else
{
If (MessageBox ("the application requires. NET Framework 4.0 installation package support. Are you sure you want to install it? "," Prompt ", MB_OKCANCEL) = IDOK)
{
WinExec ("framework/dotNetFx40_Full_x86_x64.exe", SW_SHOWNORMAL );
// The process is suspended for 3 s, and the Framework installation is monitored. After the installation is complete, the xxx program is started directly.
Sleep (3000 );
While (GetProcessIdFromName ("dotNetFx40_Full_x86_x64.exe")> 0)
{
Sleep (1000 );
}
// Close the Framework installation.
If (CheckFrameworkRegedit ())
{
If (WinExec ("OrgCertificate.exe", SW_SHOWNORMAL) = ERROR_FILE_NOT_FOUND)
MessageBox ("failed to start XXX program! "," Prompt ", MB_ OK );
Else
{
// The process is suspended for 10 s to monitor the running status of xxx. if the process is closed, the monitoring program is terminated.
Sleep (10000 );
While (GetProcessIdFromName ("xxx.exe")> 0)
{
Sleep (1000 );
}
Sleep (2000 );
SendMessage (WM_CLOSE );
}
}
Else
{
MessageBox ("Framework 4.0 Installation failed. Please reinstall it! "," Prompt ", MB_ OK );
}
}
SendMessage (WM_CLOSE );
}
}

Related Article

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.