Solved

ActiveX Control Interprocess Communication (IPC)

Posted on 2008-10-15
5
1,498 Views
Last Modified: 2013-11-20
I have an ActiveX (.ocx) control that starts a windows application (winapp.exe) via a spawn call (_spawnl from process.h). The control has a windowed (vs windowless) and thus an associated hWnd (CWnd::m_hWnd). I designed the control so that after the control spawns the winapp, it waits around until the winapp has initialized
itself, and sends back the WIN_APP_READY message to the control. This works as long as the winapp.exe knows the m_hWnd value of the control, but unless I pass that value via command line, I can't figure out how to get the winapp.exe to determine the control's m_hWnd. How can the winapp.exe find the controls m_hWnd via IPC? I looked into the following ways.

1. In the winapp.exe I tried to use EnumWindow() call with a FindWindowEx() to locate the control's m_hWnd, but the ActiveX control is not found because it is not a top level window, or an immediate child window.
2. SendMessage(HWND_BROADCAST,, ..) only sends to top level windows, thus the control does never receive the message.
3. Considering the control spawns a pid, I thought the winapp.exe could use the ppid to identify the hWnd (code examples seem too complex, and possibly prone to security model violations/errors).
4. Associate the control's m_hWnd using a mutex (if possible). I don't know that this would work, but thought it might be a way.

Any other ideas?

Thanks
0
Comment
Question by:slicer123
  • 3
  • 2
5 Comments
 
LVL 86

Accepted Solution

by:
jkr earned 500 total points
ID: 22723670
For IPC in general, see http://www.codeproject.com/KB/threads/Win32IPC.aspx ("Windows IPC"). Yet for your purpose, that'd be overkill -I'd just pass the window handle as a command line parameter, e.g.
// OCX
 

char buf[MAX_PATH];
 

sprintf(buf,"winapp.exe %d", m_hWnd);
 

// exe
 

#include <stdlib.h>
 

//...
 

LPWSTR pCmd = GetCommandLineW();
 

LPWSTR* pArgs = CommandLineToArgvW(pCmd,2);
 

HWND hOCX = (HWND) _wtoi(pArgs[1]);

Open in new window

0
 
LVL 2

Author Comment

by:slicer123
ID: 22723865
The reason I am looking for another solution is because it requires modifying the startup code of winapp.exe. Thus I was hoping for a simple method of using FindWindow calls. Basically I wanted the code to be as portable as possible, e.g. works under Xp and Vista, works under x86 and x64, does not trigger security model errors, does not blow up other apps, etc.
But failing this, another possibility according to some google results for topic "Spawning Applications and Discovering Window Handles"*, I found that one way is to have the controlling app run a timer event until finding the window has initialized, then passing the hWnd in an LPARAM via a user defined Windows Messaging Protocol.
* Borland C++ Builder 6 Developer's Guide By Bob Swart, Jarrod Hollingworth, Mark Cashman, Paul Gustavson, Chap 14, "Win32 API Functional Areas", pp 538
0
 
LVL 86

Expert Comment

by:jkr
ID: 22724203
IMO the command line parameter method is the easiest and most compatible one for all Windows platforms - and since you have to change the code anyway, why shouldn't it be the startup code?
0
 
LVL 86

Expert Comment

by:jkr
ID: 22724211
BTW, another simple way would be to put that value somewhere in the registry.
0
 
LVL 2

Author Closing Comment

by:slicer123
ID: 31506398
ty
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Introduction: Ownerdraw of the grid button.  A singleton class implentation and usage. Continuing from the fifth article about sudoku.   Open the project in visual studio. Go to the class view – CGridButton should be visible as a class.  R…
Introduction: Dialogs (2) modeless dialog and a worker thread.  Handling data shared between threads.  Recursive functions. Continuing from the tenth article about sudoku.   Last article we worked with a modal dialog to help maintain informat…
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use nested-loops in the C programming language.
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…

706 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now