Vitali Kremez
  • Home
  • About
  • Contact
  • Cyber Security
  • Cyber Intel
  • Programming
  • Reverse Engineering
  • Exploit Development
  • Penetration Test
  • WIN32 Assembly
  • On Writing
    • Blog
    • LSAT
    • Photo
  • Honeypot
  • Forum

WinAPI C++ Programming: Lesson

12/25/2015

0 Comments

 
// ​https://msdn.microsoft.com/en-us/library/windows/desktop/ff381409(v=vs.85).aspx
// Lesson I by Vitali Kremez

#include <windows.h>
#include <stdlib.h>
#include <string.h>
#include <tchar.h>
 
// Just as every C application and C++ application must have a main function as its starting point, every Win32-based application must have a WinMain function. WinMain has the following syntax.
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow);
// In addition to the WinMain function, every Windows desktop application must also have a window-procedure function. This function is typically named WndProc. WndProc has the following syntax.
LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
 
// TO ADD FUNCTIONALITY TO THE WINMAIN FUNCTION
// 1. In the WinMain function, create a window class structure of type WNDCLASSEX.This structure contains information about the window, for example, the application icon, the background color of the window, the name to display in the title bar, the name of the window procedure function, and so on.The following example shows a typical WNDCLASSEX structure.
 
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_APPLICATION));
wcex.hCursor =                    LoadCursor(NULL, IDC_ARROW);
wcex.hbrBackground =      (HBRUSH)(COLOR_WINDOW + 1);
wcex.lpszMenuName =      NULL;
wcex.lpszClassName =       szWindowClass;
wcex.hIconSm =                  LoadIcon(wcex.hInstance, MAKEINTRESOURCE(IDI_APPLICATION));
 
// For information about the fields of this structure, see WNDCLASSEX.
// 2. Now that you have created a window class, you must register it.Use the RegisterClassEx function and pass the window class structure as an argument.
 
if (!RegisterClassEx(&wcex))
{
       MessageBox(NULL,
              _T("Call to RegisterClassEx failed!"),
              _T("Win32 Guided Tour"),
              NULL);
 
       return 1;
}
 
// Now you can create a window. Use the CreateWindow function.
 
 
static TCHAR szWindowClass[] = _T("win32app");
static TCHAR szTitle[] = _T("Win32 Guided Tour Application");
 
 
// The parameters to CreateWindow explained:
// szWindowClass: the name of the application
// szTitle: the text that appears in the title bar
// WS_OVERLAPPEDWINDOW: the type of window to create
// CW_USEDEFAULT, CW_USEDEFAULT: initial position (x, y)
// 500, 100: initial size (width, length)
// NULL: the parent of this window
// NULL: this application does not have a menu bar
// hInstance: the first parameter from WinMain
// NULL: not used in this application
HWND hWnd = CreateWindow(
       szWindowClass,
       szTitle,
       WS_OVERLAPPEDWINDOW,
       CW_USEDEFAULT, CW_USEDEFAULT,
       500, 100,
       NULL,
       NULL,
       hInstance,
       NULL
       );
if (!hWnd)
{
       MessageBox(NULL,
              _T("Call to CreateWindow failed!"),
              _T("Win32 Guided Tour"),
              NULL);
 
       return 1;
}
 
// 4.Now, use the following code to display the window.
// The parameters to ShowWindow explained:
// hWnd: the value returned from CreateWindow
// nCmdShow: the fourth parameter from WinMain
ShowWindow(hWnd,
       nCmdShow);
UpdateWindow(hWnd);
 
// At this point, the displayed window will not have much content because you have not yet implemented the WndProc function.
 
// 5. Now add a message loop to listen for the messages that the operating system sends. When the application receives a message, this loop dispatches it to the WndProc function to be handled. The message loop resembles the following code.
MSG msg;
while (GetMessage(&msg, NULL, 0, 0))
{
       TranslateMessage(&msg);
       DispatchMessage(&msg);
}
 
return (int)msg.wParam;
 
// For more information about the structures and functions in the message loop, see MSG, GetMessage, TranslateMessage, and DispatchMessage.
 
// TO ADD FUNCTIONALITY TO THE WNDPROC FUNCTION
 
// 1. To enable the WndProc function to handle the messages that the application receives, implement a switch statement.
// The first message to handle is the WM_PAINT message.The application receives this message when part of its displayed window must be updated. (When the window is first displayed, all of it must be updated.)
// To handle a WM_PAINT message, first call BeginPaint, then handle all the logic to lay out the text, buttons, and other controls in the window, and then call EndPaint.For this application, the logic between the beginning call and the ending call is to display the string "Hello, World!" in the window.In the following code, notice that the TextOut function is used to display the string.
 
PAINTSTRUCT ps;
HDC hdc;
TCHAR greeting[] = _T("Hello, World!");
 
switch (message)
{
case WM_PAINT:
       hdc = BeginPaint(hWnd, &ps);
 
       // Here your application is laid out.
       // For this introduction, we just print out "Hello, World!"
       // in the top left corner.
       TextOut(hdc,
              5, 5,
              greeting, _tcslen(greeting));
       // End application-specific layout section.
 
       EndPaint(hWnd, &ps);
       break;
}
 
// 2. An application typically handles many other messages, for example, WM_CREATE and WM_DESTROY. The following code shows a basic but complete WndProc function.
 
LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
       PAINTSTRUCT ps;
       HDC hdc;
       TCHAR greeting[] = _T("Hello, World!");
 
       switch (message)
       {
       case WM_PAINT:
              hdc = BeginPaint(hWnd, &ps);
 
              // Here your application is laid out.
              // For this introduction, we just print out "Hello, World!"
              // in the top left corner.
              TextOut(hdc,
                      5, 5,
                      greeting, _tcslen(greeting));
              // End application specific layout section.
 
              EndPaint(hWnd, &ps);
              break;
       case WM_DESTROY:
              PostQuitMessage(0);
              break;
       default:
              return DefWindowProc(hWnd, message, wParam, lParam);
              break;
       }
 
       return 0;
}
​
0 Comments



Leave a Reply.

    Author

    Vitali Kremez
    The Coder

    Archives

    January 2016
    December 2015
    November 2015
    October 2015
    September 2015

    Categories

    All

    RSS Feed

Powered by Create your own unique website with customizable templates.
  • Home
  • About
  • Contact
  • Cyber Security
  • Cyber Intel
  • Programming
  • Reverse Engineering
  • Exploit Development
  • Penetration Test
  • WIN32 Assembly
  • On Writing
    • Blog
    • LSAT
    • Photo
  • Honeypot
  • Forum