Static function, send message to hidden window

Hi Experts,

I'm using a named pipe to do IPC.  My callback function is static.  When I receive commands from this pipe, I need to call my instance class somehow - the only way I could think of was with SendMessage.  However, my AfxGetMainWnd is returning NULL since I'm not showing my window.  The reason:  this app is only displaying a system tray icon, so the main window is invisible.  How can I communicate with my class from the static callback?

Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Add a static variable (pointer to the instance of your class) into the cpp-file with your static function. This static variable you can use in the static function.

Not in this case probably, but, for example, a first parameter of this static function can be the object you need to use in the static function. So in the function you can access all its public methods.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
trinitrotolueneDirector - Software EngineeringCommented:
Lets look at the situation on your hands. Try analyzing whether it is absolutely essential to have a static callback function in the first place.

If you are performing some form of IPC I would suggest that you look at maintaining the abstraction of object communication. In such a case your callback function should also be serving an object.
In other words I am asking you to check whether you can make the callback function non-static.

Still if this cannot be avoided there is another alternative you might want to consider:

make the callbacks free functions in a separately named namespace. See my snippet below:
trinitrotolueneDirector - Software EngineeringCommented:
oops my snippet didnt get attached
namespace MyCallbacks
 rettype cb1(arg1, arg2 ..);
 rettype cb2(arg1..);

 rettype cb1(arg1, arg2)
   Objecttobecommunicatedwith* ptr = new Objecttobecommunicatedwith()


Open in new window

Python 3 Fundamentals

This course will teach participants about installing and configuring Python, syntax, importing, statements, types, strings, booleans, files, lists, tuples, comprehensions, functions, and classes.

Even if your window is hidden, 'FindWindow()' ( will still return a handle. So you could
HWND hWnd = FindWindow(_T("Window Name"),_T("Class Name or NULL"));


Open in new window

threadyAuthor Commented:
Thanks pgnatyuk, I don't know why I didn't think of that!

trinitrotoluene, that looks nice and clean, but can you think of a way to do it without creating a new object to call its method?  i.e., can i make my callback point to a specific object?

jkr, that looks dirty!  ;-)   but it would work....
>> jkr, that looks dirty!  ;-)

Absolutely not, that's a standard procedure on Windows.
threadyAuthor Commented:
jkr - how do I name my window?  The window happens to be the mainframe so there's only 1, but isn't there a chance that another instance of the program could have the same name?  How do you resolve the ambiguity?

threadyAuthor Commented:
pgnatyuk:  I created my static pointer as you suggested, just to get this working quickly, but I get a compiler error:

MainFrm.obj : error LNK2001: unresolved external symbol "public: static struct HWND__ * CMainFrame::m_hWndInstance" (?m_hWndInstance@CMainFrame@@2PAUHWND__@@A)

Why is it unresolved?
threadyAuthor Commented:
I really hate how c++ differes from java in its handling of statics....  I can never get it through my think head...  I always run into this problem whenever I use statics and I can't remember what to do...  I seem to remember needing to declare some other kind of forward declaration or something....
threadyAuthor Commented:
thick head...  not think head...  :-)
You added a static variable to your class. So you need to initialize it in the cpp-file.
HWND CMainFrame::m_hWndInstance = NULL;

You need to assign a value to this variable after you create the window, and set it to NULL when you are destroying it.

>>how do I name my window?

Since you are using MFC, the Window name should be placed in the resources.
BTW, if you can't locate it there, you can use Spy+ to find out what the title and the class name are.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
System Programming

From novice to tech pro — start learning today.