Good Morning,

For the last few weeks I've been searching HIGH AND LOW. All forms, dozens of tabs open, and bought about 200 bucks in books. My goal is to learn the windows win32 API and I'm hitting brick walls everywhere I look. So far, I've learned that findwindow (or calls from user32.dll) can be accessed from winuser.h and I'm having a DIFFICULT TIME learning how to get around with the API calls. I'm okay with VB6 / VB 2010 but this, I want to rip my eyes out, freeze them put them in a sock and beat microsoft employees to death with them. I've played with code such as: 



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

int main()
{
    HWND hWnd;
    hWnd = FindWindow("CalcFrame", NULL);

    if ( hWnd != 1)
    {
        printf("Window is closed\n"); 
    } else {
        printf("The window is open!\n"); 
    }

    return 0; 
    }


However, NOTHING is working! Maybe there is a resource online for C API calls in windows? Maybe there is a book? Please I need something. I should also mention that **I AM NOT** using .NET. and I'm using Dev-C++ and NetBeans (which I'm leaning more towards NetBeans). Someone, PLEASE HELP!!! 

Thank you, 

amvx86.

Dani AI

Generated

As pointed out, Visual Studio makes Win32 work much easier out of the box; as noted, Dev‑C++ typically uses TDM‑GCC so you may need to add Windows libraries yourself. The immediate problems in the OP’s example are not mystical — they’re common mistakes: treating an HWND like an integer, relying on brittle class names, and not checking Unicode/link settings.

FindWindow returns NULL on failure; any non-NULL HWND means “found.” For reliable diagnostics, clear the last error, call the API, then read GetLastError if you get NULL. Avoid assuming a class name will be the same across Windows versions. A simple corrected pattern:

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

int main(void)
{
    SetLastError(0);
    HWND h = FindWindowW(NULL, L"Calculator"); /* or pass a known class name */
    if (h == NULL) {
        DWORD err = GetLastError();
        printf("Window not found (error %lu)\n", (unsigned long)err);
        return 1;
    }
    printf("Found HWND=%p\n", (void*)h);
    return 0;
}

Build and environment tips: link against user32 (user32.lib in MSVC or add -luser32 with MinGW/TDM‑GCC). Prefer the wide APIs (FindWindowW or define UNICODE) to avoid ANSI/Unicode mismatches. If a target is a modern UWP/Store app or its title/class has changed, FindWindow may never match — use EnumWindows + GetWindowText/GetWindowThreadProcessId to examine windows, or use a UI Automation approach for robust automation.

Quick checklist to resolve similar issues: 1) compile/link for Win32 and include user32, 2) use the correct NULL check on HWND, 3) prefer wide APIs or explicit W/A variants, 4) inspect windows with Spy++/window inspector to find real class/title, and 5) fall back to EnumWindows or UI Automation if FindWindow proves unreliable.

Recommended Answers

All 2 Replies

Frankly, if I was going to write Win32 Apps I'd head straight to Visual Studio. Since there are free versions for C++ and C# there's not much reason to deal with getting other dev systems to work. Yes, there are some that abhor all things Microsoft but here you are trying to make Windows apps so use the tools I see folk use.

About learning the Win32 API. That's a lifetime topic. My view has been to only use the Win32 API when nothing else will do.

What says, however in your case you do need the Windows API for this. What happens? Does it just hang, or does it give the negative answer? Also, what compiler are you using, MS's visual C compiler? Some other compiler that supports the win32 API? Nevermind. I see you are using Dev-C++ which is a non-MS IDE, and by default uses TDM-GCC, a version of the Gnu compiler suite. As rproffitt said, try the MS Visual Studio compiler. It should have no problem with your .C or .CPP/.CC files.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.