Advertisement
krot

test1_stilsof

Mar 28th, 2017
224
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 11.99 KB | None | 0 0
  1. // test.cpp : Defines the entry point for the application.
  2. //
  3.  
  4. #include "stdafx.h"
  5. #include "test.h"
  6. #include <string>
  7. #include <vector>
  8. #include <winsock.h>
  9. #pragma comment (lib, "ws2_32.lib")
  10.  
  11. #define BUFFERSIZE 1024
  12. #define MAX_LOADSTRING 255
  13. #define BUTTON_ID      1001
  14. #define EDIT_ID      2001
  15. #define EDIT_ID2      2002
  16. #define LABEL_ID    2003
  17.  
  18. using namespace std;
  19.  
  20. HANDLE hEvent;
  21. HANDLE hThread1,hThread2;
  22.  
  23. vector<string>vDATA;
  24.  
  25. HWND hButton;
  26. HWND hEdit;
  27. HWND hEdit2;
  28. HWND hLabel;
  29.  
  30. HINSTANCE hInst;                                // current instance
  31. TCHAR szTitle[MAX_LOADSTRING];                  // The title bar text
  32. TCHAR szWindowClass[MAX_LOADSTRING];            // the main window class name
  33.  
  34. // Forward declarations of functions included in this code module:
  35. ATOM                MyRegisterClass(HINSTANCE hInstance);
  36. BOOL                InitInstance(HINSTANCE, int);
  37. LRESULT CALLBACK    WndProc(HWND, UINT, WPARAM, LPARAM);
  38. INT_PTR CALLBACK    About(HWND, UINT, WPARAM, LPARAM);
  39.  
  40.  
  41.  
  42. void die_error(char *errorMessage)
  43. {
  44.     SetWindowTextA(hLabel,errorMessage);
  45.     EnableWindow(hButton, 1);
  46.     //exit(1);
  47. }
  48.  
  49. DWORD WINAPI Thread1(PVOID pvParam)
  50. {
  51.     do{
  52.      WaitForSingleObject(hEvent, INFINITE);//подождать когда второй поток получит данные
  53.      if(!vDATA.empty()){
  54.      for(int i=0,size=vDATA.size();i<size;i++)
  55.      {
  56.         wstring stemp = wstring(vDATA.at(i).begin(), vDATA.at(i).end());
  57.         SetWindowText(hEdit2, (LPCWSTR) stemp.c_str());
  58.      }
  59.      vDATA.clear();
  60.      }
  61.      SetWindowText(hLabel, L"OK");
  62.      ResetEvent(hEvent);
  63.      EnableWindow(hButton, 1);
  64.     }while(true);
  65.         return 0;
  66. }
  67. DWORD WINAPI Thread2(PVOID pvParam)
  68. {
  69.     char host[MAX_LOADSTRING];
  70.     GetWindowTextA(hEdit, host, 255);
  71.  
  72.     string request;
  73.     string response;
  74.     int resp_leng;
  75.  
  76.     char buffer[BUFFERSIZE];
  77.     struct sockaddr_in serveraddr;
  78.     int sock;
  79.  
  80.     WSADATA wsaData;
  81.  
  82.     request+="GET /robots.txt HTTP/1.0\r\n";
  83.     request+="Host: ";
  84.     request+=host;
  85.     request+="\r\n";
  86.     request+="\r\n";
  87.  
  88.     if (WSAStartup(MAKEWORD(2, 0), &wsaData) != 0) {die_error("WSAStartup() failed");return -1;}
  89.     if ((sock = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP)) < 0){die_error("socket() failed");return -1;}
  90.  
  91.     memset(&serveraddr, 0, sizeof(serveraddr));
  92.     struct hostent *he;
  93.     if((he = gethostbyname(host)) == NULL){ die_error("gethostbyname() failed");return -1;}
  94.     memcpy(&serveraddr.sin_addr, he->h_addr_list[0], he->h_length);
  95.     serveraddr.sin_family      = AF_INET;
  96.     serveraddr.sin_port        = htons(80);
  97.  
  98.     if (connect(sock, (struct sockaddr *) &serveraddr, sizeof(serveraddr)) < 0){die_error("connect() failed");return -1;}
  99.     if (send(sock, request.c_str(), request.length(), 0) != request.length()){die_error("send() failed");return -1;}
  100.  
  101.     response = "";resp_leng= BUFFERSIZE;
  102.     while (resp_leng == BUFFERSIZE)
  103.     {
  104.         resp_leng= recv(sock, (char*)&buffer, BUFFERSIZE, 0);
  105.         if (resp_leng>0)response+= string(buffer).substr(0,resp_leng);
  106.     }
  107.     vDATA.push_back(response.c_str());
  108.  
  109.     SetEvent(hEvent);//сигнализируем 1 потоку о том что данные были получены
  110.  
  111.     closesocket(sock);
  112.     WSACleanup();
  113.     return 0;
  114. }
  115.  
  116.  
  117.  
  118.  
  119.  
  120. int APIENTRY _tWinMain(HINSTANCE hInstance,
  121.                      HINSTANCE hPrevInstance,
  122.                      LPTSTR    lpCmdLine,
  123.                      int       nCmdShow)
  124. {
  125.     hEvent=CreateEvent(NULL,TRUE,FALSE,L"Recv");
  126.     hThread1=CreateThread(NULL,0,&Thread1,NULL,0,NULL);
  127.  
  128.     UNREFERENCED_PARAMETER(hPrevInstance);
  129.     UNREFERENCED_PARAMETER(lpCmdLine);
  130.  
  131.     // TODO: Place code here.
  132.     MSG msg;
  133.     HACCEL hAccelTable;
  134.  
  135.     // Initialize global strings
  136.     LoadString(hInstance, IDS_APP_TITLE, szTitle, MAX_LOADSTRING);
  137.     LoadString(hInstance, IDC_TEST, szWindowClass, MAX_LOADSTRING);
  138.     MyRegisterClass(hInstance);
  139.  
  140.     // Perform application initialization:
  141.     if (!InitInstance (hInstance, nCmdShow))
  142.     {
  143.         return FALSE;
  144.     }
  145.  
  146.     hAccelTable = LoadAccelerators(hInstance, MAKEINTRESOURCE(IDC_TEST));
  147.  
  148.     // Main message loop:
  149.     while (GetMessage(&msg, NULL, 0, 0))
  150.     {
  151.         if (!TranslateAccelerator(msg.hwnd, hAccelTable, &msg))
  152.         {
  153.             TranslateMessage(&msg);
  154.             DispatchMessage(&msg);
  155.         }
  156.     }
  157.  
  158.     CloseHandle(hThread1);
  159.     CloseHandle(hThread2);
  160.     CloseHandle(hEvent);
  161.     return (int) msg.wParam;
  162. }
  163.  
  164.  
  165.  
  166. //
  167. //  FUNCTION: MyRegisterClass()
  168. //
  169. //  PURPOSE: Registers the window class.
  170. //
  171. //  COMMENTS:
  172. //
  173. //    This function and its usage are only necessary if you want this code
  174. //    to be compatible with Win32 systems prior to the 'RegisterClassEx'
  175. //    function that was added to Windows 95. It is important to call this function
  176. //    so that the application will get 'well formed' small icons associated
  177. //    with it.
  178. //
  179. ATOM MyRegisterClass(HINSTANCE hInstance)
  180. {
  181.     WNDCLASSEX wcex;
  182.  
  183.     wcex.cbSize = sizeof(WNDCLASSEX);
  184.  
  185.     wcex.style          = CS_HREDRAW | CS_VREDRAW;
  186.     wcex.lpfnWndProc    = WndProc;
  187.     wcex.cbClsExtra     = 0;
  188.     wcex.cbWndExtra     = 0;
  189.     wcex.hInstance      = hInstance;
  190.     wcex.hIcon          = LoadIcon(hInstance, MAKEINTRESOURCE(IDI_TEST));
  191.     wcex.hCursor        = LoadCursor(NULL, IDC_ARROW);
  192.     wcex.hbrBackground  = (HBRUSH)(COLOR_WINDOW+1);
  193.     wcex.lpszMenuName   = MAKEINTRESOURCE(IDC_TEST);
  194.     wcex.lpszClassName  = szWindowClass;
  195.     wcex.hIconSm        = LoadIcon(wcex.hInstance, MAKEINTRESOURCE(IDI_SMALL));
  196.  
  197.     return RegisterClassEx(&wcex);
  198. }
  199.  
  200. //
  201. //   FUNCTION: InitInstance(HINSTANCE, int)
  202. //
  203. //   PURPOSE: Saves instance handle and creates main window
  204. //
  205. //   COMMENTS:
  206. //
  207. //        In this function, we save the instance handle in a global variable and
  208. //        create and display the main program window.
  209. //
  210. BOOL InitInstance(HINSTANCE hInstance, int nCmdShow)
  211. {
  212.    HWND hWnd;
  213.  
  214.    hInst = hInstance; // Store instance handle in our global variable
  215.  
  216.    hWnd = CreateWindow(szWindowClass, szTitle, WS_OVERLAPPEDWINDOW,
  217.       CW_USEDEFAULT, 0, CW_USEDEFAULT, 0, NULL, NULL, hInstance, NULL);
  218.  
  219.    if (!hWnd)
  220.    {
  221.       return FALSE;
  222.    }
  223.  
  224.    ShowWindow(hWnd, nCmdShow);
  225.    UpdateWindow(hWnd);
  226.  
  227.    return TRUE;
  228. }
  229.  
  230. //
  231. //  FUNCTION: WndProc(HWND, UINT, WPARAM, LPARAM)
  232. //
  233. //  PURPOSE:  Processes messages for the main window.
  234. //
  235. //  WM_COMMAND  - process the application menu
  236. //  WM_PAINT    - Paint the main window
  237. //  WM_DESTROY  - post a quit message and return
  238. //
  239. //
  240. LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
  241. {
  242.     int wmId, wmEvent;
  243.     PAINTSTRUCT ps;
  244.     HDC hdc;
  245.  
  246.     switch (message)
  247.     {
  248.     case WM_CREATE:
  249.      
  250.        hButton = CreateWindow( L"button", L"Открыть",
  251.                 WS_CHILD | WS_VISIBLE | BS_DEFPUSHBUTTON,
  252.                 300, 200,
  253.                 150, 20,
  254.                 hWnd, (HMENU) BUTTON_ID,
  255.                 hInst, NULL );
  256.  
  257.         hEdit = CreateWindow( L"edit", L"www.google.ru",
  258.                 WS_CHILD |WS_VISIBLE|WS_BORDER |ES_AUTOHSCROLL,
  259.                 0, 0,
  260.                 500, 20,
  261.                 hWnd, (HMENU) EDIT_ID,
  262.                 hInst, NULL );
  263.  
  264.         hEdit2 = CreateWindow( L"edit", L"",
  265.                 WS_CHILD|WS_VISIBLE|WS_BORDER|WS_HSCROLL|WS_VSCROLL |ES_MULTILINE|ES_AUTOHSCROLL |ES_AUTOVSCROLL,
  266.                 0, 20,
  267.                 500, 150,
  268.                 hWnd, (HMENU) EDIT_ID2,
  269.                 hInst, NULL );
  270.  
  271.  
  272.          hLabel = CreateWindow( L"static", L".. ",
  273.                 WS_CHILD | WS_VISIBLE,
  274.                 10, 200,
  275.                 240, 20,
  276.                 hWnd, (HMENU) LABEL_ID,
  277.                 hInst, NULL );
  278.          break;
  279.     case WM_KEYDOWN:
  280.         if(wParam == VK_ESCAPE)DestroyWindow(hWnd);
  281.             break;
  282.     case WM_COMMAND:
  283.         wmId    = LOWORD(wParam);
  284.         wmEvent = HIWORD(wParam);
  285.         // Parse the menu selections:
  286.         switch (wmId)
  287.         {
  288.         case IDM_ABOUT:
  289.             DialogBox(hInst, MAKEINTRESOURCE(IDD_ABOUTBOX), hWnd, About);
  290.             break;
  291.         case IDM_EXIT:
  292.             DestroyWindow(hWnd);
  293.             break;
  294.         case BUTTON_ID:
  295.             EnableWindow(hButton, 0);
  296.             SetWindowText(hLabel, L"Загрузка..");
  297.             hThread2=CreateThread(NULL,0,&Thread2,NULL,0,NULL);
  298.            
  299.         break;
  300.         default:
  301.             return DefWindowProc(hWnd, message, wParam, lParam);
  302.         }
  303.         break;
  304.     case WM_PAINT:
  305.         hdc = BeginPaint(hWnd, &ps);
  306.         // TODO: Add any drawing code here...
  307.         EndPaint(hWnd, &ps);
  308.         break;
  309.     case WM_DESTROY:
  310.         PostQuitMessage(0);
  311.         break;
  312.     default:
  313.         return DefWindowProc(hWnd, message, wParam, lParam);
  314.     }
  315.     return 0;
  316. }
  317.  
  318. // Message handler for about box.
  319. INT_PTR CALLBACK About(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)
  320. {
  321.     UNREFERENCED_PARAMETER(lParam);
  322.     switch (message)
  323.     {
  324.     case WM_INITDIALOG:
  325.         return (INT_PTR)TRUE;
  326.  
  327.     case WM_COMMAND:
  328.         if (LOWORD(wParam) == IDOK || LOWORD(wParam) == IDCANCEL)
  329.         {
  330.             EndDialog(hDlg, LOWORD(wParam));
  331.             return (INT_PTR)TRUE;
  332.         }
  333.         break;
  334.     }
  335.     return (INT_PTR)FALSE;
  336. }
  337. //////////////////////////////////////////// test.rc
  338. //Microsoft Visual C++ generated resource script.
  339. //
  340. #include "resource.h"
  341.  
  342. #define APSTUDIO_READONLY_SYMBOLS
  343. /////////////////////////////////////////////////////////////////////////////
  344. //
  345. // Generated from the TEXTINCLUDE 2 resource.
  346. //
  347. #ifndef APSTUDIO_INVOKED
  348. #include "targetver.h"
  349. #endif
  350. #define APSTUDIO_HIDDEN_SYMBOLS
  351. #include "windows.h"
  352. #undef APSTUDIO_HIDDEN_SYMBOLS
  353. /////////////////////////////////////////////////////////////////////////////
  354. #undef APSTUDIO_READONLY_SYMBOLS
  355.  
  356. #if !defined(AFX_RESOURCE_DLL) || defined(AFX_TARG_ENU)
  357. LANGUAGE 9, 1
  358.  
  359. /////////////////////////////////////////////////////////////////////////////
  360. //
  361. // Icon
  362. //
  363.  
  364. // Icon with lowest ID value placed first to ensure application icon
  365. // remains consistent on all systems.
  366.  
  367. IDI_TEST       ICON         "test.ico"
  368. IDI_SMALL               ICON         "small.ico"
  369.  
  370. /////////////////////////////////////////////////////////////////////////////
  371. //
  372. // Menu
  373. //
  374.  
  375. IDC_TEST MENU
  376. BEGIN
  377.     POPUP "&File"
  378.     BEGIN
  379.         MENUITEM "E&xit",                IDM_EXIT
  380.     END
  381.     POPUP "&Help"
  382.     BEGIN
  383.         MENUITEM "&About ...",           IDM_ABOUT
  384.     END
  385. END
  386.  
  387.  
  388. /////////////////////////////////////////////////////////////////////////////
  389. //
  390. // Accelerator
  391. //
  392.  
  393. IDC_TEST ACCELERATORS
  394. BEGIN
  395.     "?",            IDM_ABOUT,              ASCII,  ALT
  396.     "/",            IDM_ABOUT,              ASCII,  ALT
  397. END
  398.  
  399.  
  400. /////////////////////////////////////////////////////////////////////////////
  401. //
  402. // Dialog
  403. //
  404.  
  405. IDD_ABOUTBOX DIALOGEX 0, 0, 170, 62
  406. STYLE DS_SETFONT | DS_MODALFRAME | DS_FIXEDSYS | WS_POPUP | WS_CAPTION | WS_SYSMENU
  407. CAPTION "About test"
  408. FONT 8, "MS Shell Dlg"
  409. BEGIN
  410.     ICON            IDR_MAINFRAME,IDC_STATIC,14,14,21,20
  411.     LTEXT           "test, Version 1.0",IDC_STATIC,42,14,114,8,SS_NOPREFIX
  412.     LTEXT           "Copyright (C) 2017",IDC_STATIC,42,26,114,8
  413.     DEFPUSHBUTTON   "OK",IDOK,113,41,50,14,WS_GROUP
  414. END
  415.  
  416. /////////////////////////////////////////////////////////////////////////////
  417. //
  418. // DESIGNINFO
  419. //
  420.  
  421. #ifdef APSTUDIO_INVOKED
  422. GUIDELINES DESIGNINFO
  423. BEGIN
  424.     IDD_ABOUTBOX, DIALOG
  425.     BEGIN
  426.         LEFTMARGIN, 7
  427.         RIGHTMARGIN, 163
  428.         TOPMARGIN, 7
  429.         BOTTOMMARGIN, 55
  430.     END
  431. END
  432. #endif    // APSTUDIO_INVOKED
  433.  
  434. #ifdef APSTUDIO_INVOKED
  435. /////////////////////////////////////////////////////////////////////////////
  436. //
  437. // TEXTINCLUDE
  438. //
  439. 1 TEXTINCLUDE
  440. BEGIN
  441.     "resource.h\0"
  442. END
  443.  
  444. 2 TEXTINCLUDE
  445. BEGIN
  446.     "#ifndef APSTUDIO_INVOKED\r\n"
  447.     "#include ""targetver.h""\r\n"
  448.     "#endif\r\n"
  449.     "#define APSTUDIO_HIDDEN_SYMBOLS\r\n"
  450.     "#include ""windows.h""\r\n"
  451.     "#undef APSTUDIO_HIDDEN_SYMBOLS\r\n"
  452.     "\0"
  453. END
  454.  
  455. 3 TEXTINCLUDE
  456. BEGIN
  457.     "\r\n"
  458.     "\0"
  459. END
  460.  
  461. #endif    // APSTUDIO_INVOKED
  462.  
  463. /////////////////////////////////////////////////////////////////////////////
  464. //
  465. // String Table
  466. //
  467.  
  468. STRINGTABLE
  469. BEGIN
  470.    IDC_TEST   "TEST"
  471.    IDS_APP_TITLE       "test"
  472. END
  473.  
  474. #endif
  475. /////////////////////////////////////////////////////////////////////////////
  476.  
  477.  
  478.  
  479. #ifndef APSTUDIO_INVOKED
  480. /////////////////////////////////////////////////////////////////////////////
  481. //
  482. // Generated from the TEXTINCLUDE 3 resource.
  483. //
  484.  
  485. /////////////////////////////////////////////////////////////////////////////
  486. #endif    // not APSTUDIO_INVOKED
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement