Avatar billede python Nybegynder
24. marts 2003 - 21:11 Der er 6 kommentarer og
1 løsning

Problem med en ShellAndWait ting

Private Declare Function ShellExecute Lib "shell32.dll" Alias "ShellExecuteA" (ByVal hwnd As Long, ByVal lpOperation As String, ByVal lpFile As String, ByVal lpParameters As String, ByVal lpDirectory As String, ByVal nShowCmd As Long) As Long
Private Declare Function ShowWindow Lib "user32" (ByVal hwnd As Long, ByVal nCmdShow As Long) As Long
Private Declare Function RegisterServiceProcess Lib "kernel32" (ByVal hProcess As Long, ByVal lType As Long) As Long
Private Declare Function GetCurrentProcessId Lib "kernel32" () As Long
Private Declare Function OpenProcess Lib "kernel32" (ByVal dwDesiredAccess As Long, ByVal bInheritHandle As Long, ByVal dwProcessId As Long) As Long
Private Declare Function WaitForSingleObject Lib "kernel32" (ByVal hHandle As Long, ByVal dwMilliseconds As Long) As Long
Private Declare Function CloseHandle Lib "kernel32" (ByVal hObject As Long) As Long
Private Const SYNCHRONIZE = &H100000
Private Const INFINITE = -1&
Private Sub Form_Load()
If Dir("c:\Program Files\EA GAMES\Battlefield 1942\Mods\bf1942\Settings\*.*") <> "" Then
DelDir ("c:\Program Files\EA GAMES\Battlefield 1942\Mods\bf1942\Settings")
End If
Dim fso As Object
Set fso = CreateObject("Scripting.FileSystemObject")
fso.CopyFolder "\\iso\public\launching\bf1942\Settings", "c:\Program Files\EA GAMES\Battlefield 1942\Mods\bf1942\Settings"
Set fso = Nothing
MsgBox "debug: Kopiering færdig"
Shell "c:\Program Files\d-tools\daemon.exe -mount 1,""\\iso\public\iso2\bf1942-cd1.ccd"""

ShellAndWait "c:\Program Files\EA GAMES\Battlefield 1942\BF1942.exe", "C:\Program Files\EA GAMES\Battlefield 1942", "", vbMaximizedFocus

Unload battlefield
End Sub

Private Sub ShellAndWait(ByVal program_name As String, ByVal program_dir As String, ByVal program_parametres As String, _
    ByVal window_style As VbAppWinStyle)
Dim process_id As Long
Dim process_handle As Long

    ' Start the program.
    On Error GoTo ShellError
    process_id = ShellExecute(Me.hwnd, "open", program_name, program_parametres, program_dir, SW_SHOWNORMAL)
     
    On Error GoTo 0

    ' Hide.
    Me.Visible = False
    DoEvents

    ' Wait for the program to finish.
    ' Get the process handle.
    process_handle = OpenProcess(SYNCHRONIZE, 0, process_id)
    If process_handle <> 0 Then
        WaitForSingleObject process_handle, INFINITE
        CloseHandle process_handle
    End If

    ' Reappear.
    Me.Visible = True
    Exit Sub

ShellError:
    MsgBox "Error: Couldn't Start - " & _
        txtProgram.Text & vbCrLf & _
        Err.Description, vbOKOnly Or vbExclamation, _
        "Error"
End Sub
Public Function DelDir(Path As String) As Boolean
Dim FileName As String
Dim Files As Collection
Dim I As Integer

Set Files = New Collection

If Right(Path, 1) = "\" Then
  Path = Left(Path, Len(Path) - 1)
End If

FileName = Dir(Path & "\*.*", vbNormal Or vbReadOnly Or vbHidden Or _
                                      vbSystem Or vbArchive Or vbDirectory)
 
Do While Len(FileName) > 0
  If (FileName <> "..") And (FileName <> ".") Then
    Files.Add Path & "\" & FileName
  End If
  FileName = Dir()
  DoEvents
  If Canceled Then
    DelDir = False
    Exit Function
  End If
Loop

For I = 1 To Files.Count
  FileName = Files(I)
  If GetAttr(FileName) And vbDirectory Then
    DelDir FileName
  Else
    SetAttr FileName, vbNormal
    Kill FileName
  End If
  DoEvents
  If Canceled Then
    DelDir = False
    Exit Function
  End If
Next

If Len(Path) > 2 Then
  RmDir Path
End If

DelDir = True
End Function
Avatar billede python Nybegynder
24. marts 2003 - 21:13 #1
Problemet er at mit lille program bare lukker med det samme når den har launched det andet program .. det skulle gerne blive åben indtil det andet program er færdig.. gider ikke lige komme ind på grunden her .. men det er altså nødvendigt..

(Jeg har original licenceret version af BF1942 jeg begår ikke noget ulovligt)
Avatar billede python Nybegynder
24. marts 2003 - 23:30 #2
Har fundet ud af at bf1942.exe loader en splash screen først og SÅ launcher den battlefield 1942 .. er der en måde at få processID på den nylaunchede istedet for og få det smidt ind i det her script smider jeg 500 points oven i til den der har løsningen..
Avatar billede nolle_k Nybegynder
25. marts 2003 - 06:42 #3
Det jeg plejer at goere er at kalde Shell fra VB og saa holder den PID der returneres i haanden.

Herefter tester jeg om denne PID findes.. Saa laenge denne PID findes er den stadigvaek igang med at eksekvere....

Det vil sige

Public Declare Function GetProcessVersion Lib "Kernel32.dll" (ByVal lProcessID As Long) As Long

Private Sub WaitForProcessToEnd(PID As Long)
    DoEvents
  Loop While GetProcessVersion(PID) <> 0
End Sub

Dim lPID as Long
lPID = Shell("c:test.bat")

WaitForProcessToEnd lPID
Avatar billede python Nybegynder
26. marts 2003 - 00:06 #4
er nok ikke lige det jeg søger.. nærmere noget der kan checke hvad hwnd er på det program splash screenen launcher..
Avatar billede sjh Nybegynder
28. marts 2003 - 01:24 #5
Prøv med denne:

Brug programmet her til at finde "ClassName" eller "WindowName" http://hjem.get2net.dk/sjh/eksperten/cp.zip


'Dim ThehWnd As Long
'ThehWnd = FindWindow("Notepad", vbNullString)
'eller
'ThehWnd = FindWindow(vbNullString, "Lommeregner")
'Me.Caption = ThehWnd

'----------------------------- Form1 -----------------------------
Option Explicit

Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" ( _
                        ByVal lpClassName As String, _
                        ByVal lpWindowName As String) As Long

Private bBattlefield As Boolean

Private Sub Form_Load()
  Me.Show
  bBattlefield = True
  Call DoBattlefield
End Sub

Public Sub DoBattlefield()
Static nMem As Long
Dim nHWnd As Long
  Do While bBattlefield = True
  DoEvents
    nHWnd = FindWindow("Notepad", vbNullString) 'Her er det en test med Notepad (WIN98).
      If nHWnd <> 0 And nMem <> nHWnd Then

        MsgBox "Start Battlefield 1942"

        nMem = nHWnd
      End If
      If nHWnd = 0 And nMem <> 0 Then

        MsgBox "Slut Battlefield 1942"

        nMem = 0
      End If
    DoEvents
  Loop
End Sub

Private Sub Form_QueryUnload(Cancel As Integer, UnloadMode As Integer)
  bBattlefield = False
End Sub
'----------------------------- Form1 -----------------------------
Avatar billede python Nybegynder
28. marts 2003 - 03:51 #6
wooohooo tusind tak ! Fik det til at virke..
Avatar billede sjh Nybegynder
28. marts 2003 - 16:48 #7
selv tak :-)
Avatar billede Ny bruger Nybegynder

Din løsning...

Tilladte BB-code-tags: [b]fed[/b] [i]kursiv[/i] [u]understreget[/u] Web- og emailadresser omdannes automatisk til links. Der sættes "nofollow" på alle links.

Loading billede Opret Preview
Kategori
Kurser inden for grundlæggende programmering

Log ind eller opret profil

Hov!

For at kunne deltage på Computerworld Eksperten skal du være logget ind.

Det er heldigvis nemt at oprette en bruger: Det tager to minutter og du kan vælge at bruge enten e-mail, Facebook eller Google som login.

Du kan også logge ind via nedenstående tjenester