can i run another program inside my window?
Posted on 2004-02-19 00:42:53 by Ni?o
This sounds like a question for f0dder
Posted on 2004-02-19 01:07:11 by mrgone
What do you really mean by this question?

Could you elaborate a little so we can understand your question better?

For example you could run another program via CreateProcess API from your programm/window
Posted on 2004-02-19 02:40:28 by BogdanOntanu
Hmm, it would be difficult to think up a question that had more answers and covered more topics than that one. I think you will have to be a little more specific.
Posted on 2004-02-19 02:47:30 by donkey
Hi,

You can run another program from your program,calling those APIs: ShellExecute or CreateProcess.

You can also exchange data between two programs with Pipes and those APIs: CreatePipe and CreateNamedPipe.

But maybe this isn't what you're waiting for...
Posted on 2004-02-19 06:56:00 by Neitsa
Nino, you can do that. But your child process will have to somehow be given the hWnd of the window you want its main window in. In other words, both apps must be made by you, and the child app will have to retrieve the parent hWnd by FindWindow (for example). Then, using that hWnd, to create its own window using CreateWindowEx, but the hWndParent must be set to the retrieved hWnd.
You need to use CreateProcess, where the bInheritHandles must be set to 1 ("true").
Posted on 2004-02-19 07:46:54 by Ultrano
Here's a working example. Don't worry about the includes - I have built it, and you just need to see maximum clear code.
Posted on 2004-02-19 08:38:30 by Ultrano
Well, the chld application can determine it's parent without needing the value passed to it. I had the same problem recently for a program that shuts down any program that starts it via CreateProcess, (Note will not function in NT4):

/*

Extract the PID of our parent process
*/

GetParentProcess FRAME
LOCAL ourPID :D
LOCAL hProcessSnap :D
LOCAL pe32 :PROCESSENTRY32

/*
Thanks to MichaelW
*/

call GetCurrentProcessId
mov [ourPID],eax

invoke CreateToolhelp32Snapshot,TH32CS_SNAPPROCESS,0
mov [hProcessSnap],eax

mov D[pe32.dwSize],SIZEOF PROCESSENTRY32
invoke Process32First,[hProcessSnap],addr pe32
or eax,eax
jz >W2

W1:
mov eax,[pe32.th32ProcessID]
cmp eax,[ourPID]
je >W2
invoke Process32Next,[hProcessSnap],addr pe32
or eax,eax
jnz <W1
W2:

invoke CloseHandle,[hProcessSnap]

mov eax,[pe32.th32ParentProcessID]
ret

ENDF

/*
Finding the window of a parent app requires enumerating the thread windows
So we need a thread ID...
*/

FindProcessThread FRAME PID
LOCAL hThreadSnap :D
LOCAL te32 :THREADENTRY32

/*
Returns a thread ID from a given process
used to send the shutdown messages to the process
*/

invoke CreateToolhelp32Snapshot,TH32CS_SNAPTHREAD,0
mov [hThreadSnap],eax

mov D[te32.dwSize],SIZEOF THREADENTRY32
invoke Thread32First,[hThreadSnap],OFFSET te32
or eax,eax
jz >.EXIT
W1:
mov eax,[te32.th32OwnerProcessID]
cmp eax,[PID]
je >W2
invoke Thread32Next,[hThreadSnap],OFFSET te32
or eax,eax
jnz <W1
jmp >.EXIT
W2:
mov eax,[te32.th32ThreadID]
.EXIT
push eax
invoke CloseHandle,[hThreadSnap]
pop eax
RET
ENDF

/*
You would call:
invoke EnumThreadWindows,[idThread],OFFSET EnumThreadWndProc,OFFSET MsgParams
*/
EnumThreadWndProc FRAME hwnd,lParam
uses edi

/*
This procedure will send a message to the parent application
it requires that lParam contain a pointer to a MSGPARAMS structure
the enumeration will stop when a non-zero result has been recieved

MSGPARAMS STRUCT
MsgID DD
MsgwParam DD
MsglParam DD
Result DD
ENDS

The procedure will wait a maximum of 2 seconds for a reply from each window

responding to this message from a dialog requires that the parent
use the following syntax to properly return a value:

invoke SetWindowLong,DWL_MSGRESULT,[dwResult]
xor eax,eax
inc
ret
*/

mov edi,[lParam]
mov edx,edi

add edx,MSGPARAMS.Result
mov D[edx],FALSE

invoke SendMessageTimeout,[hwnd],[edi+MSGPARAMS.ID],[edi+MSGPARAMS.wParam],\
[edi+MSGPARAMS.lParam],SMTO_ABORTIFHUNG,2000,edx

; Stop the enumeration when we get a non-zero response
mov eax,[edi+MSGPARAMS.Result]
or eax,eax
jz >
xor eax,eax
ret
:
xor eax,eax
inc eax
ret
ENDF
Posted on 2004-02-19 09:23:10 by donkey
:) seems longer and harder to code than just a "FindWindow" :)
and it may create its window in the wrong window :grin:
in all cases, the child must know it'll be owned and by whom.
But I wonder if I use SetParent.


Here's the result lol never seen notepad in this state. Imprisoned :) No need for the child to know anything about its parent.
Posted on 2004-02-19 10:17:46 by Ultrano
Hi Ultrano,

It isn't really applicable in this context I guess, I just thought it was an interesting bit of code and since we were talking about parent process windows anyway....

It does work quite well for sending messages to the parent application however. I am using it in my HTTPUpdater to pass messages (like "I'm shutting down" or "You need to shut down") to the process that has called it. So in my case I cannot know the class or name of the window. Also I did not want to use Broadcast message as other apps that used the updater would all shut down as well. So I narrowed the target of the message to a single PID, that way I don't have to rely on the application passing the parameter and can still send my messages. It's mostly one of those "even if you completely misunderstand the interface it will correct itself" type of things.
Posted on 2004-02-19 10:26:15 by donkey