You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在C语言(WinAPI)中打印活动窗口的标题

How to Print the Active Window Title in C (Windows API)

Hey there! Let's work through this together since you're still getting up to speed with Windows API concepts like HWND.

First, let's clear up why your initial code didn't work:

  • GetActiveWindow() is a function, so you need to call it with parentheses (GetActiveWindow())—not just write the function name.
  • Most importantly, HWND is a window handle—a unique identifier Windows uses to reference a window. It's not a string, so you can't pass it directly to printf("%s") (that expects a text pointer, not a window ID!).

Also, quick note: GetActiveWindow() only gets the active window from your program's thread. If you want the window the user is actually interacting with right now (across all apps), you should use GetForegroundWindow() instead.

Working Example Code

Here's a complete, runnable program that grabs the foreground window's title and prints it:

#include <windows.h>
#include <stdio.h>

int main() {
    // Step 1: Get the handle of the foreground window (user's active window)
    HWND foregroundWnd = GetForegroundWindow();
    if (foregroundWnd == NULL) {
        printf("Error: Couldn't get foreground window.\n");
        return 1;
    }

    // Step 2: Get the length of the window title (includes null terminator)
    int titleLength = GetWindowTextLengthW(foregroundWnd);
    if (titleLength == 0) {
        printf("Error: Window has no title, or failed to retrieve length.\n");
        return 1;
    }

    // Step 3: Allocate memory to store the title (wide character format)
    WCHAR* windowTitle = (WCHAR*)LocalAlloc(LPTR, (titleLength + 1) * sizeof(WCHAR));
    if (windowTitle == NULL) {
        printf("Error: Couldn't allocate memory for the title.\n");
        return 1;
    }

    // Step 4: Retrieve the actual window title text
    int result = GetWindowTextW(foregroundWnd, windowTitle, titleLength + 1);
    if (result == 0) {
        printf("Error: Failed to get window text.\n");
        LocalFree(windowTitle); // Clean up memory before exiting
        return 1;
    }

    // Step 5: Print the title (use wprintf for wide characters)
    wprintf(L"Active Window Title: %s\n", windowTitle);

    // Step 6: Clean up the allocated memory
    LocalFree(windowTitle);

    return 0;
}

Key Explanations

Let's break down what each part does:

  • GetForegroundWindow(): Fetches the handle of the window currently in focus (the one the user is typing/clicks in).
  • GetWindowTextLengthW(): Gets the length of the window's title in wide characters (Windows uses Unicode by default, so we use the W suffix for wide-API functions).
  • LocalAlloc(): Allocates memory to hold the title. LPTR ensures the memory is initialized to zero, which helps avoid garbage data.
  • GetWindowTextW(): Copies the window's title text into our allocated buffer.
  • wprintf(): Prints wide character strings (since windowTitle is a WCHAR*, not a regular char*).
  • LocalFree(): Releases the memory we allocated—always clean up after yourself to avoid leaks!

Compilation Note

To compile this, you'll need a Windows C compiler like MSVC or MinGW. If using MinGW, compile with:

gcc your_file.c -o window_title.exe -luser32

The -luser32 links against the User32 library, which contains all the Windows window functions we're using.

内容的提问来源于stack exchange,提问作者Sheldon

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.12 03:43:35