Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // test.cpp : Defines the entry point for the application.
- //
- #include "stdafx.h"
- #include "test.h"
- #include <string>
- #include <vector>
- #include <winsock.h>
- #pragma comment (lib, "ws2_32.lib")
- #define BUFFERSIZE 1024
- #define MAX_LOADSTRING 255
- #define BUTTON_ID 1001
- #define EDIT_ID 2001
- #define EDIT_ID2 2002
- #define LABEL_ID 2003
- using namespace std;
- HANDLE hEvent;
- HANDLE hThread1,hThread2;
- vector<string>vDATA;
- HWND hButton;
- HWND hEdit;
- HWND hEdit2;
- HWND hLabel;
- HINSTANCE hInst; // current instance
- TCHAR szTitle[MAX_LOADSTRING]; // The title bar text
- TCHAR szWindowClass[MAX_LOADSTRING]; // the main window class name
- // Forward declarations of functions included in this code module:
- ATOM MyRegisterClass(HINSTANCE hInstance);
- BOOL InitInstance(HINSTANCE, int);
- LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
- INT_PTR CALLBACK About(HWND, UINT, WPARAM, LPARAM);
- void die_error(char *errorMessage)
- {
- SetWindowTextA(hLabel,errorMessage);
- EnableWindow(hButton, 1);
- //exit(1);
- }
- DWORD WINAPI Thread1(PVOID pvParam)
- {
- do{
- WaitForSingleObject(hEvent, INFINITE);//подождать когда второй поток получит данные
- if(!vDATA.empty()){
- for(int i=0,size=vDATA.size();i<size;i++)
- {
- wstring stemp = wstring(vDATA.at(i).begin(), vDATA.at(i).end());
- SetWindowText(hEdit2, (LPCWSTR) stemp.c_str());
- }
- vDATA.clear();
- }
- SetWindowText(hLabel, L"OK");
- ResetEvent(hEvent);
- EnableWindow(hButton, 1);
- }while(true);
- return 0;
- }
- DWORD WINAPI Thread2(PVOID pvParam)
- {
- char host[MAX_LOADSTRING];
- GetWindowTextA(hEdit, host, 255);
- string request;
- string response;
- int resp_leng;
- char buffer[BUFFERSIZE];
- struct sockaddr_in serveraddr;
- int sock;
- WSADATA wsaData;
- request+="GET /robots.txt HTTP/1.0\r\n";
- request+="Host: ";
- request+=host;
- request+="\r\n";
- request+="\r\n";
- if (WSAStartup(MAKEWORD(2, 0), &wsaData) != 0) {die_error("WSAStartup() failed");return -1;}
- if ((sock = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP)) < 0){die_error("socket() failed");return -1;}
- memset(&serveraddr, 0, sizeof(serveraddr));
- struct hostent *he;
- if((he = gethostbyname(host)) == NULL){ die_error("gethostbyname() failed");return -1;}
- memcpy(&serveraddr.sin_addr, he->h_addr_list[0], he->h_length);
- serveraddr.sin_family = AF_INET;
- serveraddr.sin_port = htons(80);
- if (connect(sock, (struct sockaddr *) &serveraddr, sizeof(serveraddr)) < 0){die_error("connect() failed");return -1;}
- if (send(sock, request.c_str(), request.length(), 0) != request.length()){die_error("send() failed");return -1;}
- response = "";resp_leng= BUFFERSIZE;
- while (resp_leng == BUFFERSIZE)
- {
- resp_leng= recv(sock, (char*)&buffer, BUFFERSIZE, 0);
- if (resp_leng>0)response+= string(buffer).substr(0,resp_leng);
- }
- vDATA.push_back(response.c_str());
- SetEvent(hEvent);//сигнализируем 1 потоку о том что данные были получены
- closesocket(sock);
- WSACleanup();
- return 0;
- }
- int APIENTRY _tWinMain(HINSTANCE hInstance,
- HINSTANCE hPrevInstance,
- LPTSTR lpCmdLine,
- int nCmdShow)
- {
- hEvent=CreateEvent(NULL,TRUE,FALSE,L"Recv");
- hThread1=CreateThread(NULL,0,&Thread1,NULL,0,NULL);
- UNREFERENCED_PARAMETER(hPrevInstance);
- UNREFERENCED_PARAMETER(lpCmdLine);
- // TODO: Place code here.
- MSG msg;
- HACCEL hAccelTable;
- // Initialize global strings
- LoadString(hInstance, IDS_APP_TITLE, szTitle, MAX_LOADSTRING);
- LoadString(hInstance, IDC_TEST, szWindowClass, MAX_LOADSTRING);
- MyRegisterClass(hInstance);
- // Perform application initialization:
- if (!InitInstance (hInstance, nCmdShow))
- {
- return FALSE;
- }
- hAccelTable = LoadAccelerators(hInstance, MAKEINTRESOURCE(IDC_TEST));
- // Main message loop:
- while (GetMessage(&msg, NULL, 0, 0))
- {
- if (!TranslateAccelerator(msg.hwnd, hAccelTable, &msg))
- {
- TranslateMessage(&msg);
- DispatchMessage(&msg);
- }
- }
- CloseHandle(hThread1);
- CloseHandle(hThread2);
- CloseHandle(hEvent);
- return (int) msg.wParam;
- }
- //
- // FUNCTION: MyRegisterClass()
- //
- // PURPOSE: Registers the window class.
- //
- // COMMENTS:
- //
- // This function and its usage are only necessary if you want this code
- // to be compatible with Win32 systems prior to the 'RegisterClassEx'
- // function that was added to Windows 95. It is important to call this function
- // so that the application will get 'well formed' small icons associated
- // with it.
- //
- ATOM MyRegisterClass(HINSTANCE hInstance)
- {
- WNDCLASSEX wcex;
- wcex.cbSize = sizeof(WNDCLASSEX);
- wcex.style = CS_HREDRAW | CS_VREDRAW;
- wcex.lpfnWndProc = WndProc;
- wcex.cbClsExtra = 0;
- wcex.cbWndExtra = 0;
- wcex.hInstance = hInstance;
- wcex.hIcon = LoadIcon(hInstance, MAKEINTRESOURCE(IDI_TEST));
- wcex.hCursor = LoadCursor(NULL, IDC_ARROW);
- wcex.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
- wcex.lpszMenuName = MAKEINTRESOURCE(IDC_TEST);
- wcex.lpszClassName = szWindowClass;
- wcex.hIconSm = LoadIcon(wcex.hInstance, MAKEINTRESOURCE(IDI_SMALL));
- return RegisterClassEx(&wcex);
- }
- //
- // FUNCTION: InitInstance(HINSTANCE, int)
- //
- // PURPOSE: Saves instance handle and creates main window
- //
- // COMMENTS:
- //
- // In this function, we save the instance handle in a global variable and
- // create and display the main program window.
- //
- BOOL InitInstance(HINSTANCE hInstance, int nCmdShow)
- {
- HWND hWnd;
- hInst = hInstance; // Store instance handle in our global variable
- hWnd = CreateWindow(szWindowClass, szTitle, WS_OVERLAPPEDWINDOW,
- CW_USEDEFAULT, 0, CW_USEDEFAULT, 0, NULL, NULL, hInstance, NULL);
- if (!hWnd)
- {
- return FALSE;
- }
- ShowWindow(hWnd, nCmdShow);
- UpdateWindow(hWnd);
- return TRUE;
- }
- //
- // FUNCTION: WndProc(HWND, UINT, WPARAM, LPARAM)
- //
- // PURPOSE: Processes messages for the main window.
- //
- // WM_COMMAND - process the application menu
- // WM_PAINT - Paint the main window
- // WM_DESTROY - post a quit message and return
- //
- //
- LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
- {
- int wmId, wmEvent;
- PAINTSTRUCT ps;
- HDC hdc;
- switch (message)
- {
- case WM_CREATE:
- hButton = CreateWindow( L"button", L"Открыть",
- WS_CHILD | WS_VISIBLE | BS_DEFPUSHBUTTON,
- 300, 200,
- 150, 20,
- hWnd, (HMENU) BUTTON_ID,
- hInst, NULL );
- hEdit = CreateWindow( L"edit", L"www.google.ru",
- WS_CHILD |WS_VISIBLE|WS_BORDER |ES_AUTOHSCROLL,
- 0, 0,
- 500, 20,
- hWnd, (HMENU) EDIT_ID,
- hInst, NULL );
- hEdit2 = CreateWindow( L"edit", L"",
- WS_CHILD|WS_VISIBLE|WS_BORDER|WS_HSCROLL|WS_VSCROLL |ES_MULTILINE|ES_AUTOHSCROLL |ES_AUTOVSCROLL,
- 0, 20,
- 500, 150,
- hWnd, (HMENU) EDIT_ID2,
- hInst, NULL );
- hLabel = CreateWindow( L"static", L".. ",
- WS_CHILD | WS_VISIBLE,
- 10, 200,
- 240, 20,
- hWnd, (HMENU) LABEL_ID,
- hInst, NULL );
- break;
- case WM_KEYDOWN:
- if(wParam == VK_ESCAPE)DestroyWindow(hWnd);
- break;
- case WM_COMMAND:
- wmId = LOWORD(wParam);
- wmEvent = HIWORD(wParam);
- // Parse the menu selections:
- switch (wmId)
- {
- case IDM_ABOUT:
- DialogBox(hInst, MAKEINTRESOURCE(IDD_ABOUTBOX), hWnd, About);
- break;
- case IDM_EXIT:
- DestroyWindow(hWnd);
- break;
- case BUTTON_ID:
- EnableWindow(hButton, 0);
- SetWindowText(hLabel, L"Загрузка..");
- hThread2=CreateThread(NULL,0,&Thread2,NULL,0,NULL);
- break;
- default:
- return DefWindowProc(hWnd, message, wParam, lParam);
- }
- break;
- case WM_PAINT:
- hdc = BeginPaint(hWnd, &ps);
- // TODO: Add any drawing code here...
- EndPaint(hWnd, &ps);
- break;
- case WM_DESTROY:
- PostQuitMessage(0);
- break;
- default:
- return DefWindowProc(hWnd, message, wParam, lParam);
- }
- return 0;
- }
- // Message handler for about box.
- INT_PTR CALLBACK About(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)
- {
- UNREFERENCED_PARAMETER(lParam);
- switch (message)
- {
- case WM_INITDIALOG:
- return (INT_PTR)TRUE;
- case WM_COMMAND:
- if (LOWORD(wParam) == IDOK || LOWORD(wParam) == IDCANCEL)
- {
- EndDialog(hDlg, LOWORD(wParam));
- return (INT_PTR)TRUE;
- }
- break;
- }
- return (INT_PTR)FALSE;
- }
- //////////////////////////////////////////// test.rc
- //Microsoft Visual C++ generated resource script.
- //
- #include "resource.h"
- #define APSTUDIO_READONLY_SYMBOLS
- /////////////////////////////////////////////////////////////////////////////
- //
- // Generated from the TEXTINCLUDE 2 resource.
- //
- #ifndef APSTUDIO_INVOKED
- #include "targetver.h"
- #endif
- #define APSTUDIO_HIDDEN_SYMBOLS
- #include "windows.h"
- #undef APSTUDIO_HIDDEN_SYMBOLS
- /////////////////////////////////////////////////////////////////////////////
- #undef APSTUDIO_READONLY_SYMBOLS
- #if !defined(AFX_RESOURCE_DLL) || defined(AFX_TARG_ENU)
- LANGUAGE 9, 1
- /////////////////////////////////////////////////////////////////////////////
- //
- // Icon
- //
- // Icon with lowest ID value placed first to ensure application icon
- // remains consistent on all systems.
- IDI_TEST ICON "test.ico"
- IDI_SMALL ICON "small.ico"
- /////////////////////////////////////////////////////////////////////////////
- //
- // Menu
- //
- IDC_TEST MENU
- BEGIN
- POPUP "&File"
- BEGIN
- MENUITEM "E&xit", IDM_EXIT
- END
- POPUP "&Help"
- BEGIN
- MENUITEM "&About ...", IDM_ABOUT
- END
- END
- /////////////////////////////////////////////////////////////////////////////
- //
- // Accelerator
- //
- IDC_TEST ACCELERATORS
- BEGIN
- "?", IDM_ABOUT, ASCII, ALT
- "/", IDM_ABOUT, ASCII, ALT
- END
- /////////////////////////////////////////////////////////////////////////////
- //
- // Dialog
- //
- IDD_ABOUTBOX DIALOGEX 0, 0, 170, 62
- STYLE DS_SETFONT | DS_MODALFRAME | DS_FIXEDSYS | WS_POPUP | WS_CAPTION | WS_SYSMENU
- CAPTION "About test"
- FONT 8, "MS Shell Dlg"
- BEGIN
- ICON IDR_MAINFRAME,IDC_STATIC,14,14,21,20
- LTEXT "test, Version 1.0",IDC_STATIC,42,14,114,8,SS_NOPREFIX
- LTEXT "Copyright (C) 2017",IDC_STATIC,42,26,114,8
- DEFPUSHBUTTON "OK",IDOK,113,41,50,14,WS_GROUP
- END
- /////////////////////////////////////////////////////////////////////////////
- //
- // DESIGNINFO
- //
- #ifdef APSTUDIO_INVOKED
- GUIDELINES DESIGNINFO
- BEGIN
- IDD_ABOUTBOX, DIALOG
- BEGIN
- LEFTMARGIN, 7
- RIGHTMARGIN, 163
- TOPMARGIN, 7
- BOTTOMMARGIN, 55
- END
- END
- #endif // APSTUDIO_INVOKED
- #ifdef APSTUDIO_INVOKED
- /////////////////////////////////////////////////////////////////////////////
- //
- // TEXTINCLUDE
- //
- 1 TEXTINCLUDE
- BEGIN
- "resource.h\0"
- END
- 2 TEXTINCLUDE
- BEGIN
- "#ifndef APSTUDIO_INVOKED\r\n"
- "#include ""targetver.h""\r\n"
- "#endif\r\n"
- "#define APSTUDIO_HIDDEN_SYMBOLS\r\n"
- "#include ""windows.h""\r\n"
- "#undef APSTUDIO_HIDDEN_SYMBOLS\r\n"
- "\0"
- END
- 3 TEXTINCLUDE
- BEGIN
- "\r\n"
- "\0"
- END
- #endif // APSTUDIO_INVOKED
- /////////////////////////////////////////////////////////////////////////////
- //
- // String Table
- //
- STRINGTABLE
- BEGIN
- IDC_TEST "TEST"
- IDS_APP_TITLE "test"
- END
- #endif
- /////////////////////////////////////////////////////////////////////////////
- #ifndef APSTUDIO_INVOKED
- /////////////////////////////////////////////////////////////////////////////
- //
- // Generated from the TEXTINCLUDE 3 resource.
- //
- /////////////////////////////////////////////////////////////////////////////
- #endif // not APSTUDIO_INVOKED
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement