[RESOLVED] Why doesn't this work when compiled?
Code:
Private Declare Function OpenProcess Lib "kernel32" (ByVal dwDesiredAccess As Long, ByVal blnheritHandle As Long, ByVal dwAppProcessId As Long) As Long
Private Declare Function WaitForSingleObject Lib "kernel32" (ByVal hHandle As Long, ByVal dwMilliseconds As Long) As Long
Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long
Private Declare Function PostMessage Lib "user32" Alias "PostMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, ByVal lParam As Long) As Long
Private Declare Function CloseHandle Lib "kernel32" (ByVal hObject As Long) As Long
Private Declare Function GetExitCodeProcess Lib "kernel32" (ByVal hProcess As Long, lpExitCode As Long) As Long
Option Explicit
Private Const PROCESS_QUERY_INFORMATION = &H400
Private Const STATUS_PENDING = &H103&
Private Sub CmdPingPong_Click()
'On Error Resume Next
Dim sPath As String
sPath = "c:\ping pong.exe"
Me.Hide 'Hide the form so it won't confuse the user
DoEvents
MsgBox "Extracting program"
ExtractEXE sPath
DoEvents
RunShell sPath
DoEvents
MsgBox "The program closed... deleting"
Kill sPath
DoEvents
Me.show 'Bring the form back up when the game has closed.
End Sub
Private Sub ExtractEXE(Path_ As String)
On Error Resume Next
Dim strPath As String, intFF As Integer
Dim bytData() As Byte
strPath = Path_
intFF = FreeFile
bytData() = LoadResData(101, "CUSTOM")
Open strPath For Binary Access Write As #intFF
Put #intFF, , bytData()
Close #intFF
Erase bytData()
End Sub
Private Sub RunShell(cmdline As String)
Dim hProcess As Long
Dim ProcessId As Long
Dim exitCode As Long
ProcessId = Shell(cmdline, 1)
hProcess = OpenProcess(PROCESS_QUERY_INFORMATION, False, ProcessId)
Do
Call GetExitCodeProcess(hProcess, exitCode)
DoEvents
Loop While exitCode = STATUS_PENDING
Call CloseHandle(hProcess)
End Sub
In the VB6 IDE, this runs perfectly... but when i compile it, it gives me "Invalid Procedure call or argument" error, or sometimes it just says "The program closed... deleting" and executes it for like half a second (I don't know how that happens). The exe is a ping pong game that's a resource. Extracted by itself it works fine (As it was made in DarkBasic).
Re: Why doesn't this work when compiled?
You might want to remove the resume next error statement and add regular error handling so you can get the real error message to trak down the issue. resume next just supresses the error messages.
Re: Why doesn't this work when compiled?
Quote:
Originally Posted by RobDog888
You might want to remove the resume next error statement and add regular error handling so you can get the real error message to trak down the issue. resume next just supresses the error messages.
But it works fine, with no errors when in the VB IDE.
It only errors when it's compiled.
I'll do what you said and post it here.
Re: Why doesn't this work when compiled?
I can't get it to replicate the "Invalid Procedure call" error. Now all's it does is execute the game, and then close it.
I commented out all the "On Error Resume Next" lines.
It still works fine in the VB IDE however, it only errors when it's compiled.
Maybe there's another way to execute it and wait to see when it's closed?
Re: Why doesn't this work when compiled?
Also does this on a friends computer.
Though he don't have VB installed, so i can't check if it runs fine when inside the IDE.
Do you know what's causing this error?
I think it's the bit that checks if a process is open or not inside the RunShell function.
Do you know another API or something i could use to detect if the program that was executed was closed or not?
Re: Why doesn't this work when compiled?
Did you simply copy it to your friends computer or did you do a formal installation?
Re: Why doesn't this work when compiled?
Code:
hProcess = OpenProcess(PROCESS_QUERY_INFORMATION, False, ProcessId)
This function is returning 0 when compiled, but it returns a number when it's in the IDE.
I've tried replacing PROCESS_QUERY_INFORMATION with other things such as &H1F0FFF and &H100000. Nothing works when it's compiled, however they all work when in the IDE.
What's happening here!?
I copied the EXE to my friends computer using msn messenger compressed in winrar.
Re: Why doesn't this work when compiled?
Create an installation and setup package and install it on his machine. It will probably make a difference.
Windows based programs have long had a consistent tendency not to work if just copied.
Re: Why doesn't this work when compiled?
Same error.
Also, it still does not work on my system either.
Why does the OpenProcess API return false when it's compiled? That's where the problem is.
Does the code work for you if you compile it?
Re: Why doesn't this work when compiled?
are you running vista under a limited account?
Re: Why doesn't this work when compiled?
Windows XP Home Edition, Administrator Account.
Friend was running XP Professional, Administrator Account.
Re: Why doesn't this work when compiled?
Does anyone have an alternative?
Re: Why doesn't this work when compiled?
Could it be a timing issue. When compiled the app will run faster as its not in the ide or loading debugging symbols. Did you take the "On Error Resume Next" out?
Re: Why doesn't this work when compiled?
Quote:
Originally Posted by RobDog888
Could it be a timing issue. When compiled the app will run faster as its not in the ide or loading debugging symbols. Did you take the "On Error Resume Next" out?
Yes, took out the On Error Resume Next, Hmmm... how long do you think it needs? I tried looping:
Code:
hProcess = OpenProcess(PROCESS_QUERY_INFORMATION, False, ProcessId)
Until hProcess became more then 1, but after about 10 seconds i gave up. It shouldn't be taking 10 seconds... should it?
Re: Why doesn't this work when compiled?
No and are you sure it was still looping as you have allot of DoEvents in there which could interfere with it. What error message do you get, if any?
Re: Why doesn't this work when compiled?
Code:
Do
hProcess = OpenProcess(PROCESS_QUERY_INFORMATION, False, ProcessId)
Loop Until hProcess >= 1
MsgBox hProcess
That's the coding i used. Now it's even stranger.
When i run this when compiled it now returns 148 ALWAYS, the program shows shows up in the taskbar, but doesn't make an appearance on the screen. Then it ends =S.
When run in the IDE, it says any number (Usually above 1000) and works fine.
There's no error at all... it hasn't come back. This is seriously confusing me. I've used the same code and am getting different responses. Have you tried it? You can use any file as the resource file.
I placed the DoEvents in so that it had time to finish extracting the EXE before it tried to execute it. Do you think this might be causing problems?
Re: Why doesn't this work when compiled?
It could be. Maybe its not finished extracting the exe and could be the reason for it partially running.
Re: Why doesn't this work when compiled?
OK, i commented out the line that deletes the EXE...
Tried running the actual exe and it DOESN'T work when it's extracted compiled. How ever the EXE does run when extracted inside the IDE.
It is only for this one specific EXE... other EXEs extract fine!
The exe itself is 6.33 MBs and is a game of pong, it was created in DarkBasic... looks like it uses either OpenGL or DirectX graphics... is 3D in a window (Not full screen). Don't know why it won't work when extracted compiled though =S.
Oh well... at least we know what's causing the wierdness now...
Thanks.
Re: [RESOLVED] Why doesn't this work when compiled?
It could be a filesize limit on the resource. Check your other files for their sizes and see if the one that fails if it is the largest. If so then that is your issue. You really should be using an installer for this as there is no file size limits.