Skip to content

Instantly share code, notes, and snippets.

@erfg12
Created May 9, 2023 03:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save erfg12/ab1f176785ea9bff48aec85fac7a8f3b to your computer and use it in GitHub Desktop.
Save erfg12/ab1f176785ea9bff48aec85fac7a8f3b to your computer and use it in GitHub Desktop.
Build and Debug C app in Windows

pre-req:

  • install VS
  • add VS's "C:\Program Files\Microsoft Visual Studio\2022\Community\VC\Tools\MSVC\14.35.32215\bin\Hostx64\x64" to system environment Path var

to build:

  • type "C:\Program Files\Microsoft Visual Studio\2022\Community\VC\Auxiliary\Build\vcvars32"
  • type "cl -Zi test.c user32.lib gdi32.lib"

to debug:

  • type "/devenv /debugexe ./test.exe"
  • hit F11 key, click continue button
#include <windows.h>
LRESULT CALLBACK MainWindowCallback(HWND Window, UINT Message, WPARAM WParam, LPARAM LParam) {
LRESULT Result = 0;
switch (Message) {
case WM_CLOSE:
{
PostQuitMessage(0);
} break;
case WM_PAINT:
{
PAINTSTRUCT Paint;
HDC DeviceContext = BeginPaint(Window, &Paint);
int X = Paint.rcPaint.left;
int Y = Paint.rcPaint.top;
int Width = Paint.rcPaint.right - X;
int Height = Paint.rcPaint.bottom - Y;
PatBlt(DeviceContext, X, Y, Width, Height, WHITENESS);
EndPaint(Window, &Paint);
} break;
default: {
Result = DefWindowProc(Window, Message, WParam, LParam);
} break;
}
return(Result);
}
int CALLBACK WinMain(HINSTANCE Instance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nShowCmd) {
WNDCLASS WindowClass;
memset(&WindowClass, '\0', sizeof(WindowClass));
WindowClass.style = CS_OWNDC | CS_HREDRAW | CS_VREDRAW;
WindowClass.lpfnWndProc = MainWindowCallback;
WindowClass.hInstance = Instance;
WindowClass.lpszClassName = "MainWindowClass";
if (RegisterClass(&WindowClass)) {
HWND WindowHandle = CreateWindowEx(0, WindowClass.lpszClassName, "TestWindow", WS_OVERLAPPEDWINDOW | WS_VISIBLE, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, 0, 0, Instance, 0);
if (WindowHandle) {
for (;;) {
MSG Message;
BOOL MessageResult = GetMessage(&Message, 0, 0, 0);
if (MessageResult > 0) {
TranslateMessage(&Message);
DispatchMessage(&Message);
}
else
break;
}
}
}
return (0);
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment