Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8359457
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T11:09:00+00:00 2026-06-09T11:09:00+00:00

The following code works on Window 7, but when I run it on Windows

  • 0

The following code works on Window 7, but when I run it on Windows XP, it fails with an error message returned by windows: “The system can not find the file specified”.

#include <windows.h>
#include <windowsx.h>
#include <commctrl.h>
#include <uxtheme.h>
#include <string>

const char g_szClassName[] = "myWindowClass";
const char title[] = "Window Title\0";

COLORREF        WinColor;
HFONT       defaultFont;
NONCLIENTMETRICSA   Metrics;
DWORD       dwVersion;
DWORD       dwMajorVersion;
DWORD       dwMinorVersion;
HBRUSH      hBrushColor;

bool LastError = false;
bool W32Error (const char * Msgtext);

// Step 4: the Window Procedure
LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
    switch(msg)
    {

    case WM_NCCREATE:
    {
        Metrics.cbSize = sizeof(NONCLIENTMETRICS);
        SystemParametersInfo (SPI_GETNONCLIENTMETRICS, sizeof(NONCLIENTMETRICS), &Metrics, 0);
        defaultFont = (HFONT) CreateFontIndirect (& Metrics.lfMessageFont);
        return TRUE;
    }
    break;

    case WM_CTLCOLORSTATIC: {
        dwVersion = GetVersion();
        dwMajorVersion = (DWORD)(LOBYTE(LOWORD(dwVersion)));
        dwMinorVersion = (DWORD)(HIBYTE(LOWORD(dwVersion)));
        if ( (IsAppThemed()) && (dwMajorVersion < 6) ) {
            WinColor = GetSysColor(COLOR_WINDOW);
            SetBkColor((HDC)wParam, WinColor);
            return (LRESULT)hBrushColor;
        }
    }
    break;

    case WM_CREATE: {
        HWND hButton = CreateWindowEx(
            0,
            "BUTTON", "Button",
                WS_TABSTOP      |
                WS_VISIBLE      |
                WS_CHILD        |
                BS_NOTIFY       |
                BS_PUSHBUTTON,
            10, 10, 96, 32, hwnd,
            (HMENU)50,
            GetModuleHandle(NULL),
            NULL);
        if (W32Error ("Button Creation Failed\nReason:")) exit (1);
        return TRUE;
    }
    break;

    case WM_CLOSE:
        DestroyWindow(hwnd);
        break;

    case WM_DESTROY:
        PostQuitMessage(0);
        break;

    default:
        return DefWindowProc(hwnd, msg, wParam, lParam);
    }
    return 0;
}

int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
    LPSTR lpCmdLine, int nCmdShow)
{
    WNDCLASSEX wc;
    HWND hwnd;
    MSG Msg;

    // Initialize common controls.
    INITCOMMONCONTROLSEX icc;
    icc.dwSize = sizeof(icc);
    icc.dwICC = ICC_WIN95_CLASSES       |
                ICC_COOL_CLASSES    |
                ICC_INTERNET_CLASSES|
                ICC_LINK_CLASS  |
                ICC_STANDARD_CLASSES|
                ICC_PROGRESS_CLASS  |
                ICC_USEREX_CLASSES;
    InitCommonControlsEx(&icc);

    //Step 1: Registering the Window Class
    wc.cbSize        = sizeof(WNDCLASSEX);
    wc.style         = CS_HREDRAW | CS_VREDRAW;
    wc.lpfnWndProc   = WndProc;
    wc.cbClsExtra    = 0;
    wc.cbWndExtra    = 0;
    wc.hInstance     = hInstance;
    wc.hIcon         = LoadIcon(NULL, IDI_APPLICATION);
    wc.hCursor       = LoadCursor(NULL, IDC_ARROW);
    wc.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
    wc.lpszMenuName  = NULL;
    wc.lpszClassName = g_szClassName;
    wc.hIconSm       = LoadIcon(NULL, IDI_APPLICATION);

    // see if something is going on before the window registration takes place....
    if (W32Error ("Previous Check for Error?\nReason:")) exit (1);

    if(!RegisterClassEx(&wc))
    {
        MessageBox(NULL, "Window Registration Failed!", "Error!",
            MB_ICONEXCLAMATION | MB_OK);
        return 0;
    }
    if (W32Error ("Window Registration Failed\nReason:")) exit (1);

    // Step 2: Creating the Window
    hwnd = CreateWindowEx(
        WS_EX_CLIENTEDGE,
        g_szClassName,
        "The title of my window",
        WS_OVERLAPPEDWINDOW,
        CW_USEDEFAULT, CW_USEDEFAULT, 240, 120,
        NULL, NULL, hInstance, NULL);
    if (W32Error ("Window Creation Failed\nReason:")) exit (1);

    if(hwnd == NULL)
    {
        MessageBox(NULL, "Window Creation Failed!", "Error!",
            MB_ICONEXCLAMATION | MB_OK);
        return 0;
    }

    SendMessageA(hwnd, WM_SETFONT, WPARAM (defaultFont), TRUE);
    SendMessageA(hwnd, WM_SETTEXT, WPARAM(NULL) , LPARAM (title));

    ShowWindow(hwnd, nCmdShow);
    UpdateWindow(hwnd);

    // Step 3: The Message Loop
    while(GetMessage(&Msg, NULL, 0, 0) > 0)
    {
        TranslateMessage(&Msg);
        DispatchMessage(&Msg);
    }
    return Msg.wParam;
}


bool W32Error (const char * Msgtext)
{
    LPTSTR errorText = NULL;
    DWORD   dwLastError = GetLastError();
    if (!dwLastError) {
        LastError = false;
        return LastError;
    }
    // use system message tables to retrieve error text
    // allocate buffer on local heap for error text
    // Important! will fail otherwise, since we're not (and CANNOT) pass insertion parameters
    FormatMessageA(
        FORMAT_MESSAGE_FROM_SYSTEM     |
        FORMAT_MESSAGE_ALLOCATE_BUFFER |
        FORMAT_MESSAGE_IGNORE_INSERTS,
        NULL,                           // unused with FORMAT_MESSAGE_FROM_SYSTEM
        dwLastError,
        MAKELANGID (LANG_NEUTRAL, SUBLANG_DEFAULT),
        (LPTSTR)&errorText,             // output
        0,                              // minimum size for output buffer
        0);                             // arguments - see note
        if ( NULL != errorText ) {
            std::string Message;
            Message += Msgtext;
            Message += "\n";
            Message += errorText;
            MessageBoxA(NULL, (LPCSTR)Message.c_str(), "An Internal Error Occurred", MB_OK);
            LocalFree(errorText);
            errorText = NULL;
            LastError = true;
        } else {
            LastError = false;
        }
        return LastError;
}

As an additional, I have a resource.rc and resource.h file linked in with winres that complies in a manifest.xml that provides a controls version 6 specification.

Ok, so here is the deal, This runs on Windows 7 without a hitch. And it runs on XP, unless I put in the line if (W32Error ("Window Registration Failed\nReason:")) exit (1);
(basically, it calls my error checking routine which includes GetLastError())

   if(!RegisterClassEx(&wc))
    {
        MessageBox(NULL, "Window Registration Failed!", "Error!",
            MB_ICONEXCLAMATION | MB_OK);
        return 0;
    }
    if (W32Error ("Window Registration Failed\nReason:")) exit (1);

When I include that line, I get an error “The system can not find the file specified.” The return value back from RegisterClassEx does not go into the if block, so RegisterClassEx returns Ok.

This only happens on Windows XP, it works just fine on Windows 7. If I don’t include this line, it works as if nothing is wrong.

The point of this sample is to get themes to work right on both XP and Windows 7. (not an easy task.)

Why is RegisterClassEx returning Ok, but generates an error for GetLastError on Windows XP and not on Windows 7?

Also, what file is it looking for anyway? I have seen other Google results say It has to do with a messed up message loop or window procedure, but that’s not the case here.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-09T11:09:01+00:00Added an answer on June 9, 2026 at 11:09 am

    General mistake… Due to MSDN you may check GetLastError only when RegisterClassEx “return value is zero”. When RegisterClassEx or CreateWindowEx or any other functions are succeeded, GetLastError may return garbage, there is no guarantee that GetLastError will returns zero (if different behavior does not described in Return value section).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

The following code works as expected on CentOS and Ubuntu O/s but not on
The following code works, but I can't figure out what's going on memory-wise. Where
The following xaml code works: <Window x:Class=DerivedTemplateBug.Window1 xmlns=http://schemas.microsoft.com/winfx/2006/xaml/presentation xmlns:x=http://schemas.microsoft.com/winfx/2006/xaml xmlns:local=clr-namespace:DerivedTemplateBug Title=Window1 Height=300 Width=300> <Button>
The following code: <input style=height: 80px; width: 200px; value=test button> Works fine on Windows,
The following code works fine on Linux but throws an exception on OS X
The following code works in all browser but IE8 and below. // Create Paragraph
The following code works fine in firefox but as with many other things, I
To visualize data from F# interactive console, I can do the following: open System.Windows.Forms
I got the following code to run the application at windows startup: private void
I get the following error message trying to run my page Compiler Error Message:

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.