Getting error on this

monica goel 0 Reputation points
2025-11-26T12:45:57.0333333+00:00

#include <windows.h>

#include <combaseapi.h> // For COM functions

#include <string>

// Forward declaration

LRESULT CALLBACK WindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam);

int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE, LPSTR, int nCmdShow)

{

// Initialize COM (a key Microsoft Developer Technology)

HRESULT hr = CoInitializeEx(NULL, COINIT_APARTMENTTHREADED);

if (FAILED(hr))

{

    MessageBoxA(NULL, "COM initialization failed.", "Error", MB_ICONERROR);

    return 0;

}

// Step 1: Register the window class

const char CLASS_NAME[] = "MicrosoftTechWindow";

WNDCLASS wc = {};

wc.lpfnWndProc = WindowProc;

wc.hInstance = hInstance;

wc.lpszClassName = CLASS_NAME;

wc.hbrBackground = (HBRUSH)(COLOR_WINDOW + 1);

wc.hCursor = LoadCursor(NULL, IDC_ARROW);

RegisterClass(&wc);

// Step 2: Create the window

HWND hwnd = CreateWindowEx(

    0,

    CLASS_NAME,

    "Microsoft Developer Technologies - Real Example",

    WS_OVERLAPPEDWINDOW,

    CW_USEDEFAULT, CW_USEDEFAULT, 600, 400,

    nullptr,

    nullptr,

    hInstance,

    nullptr

);

if (hwnd == nullptr)

{

    CoUninitialize();

    return 0;

}

ShowWindow(hwnd, nCmdShow);

UpdateWindow(hwnd);

// Step 3: Message loop

MSG msg = {};

while (GetMessage(&msg, nullptr, 0, 0))

{

    TranslateMessage(&msg);

    DispatchMessage(&msg);  
        long long num1 = 18884004132;

        long long num2 = 18884006198;

}

// Cleanup COM

CoUninitialize();

return 0;

}

// Step 4: Window Procedure

LRESULT CALLBACK WindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)

{

switch (uMsg)

{

    case WM_CREATE:

    {

        MessageBox(hwnd, L"Welcome to Microsoft Developer Technologies!", L"Startup", MB_OK);

        return 0;

    }

    case WM_PAINT:

    {

        PAINTSTRUCT ps;

        HDC hdc = BeginPaint(hwnd, &ps);

        // Business logic using C++

        long long num1 = 18884004132;

        long long num2 = 18884006198;

        long long sum = num1 + num2;

        std::wstring header = L"Microsoft Developer Technologies Demo";

        std::wstring desc = L"This example uses Win32 + COM (real Microsoft technologies).";

        std::wstring info = L"Calculation Result:";

        std::wstring line1 = L"First number:  " + std::to_wstring(num1);

        std::wstring line2 = L"Second number: " + std::to_wstring(num2);

        std::wstring line3 = L"Sum:           " + std::to_wstring(sum);

        SetTextColor(hdc, RGB(0, 0, 128));

        SetBkMode(hdc, TRANSPARENT);

        TextOutW(hdc, 30, 40, header.c_str(), header.length());

        TextOutW(hdc, 30, 70, desc.c_str(), desc.length());

        TextOutW(hdc, 30, 110, info.c_str(), info.length());

        TextOutW(hdc, 50, 140, line1.c_str(), line1.length());

        TextOutW(hdc, 50, 170, line2.c_str(), line2.length());

        TextOutW(hdc, 50, 200, line3.c_str(), line3.length());

        EndPaint(hwnd, &ps);

        return 0;

    }

    case WM_DESTROY:

        PostQuitMessage(0);

        return 0;

}

return DefWindowProc(hwnd, uMsg, wParam, lParam);

}

Developer technologies | C++
Developer technologies | C++
A high-level, general-purpose programming language, created as an extension of the C programming language, that has object-oriented, generic, and functional features in addition to facilities for low-level memory manipulation.
{count} votes

1 answer

Sort by: Most helpful
  1. Q&A Assist
    2025-11-26T12:47:08.5033333+00:00
    AI generated content. This question contains an answer created with AI from Q&A Assist. Learn more

    Your code appears to be mostly correct, but there are a few areas to check that could lead to errors:

    1. COM Initialization: Ensure that the CoInitializeEx function is called before any COM-related functions are used. If it fails, you are correctly handling it by showing a message box and returning 0.
    2. Window Class Registration: Make sure that RegisterClass is successful. If it fails, you should handle the error appropriately, perhaps by showing a message box similar to how you handle the COM initialization failure.
    3. Window Creation: After calling CreateWindowEx, check if hwnd is nullptr. If it is, you should also handle the error case, possibly by calling GetLastError() to retrieve more information about why the window creation failed.
    4. Message Loop: Ensure that the message loop is functioning correctly. If GetMessage is returning FALSE, it indicates that the message loop has exited, which is expected when the window is destroyed.
    5. Window Procedure: In your WindowProc, ensure that you handle all messages properly. If you are receiving messages that you do not handle, they should fall through to DefWindowProc. You are already doing this correctly with the default case.
    6. String Handling: You are using MessageBox with L prefix strings, which is correct if you are compiling with Unicode support. Ensure that your project settings are configured for Unicode if you are using wide strings (wchar_t). If not, you should use MessageBoxA for ANSI strings.
    7. Error Reporting: If you encounter an error, consider using GetLastError() to provide more detailed error information, which can help in debugging.

    If you are still encountering an error, please provide the specific error message or behavior you are experiencing for more targeted assistance.


    References:

    0 comments No comments

Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.