Kod: Tümünü seç
/* ping_checker_win_gui.c
* Windows 11 GUI Ping Checker
* Design : Modern Windows 11 style, English labels
* Feature : Threaded ping, progress bar, avg/min/max parsing
* Compiler: MinGW-w64
* Build : gcc -o ping_checker_win_gui.exe ping_checker_win_gui.c -lws2_32 -lcomctl32 -mwindows
*/
#define UNICODE
#define _UNICODE
#define _WIN32_WINNT 0x0601
#include <windows.h>
#include <commctrl.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <wchar.h>
/* -------------------------------------------------------------------------
* Control IDs
* ---------------------------------------------------------------------- */
#define ID_EDIT_IP 101
#define ID_EDIT_WEB 102
#define ID_EDIT_COUNT 103
#define ID_BTN_START 104
#define ID_BTN_CLEAR 105
#define ID_PROGRESS 106
#define ID_EDIT_OUTPUT 107
#define ID_LBL_STATUS 108
#define ID_LBL_AVG 109
#define ID_LBL_RECV 110
#define ID_LBL_MINMAX 111
/* -------------------------------------------------------------------------
* Window dimensions
* ---------------------------------------------------------------------- */
#define WIN_W 640
#define WIN_H 580
/* -------------------------------------------------------------------------
* Thread communication
* ---------------------------------------------------------------------- */
#define WM_PING_DONE (WM_USER + 1)
#define WM_PING_LINE (WM_USER + 2)
#define WM_PING_PROG (WM_USER + 3)
typedef struct {
HWND hwnd;
wchar_t target[512];
int count;
} PingArgs;
typedef struct {
wchar_t status[64];
int avg_ms;
int min_ms;
int max_ms;
int sent;
int received;
int lost;
} PingResult;
/* -------------------------------------------------------------------------
* Global handles
* ---------------------------------------------------------------------- */
static HWND g_hwnd;
static HWND g_hEditIP;
static HWND g_hEditWeb;
static HWND g_hEditCount;
static HWND g_hBtnStart;
static HWND g_hBtnClear;
static HWND g_hProgress;
static HWND g_hOutput;
static HWND g_hLblStatus;
static HWND g_hLblAvg;
static HWND g_hLblRecv;
static HWND g_hLblMinMax;
static HFONT g_hFontUI;
static HFONT g_hFontMono;
static HFONT g_hFontBold;
/* -------------------------------------------------------------------------
* Helper: append text to output edit control
* ---------------------------------------------------------------------- */
static void AppendOutput(HWND hEdit, const wchar_t *text) {
int len = GetWindowTextLengthW(hEdit);
SendMessageW(hEdit, EM_SETSEL, (WPARAM)len, (LPARAM)len);
SendMessageW(hEdit, EM_REPLACESEL, FALSE, (LPARAM)text);
}
/* -------------------------------------------------------------------------
* Helper: extract integer ms value from wide string after keyword
* ---------------------------------------------------------------------- */
static int ExtractMs(const wchar_t *line, const wchar_t *keyword) {
const wchar_t *pos = wcsstr(line, keyword);
if (!pos) return -1;
pos += wcslen(keyword);
while (*pos == L' ' || *pos == L'=') pos++;
int val = 0;
if (swscanf(pos, L"%d", &val) == 1) return val;
return -1;
}
/* -------------------------------------------------------------------------
* Ping thread
* ---------------------------------------------------------------------- */
static DWORD WINAPI PingThread(LPVOID lpParam) {
PingArgs *args = (PingArgs *)lpParam;
PingResult *result = (PingResult *)calloc(1, sizeof(PingResult));
result->avg_ms = -1;
result->min_ms = -1;
result->max_ms = -1;
/* Build command */
wchar_t cmd[768];
_snwprintf(cmd, 768, L"ping -n %d %s", args->count, args->target);
/* Open pipe */
SECURITY_ATTRIBUTES sa = { sizeof(sa), NULL, TRUE };
HANDLE hRead, hWrite;
if (!CreatePipe(&hRead, &hWrite, &sa, 0)) {
free(result);
free(args);
PostMessageW(args->hwnd, WM_PING_DONE, 0, 0);
return 1;
}
SetHandleInformation(hRead, HANDLE_FLAG_INHERIT, 0);
STARTUPINFOW si = { sizeof(si) };
si.dwFlags = STARTF_USESTDHANDLES | STARTF_USESHOWWINDOW;
si.hStdOutput = hWrite;
si.hStdError = hWrite;
si.wShowWindow = SW_HIDE;
PROCESS_INFORMATION pi = {0};
wchar_t cmdLine[800];
_snwprintf(cmdLine, 800, L"cmd.exe /C %s", cmd);
if (!CreateProcessW(NULL, cmdLine, NULL, NULL, TRUE,
CREATE_NO_WINDOW, NULL, NULL, &si, &pi)) {
CloseHandle(hRead);
CloseHandle(hWrite);
free(result);
free(args);
return 1;
}
CloseHandle(hWrite);
/* Read output line by line */
char buf[512];
wchar_t wline[512];
char leftover[512] = {0};
int leftlen = 0;
DWORD bytesRead;
int step = 0;
while (ReadFile(hRead, buf + leftlen,
(DWORD)(sizeof(buf) - leftlen - 1),
&bytesRead, NULL) && bytesRead > 0) {
buf[leftlen + bytesRead] = '\0';
char *start = buf;
char *nl;
while ((nl = strchr(start, '\n')) != NULL) {
*nl = '\0';
/* Strip \r */
int slen = (int)strlen(start);
if (slen > 0 && start[slen-1] == '\r') start[slen-1] = '\0';
/* Convert to wide */
MultiByteToWideChar(CP_OEMCP, 0, start, -1, wline, 512);
/* Send line to main window */
wchar_t *lineCopy = _wcsdup(wline);
PostMessageW(args->hwnd, WM_PING_LINE, 0, (LPARAM)lineCopy);
/* Parse statistics */
if (result->avg_ms < 0) {
result->avg_ms = ExtractMs(wline, L"Average = ");
if (result->avg_ms < 0)
result->avg_ms = ExtractMs(wline, L"Ortalama = ");
}
if (result->min_ms < 0)
result->min_ms = ExtractMs(wline, L"Minimum = ");
if (result->max_ms < 0) {
result->max_ms = ExtractMs(wline, L"Maximum = ");
if (result->max_ms < 0)
result->max_ms = ExtractMs(wline, L"Maksimum = ");
}
/* Parse packet counts */
if (wcsstr(wline, L"Sent =") || wcsstr(wline, L"Gonderilen =")) {
swscanf(wcsstr(wline, L"= "), L"= %d", &result->sent);
const wchar_t *r = wcsstr(wline, L"Received =");
if (!r) r = wcsstr(wline, L"Alinan =");
if (r) swscanf(wcsstr(r, L"= "), L"= %d", &result->received);
const wchar_t *l = wcsstr(wline, L"Lost =");
if (!l) l = wcsstr(wline, L"Kayip =");
if (l) swscanf(wcsstr(l, L"= "), L"= %d", &result->lost);
}
/* Progress */
step++;
int pct = (step * 100) / (args->count + 2);
if (pct > 99) pct = 99;
PostMessageW(args->hwnd, WM_PING_PROG, (WPARAM)pct, 0);
start = nl + 1;
}
/* Keep leftover */
leftlen = (int)strlen(start);
memmove(buf, start, leftlen + 1);
}
CloseHandle(hRead);
WaitForSingleObject(pi.hProcess, 3000);
CloseHandle(pi.hProcess);
CloseHandle(pi.hThread);
/* Determine status */
if (result->received > 0)
wcscpy(result->status, L"ONLINE");
else
wcscpy(result->status, L"OFFLINE");
PostMessageW(args->hwnd, WM_PING_DONE, 0, (LPARAM)result);
free(args);
return 0;
}
/* -------------------------------------------------------------------------
* Set label text helper
* ---------------------------------------------------------------------- */
static void SetLabel(HWND hLbl, const wchar_t *text) {
SetWindowTextW(hLbl, text);
InvalidateRect(hLbl, NULL, TRUE);
}
/* -------------------------------------------------------------------------
* Window procedure
* ---------------------------------------------------------------------- */
static LRESULT CALLBACK WndProc(HWND hwnd, UINT msg,
WPARAM wParam, LPARAM lParam) {
switch (msg) {
case WM_CREATE: {
/* Fonts */
g_hFontUI = CreateFontW(16, 0, 0, 0, FW_NORMAL, 0, 0, 0,
DEFAULT_CHARSET, 0, 0,
CLEARTYPE_QUALITY, 0, L"Segoe UI");
g_hFontBold = CreateFontW(16, 0, 0, 0, FW_BOLD, 0, 0, 0,
DEFAULT_CHARSET, 0, 0,
CLEARTYPE_QUALITY, 0, L"Segoe UI");
g_hFontMono = CreateFontW(14, 0, 0, 0, FW_NORMAL, 0, 0, 0,
DEFAULT_CHARSET, 0, 0,
CLEARTYPE_QUALITY, 0, L"Consolas");
int lx = 12, lw = 110, ex = 128, ew = 380, y = 14, dy = 34;
/* Row 1 - IP Address */
HWND h;
h = CreateWindowW(L"STATIC", L"IP Address:", WS_CHILD|WS_VISIBLE,
lx, y+2, lw, 20, hwnd, NULL, NULL, NULL);
SendMessageW(h, WM_SETFONT, (WPARAM)g_hFontUI, TRUE);
g_hEditIP = CreateWindowW(L"EDIT", L"",
WS_CHILD|WS_VISIBLE|WS_BORDER|ES_AUTOHSCROLL,
ex, y, ew, 24, hwnd, (HMENU)ID_EDIT_IP, NULL, NULL);
SendMessageW(g_hEditIP, WM_SETFONT, (WPARAM)g_hFontUI, TRUE);
y += dy;
/* Row 2 - Web Site */
h = CreateWindowW(L"STATIC", L"Web Site:", WS_CHILD|WS_VISIBLE,
lx, y+2, lw, 20, hwnd, NULL, NULL, NULL);
SendMessageW(h, WM_SETFONT, (WPARAM)g_hFontUI, TRUE);
g_hEditWeb = CreateWindowW(L"EDIT", L"",
WS_CHILD|WS_VISIBLE|WS_BORDER|ES_AUTOHSCROLL,
ex, y, ew, 24, hwnd, (HMENU)ID_EDIT_WEB, NULL, NULL);
SendMessageW(g_hEditWeb, WM_SETFONT, (WPARAM)g_hFontUI, TRUE);
y += dy;
/* Row 3 - Ping Count + Start button */
h = CreateWindowW(L"STATIC", L"Ping Count:", WS_CHILD|WS_VISIBLE,
lx, y+2, lw, 20, hwnd, NULL, NULL, NULL);
SendMessageW(h, WM_SETFONT, (WPARAM)g_hFontUI, TRUE);
g_hEditCount = CreateWindowW(L"EDIT", L"4",
WS_CHILD|WS_VISIBLE|WS_BORDER|ES_NUMBER,
ex, y, 80, 24, hwnd, (HMENU)ID_EDIT_COUNT, NULL, NULL);
SendMessageW(g_hEditCount, WM_SETFONT, (WPARAM)g_hFontUI, TRUE);
g_hBtnStart = CreateWindowW(L"BUTTON", L"Start",
WS_CHILD|WS_VISIBLE|BS_PUSHBUTTON,
ex+90, y, 90, 26, hwnd, (HMENU)ID_BTN_START, NULL, NULL);
SendMessageW(g_hBtnStart, WM_SETFONT, (WPARAM)g_hFontBold, TRUE);
g_hBtnClear = CreateWindowW(L"BUTTON", L"Clear",
WS_CHILD|WS_VISIBLE|BS_PUSHBUTTON,
ex+190, y, 90, 26, hwnd, (HMENU)ID_BTN_CLEAR, NULL, NULL);
SendMessageW(g_hBtnClear, WM_SETFONT, (WPARAM)g_hFontUI, TRUE);
y += dy;
/* Progress bar */
INITCOMMONCONTROLSEX icc = { sizeof(icc), ICC_PROGRESS_CLASS };
InitCommonControlsEx(&icc);
g_hProgress = CreateWindowW(PROGRESS_CLASSW, NULL,
WS_CHILD|WS_VISIBLE|PBS_SMOOTH,
12, y, WIN_W-24, 22, hwnd, (HMENU)ID_PROGRESS, NULL, NULL);
SendMessageW(g_hProgress, PBM_SETRANGE, 0, MAKELPARAM(0, 100));
SendMessageW(g_hProgress, PBM_SETPOS, 0, 0);
y += 30;
/* Status labels */
g_hLblStatus = CreateWindowW(L"STATIC", L"Status : --",
WS_CHILD|WS_VISIBLE,
12, y, 300, 20, hwnd, (HMENU)ID_LBL_STATUS, NULL, NULL);
SendMessageW(g_hLblStatus, WM_SETFONT, (WPARAM)g_hFontBold, TRUE);
y += 22;
g_hLblAvg = CreateWindowW(L"STATIC", L"Average : -- ms",
WS_CHILD|WS_VISIBLE,
12, y, 300, 20, hwnd, (HMENU)ID_LBL_AVG, NULL, NULL);
SendMessageW(g_hLblAvg, WM_SETFONT, (WPARAM)g_hFontUI, TRUE);
y += 22;
g_hLblRecv = CreateWindowW(L"STATIC", L"Received: --/--",
WS_CHILD|WS_VISIBLE,
12, y, 300, 20, hwnd, (HMENU)ID_LBL_RECV, NULL, NULL);
SendMessageW(g_hLblRecv, WM_SETFONT, (WPARAM)g_hFontUI, TRUE);
g_hLblMinMax = CreateWindowW(L"STATIC", L"Min: -- ms | Max: -- ms",
WS_CHILD|WS_VISIBLE,
320, y, 300, 20, hwnd, (HMENU)ID_LBL_MINMAX, NULL, NULL);
SendMessageW(g_hLblMinMax, WM_SETFONT, (WPARAM)g_hFontUI, TRUE);
y += 28;
/* Separator line */
h = CreateWindowW(L"STATIC", L"",
WS_CHILD|WS_VISIBLE|SS_ETCHEDHORZ,
12, y, WIN_W-24, 2, hwnd, NULL, NULL, NULL);
y += 8;
/* Output text area */
g_hOutput = CreateWindowW(L"EDIT", L"",
WS_CHILD|WS_VISIBLE|WS_BORDER|WS_VSCROLL|
ES_MULTILINE|ES_READONLY|ES_AUTOVSCROLL,
12, y, WIN_W-24, WIN_H-y-12,
hwnd, (HMENU)ID_EDIT_OUTPUT, NULL, NULL);
SendMessageW(g_hOutput, WM_SETFONT, (WPARAM)g_hFontMono, TRUE);
break;
}
case WM_COMMAND: {
int id = LOWORD(wParam);
if (id == ID_BTN_CLEAR) {
SetWindowTextW(g_hOutput, L"");
SendMessageW(g_hProgress, PBM_SETPOS, 0, 0);
SetLabel(g_hLblStatus, L"Status : --");
SetLabel(g_hLblAvg, L"Average : -- ms");
SetLabel(g_hLblRecv, L"Received: --/--");
SetLabel(g_hLblMinMax, L"Min: -- ms | Max: -- ms");
break;
}
if (id == ID_BTN_START) {
/* Read inputs */
wchar_t ip[256] = {0}, web[256] = {0}, cntStr[16] = {0};
GetWindowTextW(g_hEditIP, ip, 256);
GetWindowTextW(g_hEditWeb, web, 256);
GetWindowTextW(g_hEditCount, cntStr, 16);
/* Determine target */
wchar_t target[512] = {0};
if (wcslen(ip) > 0)
wcsncpy(target, ip, 511);
else if (wcslen(web) > 0)
wcsncpy(target, web, 511);
else {
MessageBoxW(hwnd,
L"Please enter an IP Address or Web Site.",
L"Input Required", MB_OK | MB_ICONWARNING);
break;
}
/* Strip http(s):// and path */
if (wcsncmp(target, L"https://", 8) == 0)
wmemmove(target, target+8, wcslen(target)-7);
else if (wcsncmp(target, L"http://", 7) == 0)
wmemmove(target, target+7, wcslen(target)-6);
wchar_t *slash = wcschr(target, L'/');
if (slash) *slash = L'\0';
int count = _wtoi(cntStr);
if (count < 1 || count > 100) count = 4;
/* Disable Start button during ping */
EnableWindow(g_hBtnStart, FALSE);
SetWindowTextW(g_hOutput, L"");
SendMessageW(g_hProgress, PBM_SETPOS, 0, 0);
SetLabel(g_hLblStatus, L"Status : Running...");
SetLabel(g_hLblAvg, L"Average : -- ms");
SetLabel(g_hLblRecv, L"Received: --/--");
SetLabel(g_hLblMinMax, L"Min: -- ms | Max: -- ms");
/* Launch ping thread */
PingArgs *args = (PingArgs *)malloc(sizeof(PingArgs));
args->hwnd = hwnd;
args->count = count;
wcsncpy(args->target, target, 511);
HANDLE hThread = CreateThread(NULL, 0, PingThread, args, 0, NULL);
if (hThread) CloseHandle(hThread);
}
break;
}
/* Line received from ping thread */
case WM_PING_LINE: {
wchar_t *line = (wchar_t *)lParam;
if (line) {
AppendOutput(g_hOutput, line);
AppendOutput(g_hOutput, L"\r\n");
free(line);
}
break;
}
/* Progress update from ping thread */
case WM_PING_PROG: {
SendMessageW(g_hProgress, PBM_SETPOS, wParam, 0);
break;
}
/* Ping completed */
case WM_PING_DONE: {
SendMessageW(g_hProgress, PBM_SETPOS, 100, 0);
EnableWindow(g_hBtnStart, TRUE);
PingResult *res = (PingResult *)lParam;
if (!res) break;
wchar_t buf[128];
/* Status */
_snwprintf(buf, 128, L"Status : %s", res->status);
SetLabel(g_hLblStatus, buf);
/* Average */
if (res->avg_ms >= 0)
_snwprintf(buf, 128, L"Average : %d ms", res->avg_ms);
else
wcscpy(buf, L"Average : N/A");
SetLabel(g_hLblAvg, buf);
/* Received */
if (res->sent > 0)
_snwprintf(buf, 128, L"Received: %d/%d", res->received, res->sent);
else
wcscpy(buf, L"Received: --/--");
SetLabel(g_hLblRecv, buf);
/* Min / Max */
if (res->min_ms >= 0 && res->max_ms >= 0)
_snwprintf(buf, 128, L"Min: %d ms | Max: %d ms",
res->min_ms, res->max_ms);
else
wcscpy(buf, L"Min: -- ms | Max: -- ms");
SetLabel(g_hLblMinMax, buf);
free(res);
break;
}
case WM_CTLCOLORSTATIC: {
HDC hdc = (HDC)wParam;
SetBkMode(hdc, TRANSPARENT);
HWND hCtrl = (HWND)lParam;
if (hCtrl == g_hLblStatus) {
wchar_t txt[64];
GetWindowTextW(hCtrl, txt, 64);
if (wcsstr(txt, L"ONLINE"))
SetTextColor(hdc, RGB(0, 140, 60));
else if (wcsstr(txt, L"OFFLINE"))
SetTextColor(hdc, RGB(200, 30, 30));
}
return (LRESULT)GetSysColorBrush(COLOR_BTNFACE);
}
case WM_DESTROY:
DeleteObject(g_hFontUI);
DeleteObject(g_hFontBold);
DeleteObject(g_hFontMono);
PostQuitMessage(0);
break;
default:
return DefWindowProcW(hwnd, msg, wParam, lParam);
}
return 0;
}
/* -------------------------------------------------------------------------
* WinMain
* ---------------------------------------------------------------------- */
int WINAPI WinMain(HINSTANCE hInst, HINSTANCE hPrev,
LPSTR lpCmd, int nShow) {
(void)hPrev; (void)lpCmd;
WNDCLASSW wc = {0};
wc.lpfnWndProc = WndProc;
wc.hInstance = hInst;
wc.hCursor = LoadCursor(NULL, IDC_ARROW);
wc.hbrBackground = (HBRUSH)(COLOR_BTNFACE + 1);
wc.lpszClassName = L"PingCheckerWin11";
wc.hIcon = LoadIcon(NULL, IDI_APPLICATION);
RegisterClassW(&wc);
g_hwnd = CreateWindowW(
L"PingCheckerWin11",
L"Yazilimadasi Ping Checker",
WS_OVERLAPPED | WS_CAPTION | WS_SYSMENU | WS_MINIMIZEBOX,
CW_USEDEFAULT, CW_USEDEFAULT,
WIN_W, WIN_H,
NULL, NULL, hInst, NULL);
ShowWindow(g_hwnd, nShow);
UpdateWindow(g_hwnd);
MSG msg;
while (GetMessageW(&msg, NULL, 0, 0)) {
TranslateMessage(&msg);
DispatchMessageW(&msg);
}
return (int)msg.wParam;
}