C++獲取多瀏覽器上網(wǎng)歷史記錄示例代碼(支持獲取IE/Chrome/FireFox)
// FileName: BrowsHistory.h
// ------------------------------------------------------------------------------------------------------------------------
// Remarks:
// BrowsHistory對象應(yīng)該設(shè)置成全局,或者靜態(tài);防止還沒有獲取完網(wǎng)址,對象就析構(gòu)了;
// ------------------------------------------------------------------------------------------------------------------------
#pragma once
#include <iostream>
#include <algorithm>
#include <functional>
#include <vector>
struct BrowsData
{
public:
// 網(wǎng)址
CString strURL;
// 對應(yīng)網(wǎng)址訪問次數(shù)
unsigned int nCount;
// 重載<操作符
bool operator < (const BrowsData &m)const
{
return nCount > m.nCount;
}
};
class BrowsHistory
{
private:
// 保存獲得的網(wǎng)址和訪問次數(shù)
std::vector<BrowsData> m_BrowsHistroy;
private:
// IE網(wǎng)址過濾,如只取網(wǎng)址com前邊的
void urlFiltrateIE (LPWSTR lpszSourceUrlName);
// Chrome網(wǎng)址過濾,如只取網(wǎng)址com前邊的
void urlFiltrateChrome (CString strUrlName);
// Firefox網(wǎng)址過濾,如只去網(wǎng)址com前邊的
void urlFiltrateFirefox (CString strUrlName, int nCount);
// 查詢進程是否已存在, 返回true表示存在;自動結(jié)束其進程
bool IsRunning(CString exe);
// 編碼轉(zhuǎn)換
void ConvertUtf8ToGBK(CStringA &strUtf8);
// 獲取瀏覽器歷史記錄
void InitHistroy (void);
// 多線程函數(shù)
static void ThreadPro (LPVOID * ptr);
// 對獲得的網(wǎng)址進行排序
void Sort (void);
public:
BrowsHistory();
~BrowsHistory();
// 獲取網(wǎng)址的進程,是否執(zhí)行完;執(zhí)行完時為true;
bool m_bStatus;
// 初始化
void Init (void);
// 獲取瀏覽器歷史記錄
std::vector<BrowsData> GetBrowsHistory(void) const;
};
// // FileName: BrowsHistory.cpp
#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;
// 排除非必要的網(wǎng)址
if (strTemp.Find(_T("@http://")) != -1)
{
strTemp.Delete(0, strTemp.Find(_T("@http://"))+8);
// 排除非必要網(wǎng)址
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("/"));
BrowsData browsDate;
browsDate.nCount = 0;
std::vector<BrowsData>::iterator iter;
if (nIndex != -1)
{
for (iter=m_BrowsHistroy.begin(); iter != m_BrowsHistroy.end(); iter++)
{
if (iter->strURL == strUrlName.Left(nIndex))
{
iter->nCount += 1;
return;
}
}
browsDate.strURL = strUrlName.Left(nIndex);
browsDate.nCount = 1;
m_BrowsHistroy.push_back(browsDate);
}
else
{
for (iter=m_BrowsHistroy.begin(); iter != m_BrowsHistroy.end(); iter++)
{
if (iter->strURL == strUrlName)
{
iter->nCount += 1;
return;
}
}
browsDate.strURL = strUrlName;
browsDate.nCount = 1;
m_BrowsHistroy.push_back(browsDate);
}
}
void BrowsHistory::urlFiltrateFirefox (CString strUrlName, int nCount)
{
BrowsData browsDate;
browsDate.nCount = 0;
int nIndex = strUrlName.Find(_T("/"));
if (nIndex != -1)
{
strUrlName = strUrlName.Left(nIndex);
}
std::vector<BrowsData>::iterator iter;
for (iter=m_BrowsHistroy.begin(); iter != m_BrowsHistroy.end(); iter++)
{
if (iter->strURL == strUrlName)
{
iter->nCount += nCount;
return;
}
}
browsDate.strURL = strUrlName;
browsDate.nCount += nCount;
m_BrowsHistroy.push_back(browsDate);
}
bool BrowsHistory::IsRunning(CString exe)
{
PROCESSENTRY32 pe32;
HANDLE hprocess;
hprocess = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS,0);
pe32.dwSize = sizeof(PROCESSENTRY32);
if(Process32First(hprocess,&pe32))
{
do
{
HANDLE h_id;
h_id = OpenProcess(PROCESS_TERMINATE,false,pe32.th32ProcessID);
CString exefile;
exefile=pe32.szExeFile;
exefile.MakeLower();
exe.MakeLower();
if(exefile==exe)
{
if (TerminateProcess(h_id, 0) !=0)
{
return FALSE;
}
else
{
return TRUE;
}
}
}
while(Process32Next(hprocess,&pe32));
}
return FALSE;
}
void BrowsHistory::ConvertUtf8ToGBK(CStringA &strUtf8)
{
int len=MultiByteToWideChar(CP_UTF8, 0, (LPCSTR)strUtf8, -1, NULL,0);
unsigned short * wszGBK = new unsigned short[len+1];
memset(wszGBK, 0, len * 2 + 2);
MultiByteToWideChar(CP_UTF8, 0, (LPCSTR)strUtf8, -1,(LPWSTR) wszGBK, len);
len = WideCharToMultiByte(CP_ACP, 0, (LPWSTR)wszGBK, -1, NULL, 0, NULL, NULL);
char *szGBK=new char[len + 1];
memset(szGBK, 0, len + 1);
WideCharToMultiByte (CP_ACP, 0, (LPWSTR)wszGBK, -1, szGBK, len, NULL,NULL);
strUtf8 = szGBK;
delete[] szGBK;
delete[] wszGBK;
}
void BrowsHistory::Init (void)
{
// 創(chuàng)建一個線程
CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE)ThreadPro, this, 0, NULL);
}
void BrowsHistory::InitHistroy (void)
{
// 用來支持多次調(diào)用
m_bStatus = false;
m_BrowsHistroy.clear();
// 獲取IE的歷史記錄
HANDLE hCacheEnumHandle = NULL;
LPINTERNET_CACHE_ENTRY_INFO lpCacheEntry = NULL;
DWORD dwSize = 4096;
BrowsData browsDate;
browsDate.nCount = 0;
lpCacheEntry = (LPINTERNET_CACHE_ENTRY_INFO) new char[dwSize];
lpCacheEntry->dwStructSize = dwSize;
hCacheEnumHandle = FindFirstUrlCacheEntry(_T("visited:"), lpCacheEntry, &dwSize);
if(hCacheEnumHandle != NULL)
{
urlFiltrateIE(lpCacheEntry->lpszSourceUrlName);
}
else
{
switch(GetLastError())
{
case ERROR_INSUFFICIENT_BUFFER:
lpCacheEntry = (LPINTERNET_CACHE_ENTRY_INFO) new char[dwSize];
lpCacheEntry->dwStructSize = dwSize;
hCacheEnumHandle = FindFirstUrlCacheEntry(_T("visited:"), lpCacheEntry,
&dwSize);
if (hCacheEnumHandle != NULL)
{
urlFiltrateIE(lpCacheEntry->lpszSourceUrlName);
break;
}
else
{
// 查找失敗
return;
}
default:
{
FindCloseUrlCache(hCacheEnumHandle);
}
}
}
bool bSign = true;
do
{
if (FindNextUrlCacheEntry(hCacheEnumHandle, lpCacheEntry, &dwSize))
{
urlFiltrateIE(lpCacheEntry->lpszSourceUrlName);
}
else
{
switch(GetLastError())
{
case ERROR_INSUFFICIENT_BUFFER:
lpCacheEntry =
(LPINTERNET_CACHE_ENTRY_INFO) new char[dwSize];
memset(lpCacheEntry,0,dwSize);
lpCacheEntry->dwStructSize = dwSize;
if (FindNextUrlCacheEntry(hCacheEnumHandle, lpCacheEntry,
&dwSize))
{
urlFiltrateIE(lpCacheEntry->lpszSourceUrlName);
break;
}
else
{
FindCloseUrlCache(hCacheEnumHandle);
bSign = false;
break;
}
break;
case ERROR_NO_MORE_ITEMS:
FindCloseUrlCache(hCacheEnumHandle);
bSign = false;
break;
default:
FindCloseUrlCache(hCacheEnumHandle);
bSign = false;
break;
}
}
} while (bSign);
// 獲取谷歌瀏覽器的歷史記錄
char path[MAX_PATH];
::SHGetSpecialFolderPathA(NULL,path,CSIDL_LOCAL_APPDATA,FALSE);
strcat_s(path,"\\google\\chrome\\User Data\\default\\History");
if (PathFileExistsA(path))
{
// 谷歌瀏覽器正在運行,強制關(guān)閉;關(guān)閉后才能得到谷歌瀏覽器的歷史記錄
if (!IsRunning(_T("chrome.exe")))
{
try
{
CppSQLite3DB db;
CppSQLite3Query query;
db.open(path);
query=db.execQuery("select url from urls");
while(!query.eof())
{
CStringA utf8url;
utf8url=query.fieldValue("url");
ConvertUtf8ToGBK(utf8url);
urlFiltrateChrome((CString)utf8url);
query.nextRow();
}
db.close();
}
catch (CppSQLite3Exception& e)
{
return;
}
}
}
// 獲取火狐瀏覽器的歷史記錄
TCHAR strPath[MAX_PATH] = {0};
GetModuleFileName(NULL, strPath, MAX_PATH);
CString strPathTemp(strPath);
int nPosition = strPathTemp.ReverseFind(_T('\\'));
if (nPosition != -1)
{
USES_CONVERSION;
strPathTemp = strPathTemp.Left(nPosition);
::SHGetSpecialFolderPathA(NULL, path, CSIDL_WINDOWS, FALSE);
CString strDestPath(path);
strPathTemp += _T("\\MozillaCacheView.exe /stext ");
strDestPath += _T("\\temp.dat");
strPathTemp += strDestPath;
// 文件路徑中不能有空格
WinExec(T2A(strPathTemp), SW_HIDE);
// 延時,防止讀寫沖突
Sleep(1000);
if (PathFileExists(strDestPath))
{
CStdioFile file;
CString buffer;
if(file.Open(strDestPath, CFile::modeRead))
{
CString strTemp;
while(file.ReadString(buffer))
{
if (buffer.Find(_T("image/x-icon")) != -1)
{
file.ReadString(buffer);
buffer.Delete(0, buffer.Find(_T("http://"))+7);
file.ReadString(strTemp);
file.ReadString(strTemp);
strTemp.Delete(0, strTemp.Find(_T(": "))+2);
urlFiltrateFirefox(buffer, atoi(T2A(strTemp)));
}
}
}
}
}
Sort();
}
void BrowsHistory::ThreadPro (LPVOID * ptr)
{
BrowsHistory * pBrowsHistroy = (BrowsHistory*)ptr;
pBrowsHistroy->InitHistroy();
// 獲取網(wǎng)址的函數(shù)執(zhí)行完了
pBrowsHistroy->m_bStatus = true;
}
std::vector<BrowsData> BrowsHistory::GetBrowsHistory (void) const
{
return m_BrowsHistroy;
}
void BrowsHistory::Sort (void)
{
stable_sort(m_BrowsHistroy.begin(), m_BrowsHistroy.end(),std::less<BrowsData>());
}
上一篇:c語言打印輸出雙引號的方法示例
欄 目:C語言
下一篇:c語言實現(xiàn)的貨物管理系統(tǒng)實例代碼(增加刪除 查找貨物信息等功能)
本文標(biāo)題:C++獲取多瀏覽器上網(wǎng)歷史記錄示例代碼(支持獲取IE/Chrome/FireFox)
本文地址:http://mengdiqiu.com.cn/a1/Cyuyan/3898.html
您可能感興趣的文章
- 04-02c語言沒有round函數(shù) round c語言
- 01-10如何判斷一個數(shù)是否為2的冪次方?若是,并判斷出來是多少次方
- 01-10深入理解C++中常見的關(guān)鍵字含義
- 01-10使用C++實現(xiàn)全排列算法的方法詳解
- 01-10如何判斷一個數(shù)是否為4的冪次方?若是,并判斷出來是多少次方
- 01-10c++中inline的用法分析
- 01-10用C++實現(xiàn)DBSCAN聚類算法
- 01-10全排列算法的非遞歸實現(xiàn)與遞歸實現(xiàn)的方法(C++)
- 01-10C++大數(shù)模板(推薦)
- 01-10淺談C/C++中的static與extern關(guān)鍵字的使用詳解


閱讀排行
本欄相關(guān)
- 04-02c語言函數(shù)調(diào)用后清空內(nèi)存 c語言調(diào)用
- 04-02func函數(shù)+在C語言 func函數(shù)在c語言中
- 04-02c語言的正則匹配函數(shù) c語言正則表達
- 04-02c語言用函數(shù)寫分段 用c語言表示分段
- 04-02c語言中對數(shù)函數(shù)的表達式 c語言中對
- 04-02c語言編寫函數(shù)冒泡排序 c語言冒泡排
- 04-02c語言沒有round函數(shù) round c語言
- 04-02c語言分段函數(shù)怎么求 用c語言求分段
- 04-02C語言中怎么打出三角函數(shù) c語言中怎
- 04-02c語言調(diào)用函數(shù)求fibo C語言調(diào)用函數(shù)求
隨機閱讀
- 01-10C#中split用法實例總結(jié)
- 01-10SublimeText編譯C開發(fā)環(huán)境設(shè)置
- 08-05織夢dedecms什么時候用欄目交叉功能?
- 04-02jquery與jsp,用jquery
- 01-11Mac OSX 打開原生自帶讀寫NTFS功能(圖文
- 01-11ajax實現(xiàn)頁面的局部加載
- 08-05dedecms(織夢)副欄目數(shù)量限制代碼修改
- 01-10使用C語言求解撲克牌的順子及n個骰子
- 08-05DEDE織夢data目錄下的sessions文件夾有什
- 01-10delphi制作wav文件的方法