Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #pragma comment(lib, "Wininet")
- #include "Windows.h"
- #include <Windows.h>
- #include <WinInet.h>
- #include <tlhelp32.h>
- #include <iostream>
- #include <tchar.h>
- #include <conio.h>
- #include <tlhelp32.h>
- #include <stdio.h>
- #include <string>
- #include <vector>
- #include <algorithm>
- #define MAX_KEY_LENGTH 255
- #define MAX_VALUE_NAME 16383
- #define BUF_SIZE 5000
- PCTSTR accept_types[] = { TEXT("*/*"), 0 };
- using namespace std;
- DWORD WINAPI secondThreadFunction(LPVOID lpParam);
- HANDLE CreateAndReturnFileMapping(const char* name)
- {
- HANDLE hMap = CreateFileMapping(INVALID_HANDLE_VALUE, 0, PAGE_READWRITE, 0, BUF_SIZE, name);
- if (hMap == NULL)
- {
- printf("CreateFileMapping failed with error 0x%08x\n", GetLastError());
- return NULL;
- }
- return hMap;
- }
- HANDLE CreateAndReturnMutex(HANDLE hMap, const char* name)
- {
- HANDLE hMutex = CreateMutex(NULL, FALSE, name);
- if (hMutex == NULL)
- {
- printf("CreateMutex failed with error 0x%08x\n", GetLastError());
- CloseHandle(hMap);
- return NULL;
- }
- return hMutex;
- }
- LPTSTR CreateAndReturnMapViewOfFile(HANDLE hMap, HANDLE hSync)
- {
- LPTSTR pMap = (LPTSTR)MapViewOfFile(hMap, FILE_MAP_WRITE, 0, 0, BUF_SIZE);
- if (pMap == NULL)
- {
- printf("MapViewOfFile failed with error 0x%08x\n", GetLastError());
- CloseHandle(hMap);
- CloseHandle(hSync);
- return NULL;
- }
- return pMap;
- }
- string get_server_link(string &link) {
- for (int i = 8; i < (int)link.size(); i++)
- if (link[i] == '/')
- return link.substr(8, i - 8);
- return "";
- }
- string get_name(string &link) {
- for (int i = 8; i < (int)link.size(); i++)
- if (link[i] == '/')
- return link.substr(i + 1);
- return "";
- }
- vector<string> getAllLinks(string &html) {
- vector<string> links;
- for (int i = 0; i + 5 < html.size(); ++i) {
- if (html.substr(i, 6) != "href=\"")
- continue;
- int j = i + 6;
- string link;
- while (html[j] != '"')
- link += html[j++];
- if (link.size())
- links.push_back(link);
- i = j;
- }
- sort(links.begin(), links.end());
- links.erase(unique(links.begin(), links.end()), links.end());
- return links;
- }
- vector<string> getFilteredLinks(vector<string>links) {
- vector<string> filtered;
- for (auto currentLink : links) {
- //cout << currentLink << "\n";
- if (currentLink.substr(currentLink.size() - 3, currentLink.size()) != "pdf" &&
- currentLink.substr(currentLink.size() - 3, currentLink.size()) != "css" &&
- currentLink.substr(currentLink.size() - 3, currentLink.size()) != "rss" &&
- currentLink.substr(currentLink.size() - 3, currentLink.size()) != "png" &&
- currentLink.substr(currentLink.size() - 3, currentLink.size()) != "zip") {
- //cout << currentLink << "\n";
- filtered.push_back(currentLink);
- }
- }
- return filtered;
- }
- DWORD WINAPI firstThreadFunction(LPVOID lpParam) {
- cout << "first";
- HKEY keyHandle;
- TCHAR achClass[MAX_PATH] = TEXT("");
- DWORD cchClassName = MAX_PATH;
- DWORD cSubKeys = 0;
- DWORD cbMaxSubKey;
- DWORD cchMaxClass;
- DWORD cValues;
- DWORD cchMaxValue;
- DWORD cbMaxValueData;
- DWORD cbSecurityDescriptor;
- FILETIME ftLastWriteTime;
- TCHAR achValue[MAX_VALUE_NAME];
- DWORD cchValue = MAX_VALUE_NAME;
- DWORD typeValue = REG_SZ;
- LPBYTE dataValue = NULL;
- DWORD cdataValue;
- HANDLE hEvent1 = CreateEvent(0, 0, 0, TEXT("firstEvent"));
- if (hEvent1 == NULL) {
- cout << "Couldn't create first event" << GetLastError();
- }
- HANDLE hEvent2 = CreateEvent(0, 0, 0, TEXT("secondEvent"));
- if (hEvent2 == NULL) {
- cout << "Couldn't create second event" << GetLastError();
- }
- HANDLE hmap = CreateFileMapping(0, 0, PAGE_EXECUTE_READWRITE, 0, 1000 * sizeof(DWORD), TEXT("fisier_threaduri"));
- if (hmap == NULL) {
- cout << "Couldn't create file map" << GetLastError();
- }
- LPTSTR p = LPTSTR(MapViewOfFile(hmap, FILE_MAP_ALL_ACCESS, 0, 0, 0));
- if (p == NULL) {
- cout << "Couldn't create pointer" << GetLastError();
- CloseHandle(hmap);
- }
- HANDLE hThread2 = CreateThread(NULL, 0, secondThreadFunction, NULL, 0, NULL);
- if (hThread2 == NULL) {
- cout << "Err create second thread" << GetLastError() << endl;
- }
- DWORD resOpen = RegOpenKeyEx(HKEY_LOCAL_MACHINE, _T("SOFTWARE\\WebCrawler"), 0, KEY_ALL_ACCESS | KEY_WOW64_64KEY, &keyHandle);
- if (ERROR_SUCCESS != resOpen) {
- cout << "Couldn't open key\n";
- cout << resOpen;
- }
- DWORD retCode = RegQueryInfoKey(
- keyHandle, // key handle
- achClass, // buffer for class name
- &cchClassName, // size of class string
- NULL, // reserved
- &cSubKeys, // number of subkeys
- &cbMaxSubKey, // longest subkey size
- &cchMaxClass, // longest class string
- &cValues, // number of values for this key
- &cchMaxValue, // longest value name
- &cbMaxValueData, // longest value data
- &cbSecurityDescriptor, // security descriptor
- &ftLastWriteTime); // last write time
- HINTERNET hOpen = InternetOpen("VicTalif", INTERNET_OPEN_TYPE_PRECONFIG, NULL, NULL, NULL);
- if (hOpen == NULL)
- {
- cout << "Err InternetOpen" << GetLastError() << endl;
- exit(0);
- }
- if (cValues)
- {
- cout << "number of values: " << cValues;
- for (DWORD i = 0, retCode = ERROR_SUCCESS; i < cValues; i++)
- {
- WaitForSingleObject(hEvent1, INFINITE);
- cchValue = MAX_VALUE_NAME;
- cdataValue = MAX_VALUE_NAME;
- achValue[0] = '\0';
- retCode = RegEnumValue(keyHandle, i, achValue, &cchValue, NULL, &typeValue, NULL, NULL);
- if (retCode != ERROR_SUCCESS)
- cerr << "err reg enum value" << retCode;
- cout << "\n" << i + 1 << " " << achValue;
- string link = string(achValue);
- string server_value = get_server_link(link);
- string name_value = get_name(link);
- cout << "HERE : "<< server_value.c_str() << endl;
- HINTERNET hConnect = InternetConnect(hOpen, TEXT(server_value.c_str()), INTERNET_INVALID_PORT_NUMBER, 0, 0, INTERNET_SERVICE_HTTP, 0, 0);
- if (hConnect == NULL)
- {
- cout << "Err InternetConnect" << GetLastError() << endl;
- }
- HINTERNET hRequest = HttpOpenRequest(hConnect, TEXT("GET"), TEXT(name_value.c_str()), TEXT("1.1"), 0, accept_types, 0, 0);
- if (hOpen == NULL)
- {
- InternetCloseHandle(hConnect);
- cout << "Err HttpOpenRequest" << GetLastError() << endl;
- exit(0);
- }
- HttpSendRequest(hRequest, 0, 0, 0, 0);
- char* content = new char[1025];
- DWORD count_read;
- string html_content;
- while (InternetReadFile(hRequest, content, 1024, &count_read)) {
- if (!count_read)
- break;
- content[count_read] = 0;
- html_content += content;
- }
- //cout << html_content;
- vector<string> links = getAllLinks(html_content);
- /*for (auto it : links)
- cout << it << '\n';*/
- vector<string> filteredLinks = getFilteredLinks(links);
- /*for (auto it : filteredLinks)
- cout << it << '\n';*/
- string linksBuff;
- for (auto it : filteredLinks) {
- cout << it << endl;
- linksBuff += it + "\n";
- }
- //cout << "\n string buff " << linksBuff;
- CopyMemory(p, linksBuff.c_str(), linksBuff.size() * sizeof(linksBuff[0]));
- SetEvent(hEvent2);
- //Sleep(1000);
- }
- }
- return 0;
- }
- DWORD WINAPI secondThreadFunction(LPVOID lpParam) {
- cout << "second";
- HANDLE hEvent1 = OpenEvent(EVENT_ALL_ACCESS, 0, TEXT("firstEvent"));
- if (hEvent1 == NULL) {
- cout << "Couldn't create first event" << GetLastError();
- }
- HANDLE hEvent2 = OpenEvent(EVENT_ALL_ACCESS, 0, TEXT("secondEvent"));
- if (hEvent2 == NULL) {
- cout << "Couldn't create second event" << GetLastError();
- }
- HANDLE hMap = OpenFileMapping(FILE_MAP_ALL_ACCESS, FALSE, TEXT("fisier_threaduri"));
- if (hMap == INVALID_HANDLE_VALUE) {
- cout << "Could not open file map " << GetLastError();
- CloseHandle(hMap);
- }
- SetEvent(hEvent1);
- WaitForSingleObject(hEvent2, INFINITE);
- LPSTR buff = (LPSTR)(MapViewOfFile(hMap, FILE_MAP_ALL_ACCESS, 0, 0, 0));
- if (buff == NULL) {
- CloseHandle(hMap);
- cout << "err map view";
- }
- string strBuff = string((char*)buff);
- cout << "\nThread 2\n" << strBuff;
- HANDLE hMap2 = CreateAndReturnFileMapping("ProjectFile");
- if (hMap2 == NULL) return 0;
- HANDLE hMutex = CreateAndReturnMutex(hMap2, "shared_mutex");
- if (hMutex == NULL) return 0;
- LPTSTR pMap = CreateAndReturnMapViewOfFile(hMap2, hMutex);
- if (pMap == NULL) return 0;
- if (WaitForSingleObject(hMutex, INFINITE) != WAIT_OBJECT_0)
- {
- printf("WaitForSingleObject failed with error 0x%08x\n", GetLastError());
- ReleaseMutex(hMutex);
- CloseHandle(hMutex);
- CloseHandle(hMap2);
- UnmapViewOfFile(pMap);
- return 0;
- }
- if (!CopyMemory(pMap, strBuff.c_str(), strBuff.size()*sizeof(strBuff[0]))) {
- printf("CopyMemory failed with error 0x%08x\n", GetLastError());
- }
- ReleaseMutex(hMutex);
- CloseHandle(hMutex);
- CloseHandle(hMap2);
- UnmapViewOfFile(pMap);
- return 0;
- }
- int main() {
- while (1) {
- firstThreadFunction(NULL);
- //secondThreadFunction(NULL);
- Sleep(10000);
- }
- /*HANDLE hThread1 = CreateThread(NULL, 0, firstThreadFunction, &eventsHandles, 0, NULL);
- if (hThread1 == NULL)
- {
- cout << "Err create first thread" << GetLastError() << endl; */
- _getch();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement