C++ブラウザ閲覧履歴取得(Google、IE、火狐対応)
10824 ワード
Webコードを参照して、取得ブラウザのブラウズ履歴をクラスにカプセル化します.
ヘッダファイルhコードは以下の通りである.
クラス実装ファイルcpp
呼び出しdemo:
ヘッダファイルhコードは以下の通りである.
// FileName: BrowsHistory.h
// ------------------------------------------------------------------------------------------------------------------------
// Remarks:
// BrowsHistory , ; , ;
// ------------------------------------------------------------------------------------------------------------------------
#pragma once
#include <iostream>
#include <algorithm>
#include <functional>
#include <vector>
#include "afxwin.h"
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;
};
クラス実装ファイルcpp
// // FileName: BrowsHistory.cpp
#include "stdafx.h" //
#include "BrowseHistory.h"
#include <wininet.h>
#include "Common\\CppSQLite3.h"
#include <shlobj.h>
#include "Shlwapi.h"
#include "tlhelp32.h"
#include <atlconv.h>
#pragma comment(lib,"Shlwapi.lib")
#pragma comment(lib,"Common\\sqlite3.lib")
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("/"));
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)
{
//
CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE)ThreadPro, this, 0, NULL);
}
void BrowsHistory::InitHistroy (void)
{
//
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))
{
// , ;
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();
//
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>());
}
呼び出しdemo:
// MainTest.cpp : 。
//
#include "stdafx.h"
#include <vector>
#include <iostream>
#include "afxwin.h"
#include "BrowseHistory.h"
using namespace std;
int _tmain(int argc, _TCHAR* argv[])
{
BrowsHistory browseHistory;
browseHistory.Init();
while(!browseHistory.m_bStatus);//
std::vector<BrowsData> data=browseHistory.GetBrowsHistory();
std::vector<BrowsData>::iterator it=data.begin();
for(;it<data.end();++it)
{
wprintf(_T("%s
"),it->strURL);
}
getchar();
return 0;
}