MFC软件卸载代码

// m_Software.cpp : 实现文件
//

#include "stdafx.h"
#include "MFCApplication1.h"
#include "m_Software.h"
#include "afxdialogex.h"


// m_Software 对话框
HANDLE hThread5;
m_Software *m_this;



DWORD WINAPI ThreadPro5(LPVOID lpThreadParameter)
{
	CRect rt;
	m_this->MyList.GetClientRect(rt);
	m_this->MyList.SetExtendedStyle(LVS_EX_FULLROWSELECT);
	m_this->MyList.InsertColumn(0,_T("软件名"),0,rt.Width( ) / 4);
	m_this->MyList.InsertColumn(1,_T("厂商"),0,rt.Width( ) / 4);
	m_this->MyList.InsertColumn(2,_T("安装路径"),0,rt.Width( ) / 4);
	m_this->MyList.InsertColumn(3,_T("卸载路径"),0,rt.Width( ) / 4);
	struct _SoftInfo
	{
		// 软件名
		WCHAR m_strSoftName[50];
		// 软件版本号
		WCHAR m_strSoftVersion[50];
		// 软件安装目录
		WCHAR m_strInstallLocation[MAX_PATH];
		// 软件发布厂商
		WCHAR m_strPublisher[50];
		// 主程序所在完整路径
		WCHAR m_strMainProPath[MAX_PATH];
		// 卸载exe所在完整路径
		WCHAR m_strUninstallPth[MAX_PATH];
	}SoftInfo;
	// 主键
	HKEY RootKey; 
	// 子键名称
	LPCTSTR lpSubKey;   
	// 将要打开键的句柄 
	HKEY hkResult;      
	// 记录读取注册表是否成功
	LONG lReturn;            
	CString strBuffer;
	CString strMidReg;
	int i = 0;
	DWORD index = 0;
	TCHAR szKeyName[255] = {0};        // 注册表项名称
	TCHAR szBuffer[255] = {0};
	DWORD dwKeyLen = 255;
	DWORD dwNameLen = 255;
	DWORD dwType = REG_BINARY | REG_DWORD | REG_EXPAND_SZ | REG_MULTI_SZ | REG_NONE | REG_SZ;
	CString *name = new CString;
	RootKey = HKEY_LOCAL_MACHINE;
	lpSubKey = _T("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Uninstall");
	//lpSubKey = _T("SOFTWARE\\Wow6432Node\\Microsoft\\Windows\\CurrentVersion\\Uninstall");
	bool isOK = false;
	lReturn = RegOpenKeyEx(RootKey,lpSubKey,0,KEY_ALL_ACCESS,&hkResult);
	DWORD dwIndex = 0;
	while(true)
	{
		DWORD dwKeyLen = 255;
		WCHAR szNewKeyName[MAX_PATH] = {};
		LONG lReturn = RegEnumKeyEx(hkResult,dwIndex,szNewKeyName,&dwKeyLen,0,NULL,NULL,NULL);
		OutputDebugString(szNewKeyName);
		WCHAR strMidReg[MAX_PATH] = {};
		swprintf_s(strMidReg,L"%s%s%s",lpSubKey,L"\\",szNewKeyName);
		HKEY hkValueKey = 0;
		RegOpenKeyEx(RootKey,strMidReg,0,KEY_QUERY_VALUE,&hkValueKey);

		DWORD dwNameLen = 255;
		RegQueryValueEx(hkValueKey,L"DisplayName",0,&dwType,(LPBYTE)SoftInfo.m_strSoftName,&dwNameLen);
		dwNameLen = 255;

		if(SoftInfo.m_strSoftName == name[0])//去除重复
		{
			dwIndex++;
			continue;
		}
		name[0] = SoftInfo.m_strSoftName;

		m_this->MyList.InsertItem(i,NULL);
		m_this->MyList.SetItemText(i,0,SoftInfo.m_strSoftName);

		RegQueryValueEx(hkValueKey,L"Publisher",0,&dwType,(LPBYTE)SoftInfo.m_strPublisher,&dwNameLen);
		dwNameLen = 255;
		m_this->MyList.SetItemText(i,1,SoftInfo.m_strPublisher);
		RegQueryValueEx(hkValueKey,L"InstallLocation",0,&dwType,(LPBYTE)SoftInfo.m_strInstallLocation,&dwNameLen);
		dwNameLen = 255;
		m_this->MyList.SetItemText(i,2,SoftInfo.m_strInstallLocation);
		RegQueryValueEx(hkValueKey,L"UninstallString",0,&dwType,(LPBYTE)SoftInfo.m_strUninstallPth,&dwNameLen);
		dwNameLen = 255;
		m_this->MyList.SetItemText(i,3,SoftInfo.m_strUninstallPth);

		i++;
		dwIndex++;
		if(lReturn == ERROR_NO_MORE_ITEMS)
		{
			break;
		}
	}

	return 0;
}




IMPLEMENT_DYNAMIC(m_Software, CDialog)

m_Software::m_Software(CWnd* pParent /*=NULL*/)
	: CDialog(m_Software::IDD, pParent)
{

}

m_Software::~m_Software()
{
}

void m_Software::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	DDX_Control(pDX,IDC_BUTTON1,m_XieZaii);
	DDX_Control(pDX,IDC_PROGRESS2,MyList);
}


BEGIN_MESSAGE_MAP(m_Software, CDialog)
	ON_BN_CLICKED(IDCANCEL,&m_Software::OnBnClickedCancel)
	ON_BN_CLICKED(IDC_BUTTON1,&m_Software::OnBnClickedButton1)
END_MESSAGE_MAP( )


// m_Software 消息处理程序

//取消按钮
void m_Software::OnBnClickedCancel( )
{
	// TODO:  在此添加控件通知处理程序代码
	CDialog::OnCancel( );
}


//卸载按钮
void m_Software::OnBnClickedButton1( )
{
	// TODO:  在此添加控件通知处理程序代码
	CString str1;
	int nId;
	POSITION pos = MyList.GetFirstSelectedItemPosition( );//找到位置
	nId = (int)MyList.GetNextSelectedItem(pos);//选择的是第几行
	str1 = MyList.GetItemText(nId,3);//获取内容,0代表第一列
	ShellExecute(NULL,NULL,_T("explorer"),str1,NULL,SW_SHOW);
}


BOOL m_Software::OnInitDialog( )
{
	CDialog::OnInitDialog( );

	// TODO:  在此添加额外的初始化



// 	CRect rc;
// 	::GetClientRect(AfxGetApp( )->m_pMainWnd->m_hWnd,rc);
// 	rc.DeflateRect(0,15,40,90);
// 	MoveWindow(rc);

	//显示出来
	m_this = this;
	hThread5 = CreateThread(NULL,NULL,ThreadPro5,(LPVOID)1,NULL,NULL);

	return TRUE;  // return TRUE unless you set the focus to a control
	// 异常:  OCX 属性页应返回 FALSE
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值