Solved

ActiveX Control Interprocess Communication (IPC)

Posted on 2008-10-15
5
1,517 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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
sumHeights2  challenge 7 102
How to split this in C++ 4 101
Resolve Dependency Issues 4 64
Embarcadero C++ builder XE10.1 Berlin TRegistry declaration 1 25
Introduction: Database storage, where is the exe actually on the disc? Playing a game selected randomly (how to generate random numbers).  Error trapping with try..catch to help the code run even if something goes wrong. Continuing from the seve…
If you use Adobe Reader X it is possible you can't open OLE PDF documents in the standard. The reason is the 'save box mode' in adobe reader X. Many people think the protected Mode of adobe reader x is only to stop the write access. But this fe…
The goal of this video is to provide viewers with basic examples to understand opening and writing to files in the C programming language.
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

813 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

16 Experts available now in Live!

Get 1:1 Help Now