Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save graceman9/e8633912537e4f1f883086d8496010bc to your computer and use it in GitHub Desktop.
Save graceman9/e8633912537e4f1f883086d8496010bc to your computer and use it in GitHub Desktop.
win api find all windows with name/title "TelegramDesktop". i want raw WIN_API, not python. i want it in DLL.

To create a DLL in C++ that finds all windows with the title "TelegramDesktop" using raw Windows API, you should follow these steps:

  1. Set up your development environment: Ensure you have a C++ development environment, such as Visual Studio, set up for creating Windows-based DLLs.

  2. Write the DLL code:

    • Use Windows API functions like EnumWindows, GetWindowText, and GetWindowTextLength.
    • Implement a function that will store or print window information if its title matches "TelegramDesktop".

Here's an example implementation:

TelegramFinder.cpp

#include <Windows.h>
#include <vector>
#include <string>

std::vector<HWND> g_TelegramWindows;

BOOL CALLBACK EnumWindowsProc(HWND hwnd, LPARAM lParam) {
    // Get the length of the window's title
    int length = GetWindowTextLength(hwnd);
    if (length == 0) {
        return TRUE; // Continue enumeration
    }

    // Get the window's title
    std::vector<char> title(length + 1);
    GetWindowTextA(hwnd, title.data(), length + 1);

    // Compare the window title
    if (strcmp(title.data(), "TelegramDesktop") == 0) {
        // Add to the list if it matches
        g_TelegramWindows.push_back(hwnd);
    }

    return TRUE; // Continue enumeration
}

extern "C" __declspec(dllexport) std::vector<HWND> FindTelegramWindows() {
    g_TelegramWindows.clear(); // Clear the list

    // Enumerate all windows
    EnumWindows(EnumWindowsProc, NULL);

    return g_TelegramWindows;
}

TelegramFinder.def

LIBRARY "TelegramFinder"
EXPORTS
    FindTelegramWindows

Building the DLL

  1. Create a new DLL project in your IDE, such as Visual Studio.

  2. Add the source file (TelegramFinder.cpp) to the project.

  3. Create a module-definition file (TelegramFinder.def) to specify the exported functions.

  4. Compile the project: This will generate the DLL and an import library.

Using the DLL

To use this DLL in an application:

  1. Load the DLL with LoadLibrary.
  2. Retrieve the function address using GetProcAddress.
  3. Call the function to get the vector of window handles.

Here's a high-level example in code to show how it would work in a separate application:

#include <Windows.h>
#include <iostream>
#include <vector>

typedef std::vector<HWND>(*FindWindowsFunc)();

int main() {
    // Load the DLL
    HMODULE hMod = LoadLibraryA("TelegramFinder.dll");
    if (!hMod) {
        std::cerr << "Failed to load DLL" << std::endl;
        return -1;
    }

    // Get the function pointer
    FindWindowsFunc findWindows = (FindWindowsFunc)GetProcAddress(hMod, "FindTelegramWindows");
    if (!findWindows) {
        std::cerr << "Failed to get function address" << std::endl;
        FreeLibrary(hMod);
        return -1;
    }

    // Call the function
    std::vector<HWND> windows = findWindows();
    std::cout << "Found " << windows.size() << " windows with title 'TelegramDesktop':" << std::endl;
    for (HWND hwnd : windows) {
        std::cout << hwnd << std::endl;
    }

    // Free the library
    FreeLibrary(hMod);
    return 0;
}

Additional Considerations

  • Character Encoding: This example uses ANSI strings. If dealing with international or Unicode characters, consider using GetWindowTextW instead and adjust the code to handle wide strings (e.g., std::wstring).
  • Error Handling: Add proper error checking where necessary in a real-world scenario.
  • Dependencies: Make sure any project that uses this DLL is set up to find and link against it properly.

This guidance will help you build and use a DLL that encapsulates finding windows by title using raw Windows API calls.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment