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 843343
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T06:06:40+00:00 2026-05-15T06:06:40+00:00

There is a simple WinAPI application. All it does currently is this: register a

  • 0

There is a simple WinAPI application. All it does currently is this:

  • register a window class
  • register a tray icon with a menu
  • create a value in the registry in order to autostart
  • and finally, it checks if it’s unique using a mutex

As I’m used to writing code mainly in C++, and no MFC is allowed, I’m forced to encapsulate this into C++ classes somehow. So far I’ve come up with such a design:

  • there is a class that represents the application
  • it keeps all the wndclass, hinstance, etc variables, where the hinstance is passed as a constructor parameter as well as the icmdshow and others (see WinMain prototype)
  • it has functions for registering the window class, tray icon, reigstry information
  • it encapsulates the message loop in a function

In WinMain, the following is done:

Application app(hInstance, szCmdLIne, iCmdShow);
return app.exec();

and the constructor does the following:

registerClass();
registerTray();
registerAutostart();

So far so good. Now the question is : how do I create the window procedure (must be static, as it’s a c-style pointer to a function) AND keep track of what the application object is, that is, keep a pointer to an Application around.

The main question is : is this how it’s usually done? Am I complicating things too much? Is it fine to pass hInstance as a parameter to the Application constructor? And where’s the WndProc?

Maybe WndProc should be outside of class and the Application pointer be global? Then WndProc invokes Application methods in response to various events.

There’s one more possible solution : make the application class a singleton. Then it’s trivial to obtain the handle to that object from the WndProc.

  • 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-05-15T06:06:41+00:00Added an answer on May 15, 2026 at 6:06 am

    The answer is SetWindowLongPtr. It allows you to associate a void* with a given hWnd. Then, in the WndProc, you just extract said void*, cast, and call the member method. Problemo solvo. There’s a few ups/downs with SetWindowLongPtr, you must call some other function to see the effects or somesuch BS, and Windows sends messages before CreateWindowEx returns, so you must be prepared for GetWindowLongPtr(hWnd, GWL_USERDATA) to return NULL.

    This of course means that for a given WindowProc, all instances that use it must have a common interface, since there’s not much you can do with a void*.

    And, yes, it’s fine to pass HINSTANCE to the App constructor. I’ve seen samples that do something strange to avoid this but I never made it work myself.

    Edit:
    Don’t confuse Get/SetWindowLong with Get/SetWindowLongPtr. Get/SetWindowLong is deprecated and unsafe.

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

Sidebar

Ask A Question

Stats

  • Questions 434k
  • Answers 434k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer I've added a gtk_widget_realize(mainWindowHandle); before the gtk_widget_get_style and works perfectly! May 15, 2026 at 3:07 pm
  • Editorial Team
    Editorial Team added an answer With one instance in array, flash converts the array into… May 15, 2026 at 3:07 pm
  • Editorial Team
    Editorial Team added an answer I would read the following articles they describe your problem.… May 15, 2026 at 3:07 pm

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.