
// ------------------------------------------------------------------------------------------------------------------------
// Remarks:
// BrowsHistory对象应该设置成全局,或者静态;防止还没有获取完网址,对象就析构了;
// ------------------------------------------------------------------------------------------------------------------------
#pragma once
#include <iostream>
#include <algorithm>
#include <functional>
#include <vector>
struct BrowsData
{
public:
// 网址
CString strURL;
// 对应网址访问次数
unsigned int nCount;
// 重载<操作符
bool operator < (const BrowsData &m)const
{
return nCount > m.nCount;
}
};
class BrowsHistory
{
private:
// 保存获得的网址和访问次数
std::vector<BrowsData> m_BrowsHistroy;
private:
// IE网址过滤,如只取网址com前边的
void urlFiltrateIE (LPWSTR lpszSourceUrlName);
// Chrome网址过滤,如只取网址com前边的
void urlFiltrateChrome (CString strUrlName);
// Firefox网址过滤,如只去网址com前边的
void urlFiltrateFirefox (CString strUrlName, int nCount);
// 查询进程是否已存在, 返回true表示存在;自动结束其进程
bool IsRunning(CString exe);
// 编码转换
void ConvertUtf8ToGBK(CStringA &strUtf8);
// 获取浏览器历史记录
void InitHistroy (void);
// 多线程函数
static void ThreadPro (LPVOID * ptr);
// 对获得的网址进行排序
void Sort (void);
public:
BrowsHistory();
~BrowsHistory();
// 获取网址的进程,是否执行完;执行完时为true;
bool m_bStatus;
// 初始化
void Init (void);
// 获取浏览器历史记录
std::vector<BrowsData> GetBrowsHistory(void) const;
};
</div>
#include "stdafx.h" // 如果编译出错请删除此行
#include "BrowsHistory.h"
#include <wininet.h>
#include "Common\\CppSQLite3.h"
#include <shlobj.h>
#include "Shlwapi.h"
#pragma comment(lib,"Shlwapi.lib")
#include "tlhelp32.h"
#pragma comment(lib,"common\\sqlite3.lib")
#include <atlconv.h>
BrowsHistory::BrowsHistory()
{
m_bStatus = false;
}
BrowsHistory::~BrowsHistory()
{
}
void BrowsHistory::urlFiltrateIE (LPWSTR lpszSourceUrlName)
{
BrowsData browsDate;
browsDate.nCount = 0;
CString strTemp(lpszSourceUrlName);
std::vector<BrowsData>::iterator iter;
// 排除非必要的网址
if (strTemp.Find(_T("@http://")) != -1)
{
strTemp.Delete(0, strTemp.Find(_T("@http://"))+8);
// 排除非必要网址
if (strTemp.Find(_T(":")) != -1)
{
return;
}
int nIndex = strTemp.Find(_T("/"));
if (nIndex != -1)
{
for (iter=m_BrowsHistroy.begin(); iter != m_BrowsHistroy.end(); iter++)
{
if (iter->strURL == strTemp.Left(nIndex))
{
iter->nCount += 1;
return;
}
}
browsDate.strURL = strTemp.Left(nIndex);
browsDate.nCount = 1;
m_BrowsHistroy.push_back(browsDate);
}
else
{
for (iter=m_BrowsHistroy.begin(); iter != m_BrowsHistroy.end(); iter++)
{
if (iter->strURL == strTemp)
{
iter->nCount += 1;
return;
}
}
browsDate.strURL = strTemp;
browsDate.nCount = 1;
m_BrowsHistroy.push_back(browsDate);
}
}
}
void BrowsHistory::urlFiltrateChrome (CString strUrlName)
{
// 删除开始的"https://"
if (strUrlName.Find(_T("https://")) != -1)
{
strUrlName.Delete(0, 8);
}
else if(strUrlName.Find(_T("http://")) != -1)
{
strUrlName.Delete(0, 7);
}
int nIndex = strUrlName.Find(_T("/"));
BrowsDa