Categories > Exploiting > Tutorials >

[TUT] How to make UI in C/C++ using only standard library & Windows API

Posts: 123

Threads: 26

Joined: Feb, 2021

Reputation: 3

Posted

When you create a Windows Forms application or a WPF application in Visual Studio, you use a designer. These designers use wrappers that abstract the underlying Windows API, written originally for the C programming language. Although the Windows API is written in C, and originally intended for C, it also works on C++.

 

So, how do I create a window? Well, first of all, you'll need the `main` function:

/*
  *  You won't really need argument parameters here, since this is a (G)UI app which is intended
  *  to be ran without a command line...
  */
int main() {
    return 0;
}

In the Windows API, there is a type named `HWND`. This type represents GUI components (or as y'all like to call UI).

First, we'll need a window, therefore we ask the C/C++ preprocessor to "include" `windows.h`. After that, we create an object that represents our window:

#include <windows.h>

HWND g_hwnd;

/*
  *  You won't really need argument parameters here, since this is a (G)UI app which is intended
  *  to be ran without a command line...
  */
int main() {
    return 0;
}

Now, we need to add the following lines to the `main` function (I'll explain what they do):

#include <windows.h>

HWND g_hwnd;

/*
  *  You won't really need argument parameters here, since this is a (G)UI app which is intended
  *  to be ran without a command line...
  */
int main() {
    HINSTANCE hInstance = GetModuleHandle(NULL);
    SetWindowLongPtr(g_hwnd, GWL_STYLE, GetWindowLongPtr(g_hwnd, GWL_STYLE));

    return 0;
}

The first line in the `main` function creates a handle which represents the running application. In the second line of the `main` function, we prepare `g_hwnd` to be a window.

 

Now, we need to create a function named `WinMain`:

int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow) { }

This is the main function for our window. We will now create a class for it:

int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
{
    WNDCLASS wc = { 0 };
    wc.lpfnWndProc = WindowProc;
    wc.hInstance = hInstance;
    wc.lpszClassName = "MainWindow";

    RegisterClass(&wc);

    g_hwnd = CreateWindowEx(0, "MainWindow", L"Express", WS_OVERLAPPED | WS_CAPTION | WS_SYSMENU, CW_USEDEFAULT, CW_USEDEFAULT, 800, 600, NULL, NULL, hInstance, NULL);

    if (g_hwnd == NULL)
    {
        return 0;
    }
}

Rename "Express" to whatever you want. It's named Express, because of my ROBLOX exploit (This code comes from my ROBLOX exploit).

 

`WindowProc` is a function that is to be defined later.

 

So here we create a class using the WNDCLASS type, and we register it. Now, we set g_hwnd (the window control) to the creation of an actual window (line 8 of `WinMain`). Then we check if g_hwnd even exists (to check if the actual creation of the window succeded or failed). If it failed, the function just stops and returns 0.

 

But we're not done yet! We need to show the window now that it exists!

int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
{
    WNDCLASS wc = { 0 };
    wc.lpfnWndProc = WindowProc;
    wc.hInstance = hInstance;
    wc.lpszClassName = "MainWindow";

    RegisterClass(&wc);

    g_hwnd = CreateWindowEx(0, "MainWindow", L"Express", WS_OVERLAPPED | WS_CAPTION | WS_SYSMENU, CW_USEDEFAULT, CW_USEDEFAULT, 800, 600, NULL, NULL, hInstance, NULL);

    if (g_hwnd == NULL)
    {
        return 0;
    }

    ShowWindow(g_hwnd, nCmdShow);

    MSG msg = { 0 };
    while (GetMessage(&msg, NULL, 0, 0))
    {
        TranslateMessage(&msg);
        DispatchMessage(&msg);
    }

    return msg.wParam;
}

Let's set-up the `WindowProc` callback function:

LRESULT CALLBACK WindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
    switch (uMsg)
    {
    case WM_DESTROY:
        PostQuitMessage(0);
        return 0;

    default:
        return DefWindowProc(hwnd, uMsg, wParam, lParam);
    }
}

This is the function that you will most likely use the most; it handles events in the window. For now, it doesn't really do much. When you click `X`, it will run `PostQuitMessage`, which will close the window (obviously). If none of that happens, it will just normally display the window.

 

In the next tutorial, I'll show you how to add components like buttons & textboxes...

  • 2

Contribute to Express, an open-source ROBLOX exploit written fully in C.

Posts: 548

Threads: 51

Joined: Oct, 2022

Reputation: 29

Replied

I would put this in the tutorial section.

  • 0

Languages - C++, C#,Javascript, HTML, CSS, Lua ,Xaml, Python

https://dsc.gg/hackerpluto

Users viewing this thread:

( Members: 0, Guests: 1, Total: 1 )