News:

MASM32 SDK Description, downloads and other helpful links
MASM32.com New Forum Link
masmforum WebSite

execute another application from program

Started by white scorpion, January 28, 2005, 11:56:47 PM

Previous topic - Next topic

white scorpion

Hi All,

i forgot the name of the function to execute another application from a program, i've tried google to find it, but unfortunately no luck.
can someone please tell me which function this is?


system("cmd.exe");

this is the same in C, but i forgot the one for masm, i think it was something like execl.

thanks in advance,

Relvinian

Take a look at two Win32 API functions:

CreateProcess()
exec()

Relvinian

petezl

I seem to remember it's ShellExecute. If you can't get it to work I'll  knock an example together when I et back into windows.
Peter.
Cats and women do as they please
Dogs and men should realise it.

thomasantony

Hi,
   Instead of ShellExecute you can also use the WinExec function: You only need to pas the command line of the program and display flags to it!!

Thomas Antony :U
There are 10 types of people in the world. Those who understand binary and those who don't.


Programmer's Directory. Submit for free

hutch--

You have 2 basic choices, run the app from the shell or run it from the OS, both have their advantages.

ShellExecute() will run files based on their extension using whateer is set up in the shell where running an app from the OS gives you more control. Either CreateProcess() or the older WinExec() that is mapped to the later one anyway.
Download site for MASM32      New MASM Forum
https://masm32.com          https://masm32.com/board/index.php

white scorpion

thanks, i believe it was ShellExecute... the problem with CreateProcess is that the process will die when my program stops, and that's just what i don't want :)

i think i will manage now since i know the name and now i can lookup the syntaxis via google..

thanks for your help all :D


hutch--

This is not true, CreateProcess starts an independent process in most instances and the calling program can exit without effecting it.
Download site for MASM32      New MASM Forum
https://masm32.com          https://masm32.com/board/index.php

pbrennick

As Hutch states, the created process will become an orphan when the parent closes and this is a pertinant statement from the API,
QuoteThe created process remains in the system until all threads within the process have terminated and all handles to the process and any of its threads have been closed through calls to CloseHandle.

It is the master of its own fate.  CreateProcess seems to be a point of confusion for a lot of people, I have noticed.  Actually, it is quite simple to use and is very reliable.

Paul

fallenhobbit

Im going to be ubergeekish here and mention that CreateProcess itself in Kernel32.dll is just mapped to NtCreateProcess in NTDLL.dll. So if you wanna really optimize your code, you could call into NTDLL.dll directly and save a few redirection calls.

Cheers,
Fallenhobbit

hutch--

This is always a dangerous route to take in that by not being documented, it may break under a later version and of course will not run on win9x.
Download site for MASM32      New MASM Forum
https://masm32.com          https://masm32.com/board/index.php

Vortex

Hi white scorpion,

Here is an example code acting like a SFX archive. The main module Extractor.exe extracts an embedded file named Dlgbox.exe
The attachment contains f0dder's Bin2o tool enabling the conversion of binary data to MS COFF object file.

.386
.model flat, stdcall
option casemap :none

include \masm32\include\windows.inc
include \masm32\include\kernel32.inc

includelib \masm32\lib\kernel32.lib

extern pData:BYTE

FILE_SIZE equ 2560

.data
szFilename db 'Dlgbox.exe',0

.data?
hFile dd ?
size1 dd ?

.code

start:

invoke CreateFile,ADDR szFilename,GENERIC_WRITE,\
0,0,CREATE_ALWAYS,FILE_ATTRIBUTE_ARCHIVE,0
mov hFile,eax
invoke WriteFile,hFile,ADDR pData,FILE_SIZE,ADDR size1,0
invoke CloseHandle,hFile
invoke WinExec,ADDR szFilename,SW_SHOW
invoke ExitProcess,0

END start

[attachment deleted by admin]

white scorpion

QuoteThis is not true, CreateProcess starts an independent process in most instances and the calling program can exit without effecting it.
i didn't knew this... i have used CreateProcess several times now, but i always invoked TerminateProcess after it, since i never needed to keep the process running. i think with all these options i surely will manage to solve my problem :D

btw, i have a book called "Windows NT/2000 - Native API reference", this book describes the use of NtCreateProcess, but for now i see no need in optimizing my code so far, so i'll just stick with CreateProcess :), thanks all for your help!

P1

CreateProcess is cleaner and more functional for starting a process the way you want it.   Cleaner by the fact, you specify the process to start.  And not knowing which program will run your file from the OS Shell.   

Regards,  P1  :8)

Relvinian

Quote from: fallenhobbit on January 29, 2005, 01:30:09 PM
Im going to be ubergeekish here and mention that CreateProcess itself in Kernel32.dll is just mapped to NtCreateProcess in NTDLL.dll. So if you wanna really optimize your code, you could call into NTDLL.dll directly and save a few redirection calls.

Cheers,
Fallenhobbit

As long as you would like to limit your application to NT4 kernel and higher.  The 9x kernels just have a "stub" NTDLL.DLL.

I have used the NTDLL.DLL calls directly for special situtions in an application. The most notable was to read and write registry keys that 99% of the registry program out there couldn't access. This is because the general registry program (including REGEDIT.EXE) can't handle \0 characters in their strings without specifying the end of the string. If you go down the the kernel level, you have more control but more dangerous and non-usuable between different Window OSes which may be available.

Relvinian

joe

Code from Vortex works fine. But how to run process without saving into disk? I know there are some  tasks:
1. create new allocated memory
2. copy program into new memory (with relocated section)
3. fix imports
4. & going to entry point new process
I had seen how do it CreateProcess (it's needed substitute only loading parts), but it's for long time to understand this function.