WinAsm Studio, The Assembly IDE - Free Downloads, Source Code
Articles
Programming Quick Start
32-bit Assembler is Easy
Porting Iczelion tutorials
What is FASM
Hard Drive Recovery
Wiring your own LAN
Partners
Codegravity.com
Logix4u.net
Recover Data
custom t-shirts by Promopays.ca
Lost Birth Certificate
CF-Works.com
Easy Food Recipes
 
Forum
Pages (2) [1] 2   ( Go to first unread post )

ShellExecute does not work right, use CreateProcess, get handle of process & use WaitForSingleObject

Slider
Quote Post


Extremely Active Member
******

Group: Moderators
Posts: 179
Member No.: 5004
Joined: 11-May 07


I can't figure out why this isn't working.
If I run this code thru OllyDbg up to invoke IsWinNt, it runs the bat file.

Thanks.

Attached File ( Number of downloads: 62 )
 Login or Register to download
PMEmail Poster
Top
shoorick
Quote Post


Extremely Active Member
******

Group: Admins
Posts: 2057
Member No.: 160
Joined: 22-June 04


what are you expecting? btw, it will not wait until batch will finish. you have to use CreateProcess, not ShellExecute, then get handle of cmd process, running batch, and then wait until it will be closed (eg. WaitForSingleObject)
PMEmail PosterUsers Website
Top
Vortex
Quote Post


Extremely Active Member
******

Group: Moderators
Posts: 129
Member No.: 8
Joined: 13-May 04


Shoorick is right, WaitForSingleObject can do the job :

CODE

   .486                     ; create 32 bit code
   .model flat, stdcall     ; 32 bit memory model
   option casemap :none     ; case sensitive

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

   .code

;----------------------------------------------------------------------------;
; This is a translation of a shell script originally written by Edgar Hansen;
; in GoASM code. Translation by Paul E. Brennick  <[email protected]>  ;
;                                                                           ;
; Shell:      This function will run an executable and wait until it has    ;
;             finished before continuing. The function provides for a time  ;
;             out for a maximum wait period.                                ;
; Parameters: lpfilename = The fully qualified path to an executable to run ;
;             dwTimeOut = The amount of time in milliseconds to wait, -1 for;
;             no timeout                                                    ;
; Returns:    0 if successful, STATUS_TIMEOUT if the timeout has elapsed    ;
;             -1 if there was an error creating the process                 ;
;                                                                           ;
;----------------------------------------------------------------------------;
Shell PROC lpfilename:DWORD, dwTimeOut:DWORD
   LOCAL   Sh_st_info :STARTUPINFO
   LOCAL   Sh_pr_info :PROCESS_INFORMATION

   mov     DWORD PTR [Sh_st_info.cb], SIZEOF STARTUPINFO
   invoke  GetStartupInfoA, ADDR Sh_st_info
   mov     DWORD PTR [Sh_st_info.lpReserved], 0
   invoke  CreateProcessA, 0, [lpfilename], 0, 0, 0, 0, 0, 0, \
           ADDR Sh_st_info, ADDR Sh_pr_info
   test    eax, eax
   jz ERROR
   invoke  WaitForSingleObject, [Sh_pr_info.hProcess], [dwTimeOut]
   push    eax
   invoke  CloseHandle, [Sh_pr_info.hThread]
   invoke  CloseHandle, [Sh_pr_info.hProcess]
   pop     eax
   test    eax, eax
   RET
ERROR:
   xor     eax, eax
   sub     eax, 1
   RET
Shell ENDP

   end
PMEmail PosterUsers Website
Top
Slider
Quote Post


Extremely Active Member
******

Group: Moderators
Posts: 179
Member No.: 5004
Joined: 11-May 07


Thanks, I will study the code.

PMEmail Poster
Top
Slider
Quote Post


Extremely Active Member
******

Group: Moderators
Posts: 179
Member No.: 5004
Joined: 11-May 07


I can not figure out how to use Shell correctly.

Some things are confusing.

lpfilename name is declared as a DWORD when it really is a byte value.

Thanks.


CODE
Shell PROTO :DWORD, :DWORD

.data

lpfilename      DWORD   "E:\Bat\bak1.bat",0
dwTimeOut       DWORD   15000 ; Allow 15 seconds before timing out

.code

start:

main proc

invoke Shell,ADDR lpfilename, ADDR dwTimeOut
 
invoke  ExitProcess,NULL

main endp

;----------------------------------------------------------------------------;
; This is a translation of a shell script originally written by Edgar Hansen;
; in GoASM code. Translation by Paul E. Brennick  <[email protected]> ;
;                                                                          ;
; Shell:      This function will run an executable and wait until it has   ;
;             finished before continuing. The function provides for a time ;
;             out for a maximum wait period.                               ;
; Parameters: lpfilename = The fully qualified path to an executable to run;
;             dwTimeOut = The amount of time in milliseconds to wait, -1 for;
;             no timeout                                                   ;
; Returns:    0 if successful, STATUS_TIMEOUT if the timeout has elapsed   ;
;             -1 if there was an error creating the process                ;
;                                                                          ;
;----------------------------------------------------------------------------;
Shell PROC lpfilename:DWORD, dwTimeOut:DWORD

  LOCAL   Sh_st_info :STARTUPINFO
  LOCAL   Sh_pr_info :PROCESS_INFORMATION

  mov     DWORD PTR [Sh_st_info.cb], SIZEOF STARTUPINFO
  invoke  GetStartupInfoA, ADDR Sh_st_info
  mov     DWORD PTR [Sh_st_info.lpReserved], 0
  invoke  CreateProcessA, 0, [lpfilename], 0, 0, 0, 0, 0, 0, \
          ADDR Sh_st_info, ADDR Sh_pr_info
  test    eax, eax;ObjectToWaitOn
  jz ERROR
  invoke  WaitForSingleObject, [Sh_pr_info.hProcess], [dwTimeOut]
  push    eax
  invoke  CloseHandle, [Sh_pr_info.hThread]
  invoke  CloseHandle, [Sh_pr_info.hProcess]
  pop     eax
  test    eax, eax
  RET
ERROR:
  xor     eax, eax
  sub     eax, 1
  RET
Shell ENDP

end     start
PMEmail Poster
Top
1 User(s) are reading this topic (1 Guests and 0 Anonymous Users)
0 Members:

Topic Options Pages (2) [1] 2  Reply to this topicStart new topicStart Poll

 

Sponsors
Computer Science

Internet
C/C++
Hardware & PC maintenance

HiEditor

General Discussions
Suggestions/Bug Reports
WinAsm Studio

General Discussions
Suggestions/Bug Reports
WinAsm Studio FAQ
Multilingual User Interface
Add-Ins
Assembly Programming

Main
Newbies
Projects
Custom Controls
Snippets
Announcements & Rules

Announcements

General

Online Degrees - Distance Learning
The Heap
Russian