因为程序是放在Ukey(U盘)中运行,不是Setup打包程序,所以启动时如果未安装Framework不能直接运行.net的exe启动程序,
解决方案是:
由C++写的Startup.exe做启动程序,同时检测本机是否安装Framework,如果没有则有c++调用启动安装,安装Framework结束后,启动C#应用程序。
其中C++的检测安装启动程序代码如下,VC++6.0实现,做了一个隐藏的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 calls 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 dc(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 calls this to obtain the cursor to display while the user drags
// the minimized window.
HCURSOR CStartUpDlg::OnQueryDragIcon()
{
return (HCURSOR) m_hIcon;
}
//检测FrameWork版本
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"};
//检查注册表
bool CheckFrameworkRegedit()
{
bool result=TRUE;
//判断注册表是否存在
//for (int i=0;i<4;i++)
//{
HKEY ck;//注册表的键
//检查注册表是否存在这个键值
if(ERROR_SUCCESS == RegOpenKeyEx(HKEY_LOCAL_MACHINE,
regeditVision[0],0,KEY_ALL_ACCESS,&ck))
{
RegCloseKey(ck);//关闭注册表
result=TRUE;
//break;
}
else
{
result=FALSE;
}
//}
return result;
}
//根据程序名称检测进程(任务管理器中的名称),没找到返回0,找到返回进程号
DWORD GetProcessIdFromName(LPCTSTR name)
{
PROCESSENTRY32 pe;
DWORD id=0;
HANDLE hSnapshot=CreateToolhelp32Snapshot(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;
}
//检测.Net Framework 4.0是否安装,未安装则进行安装包调用
void CStartUpDlg::OnbtnCheckFramework()
{
//检查注册表查看是否按照Framework4.0
if(CheckFrameworkRegedit())
{
if(WinExec("xxx.exe", SW_SHOWNORMAL) == ERROR_FILE_NOT_FOUND)
MessageBox("启动xxx程序失败!","提示",MB_OK);
else
{
//进程挂起10s,监测xxx运行情况,如果已经关闭则结束本监测程序
Sleep(10000);
while(GetProcessIdFromName("xxx.exe")>0)
{
Sleep(1000);
}
Sleep(2000);
SendMessage(WM_CLOSE);
}
}
else
{
if(MessageBox("应用程序需要.NET Framework 4.0安装包支持,确定要安装吗?","提示",MB_OKCANCEL) == IDOK)
{
WinExec("framework/dotNetFx40_Full_x86_x64.exe", SW_SHOWNORMAL);
//进程挂起3s,开始监测Framework安装情况,安装结束后直接启动xxx程序
Sleep(3000);
while(GetProcessIdFromName("dotNetFx40_Full_x86_x64.exe")>0)
{
Sleep(1000);
}
//关闭Framework安装结束
if(CheckFrameworkRegedit())
{
if(WinExec("OrgCertificate.exe", SW_SHOWNORMAL) == ERROR_FILE_NOT_FOUND)
MessageBox("启动XXX程序失败!","提示",MB_OK);
else
{
//进程挂起10s,监测xxx运行情况,如果已经关闭则结束本监测程序
Sleep(10000);
while(GetProcessIdFromName("xxx.exe")>0)
{
Sleep(1000);
}
Sleep(2000);
SendMessage(WM_CLOSE);
}
}
else
{
MessageBox("Framework 4.0安装失败,请重新安装!","提示",MB_OK);
}
}
SendMessage(WM_CLOSE);
}