Renegade Projects Network Forums
Visual Basic questions - Printable Version

+- Renegade Projects Network Forums (https://forums.renegadeprojects.com)
+-- Forum: General Forums (https://forums.renegadeprojects.com/forumdisplay.php?fid=1)
+--- Forum: The Renegade Inn (https://forums.renegadeprojects.com/forumdisplay.php?fid=2)
+--- Thread: Visual Basic questions (/showthread.php?tid=1023)



Visual Basic questions - Professor_Tesla - 24.07.2008

Does anyone on here know Visual Basic 6? If so, I have a few questions:
0)How do I call another program in the same directory as the program doing the calling?
1)How do I call another program in a subdirectory of the directory of the program doing the calling?
2)How do I open a file in its default program if the file is in a subdirectory to the program doing the calling?

I tried looking on google, but it took me about an hour and a half to discover nothing.


RE: Visual Basic questions - DCoder - 24.07.2008

Marshall will probably comment in more detail, but the most universal way would be to use ShellExecute.


RE: Visual Basic questions - Marshall - 24.07.2008

I'll post some details when I get home from work. However not sure what question 2 is asking:
Are you asking, for example, how to "run a TXT file" such that it will open in Notepad (or whatever is the user's associated program)? Or something else?

EDIT:
You can call pretty much any program from your VB6 app regardless of where it is on your HD. The program you call might expect the current working directory to be set to it's own directory though, rather than the directory of your VB6 app, so you might have to set this using a separate API.

You can call programs in several different ways. Here's an example of how I might call oggdec.exe to convert an OGG file to a WAV:
Code:
dim sOggPath as string
dim process_id
dim process_handle
sOggPath = "C:\my_sound.ogg"
process_id = Shell(App.Path & "\Resource\oggdec.exe" & " " & sOggPath, vbNormal)
process_handle = OpenProcess(SYNCHRONIZE, 0, process_id)
If process_handle <> 0 Then
    WaitForSingleObject process_handle, INFINITE 'this waits until oggdec.exe has finished it's job
    CloseHandle process_handle
End If
App.Path is the directory your app's executable is in.
The above code calls three API functions that you need to declare:
Code:
Declare Function OpenProcess Lib "kernel32" (ByVal dwDesiredAccess As Long, ByVal bInheritHandle As Long, ByVal dwProcessId As Long) As Long
Declare Function WaitForSingleObject Lib "kernel32" (ByVal hHandle As Long, ByVal dwMilliseconds As Long) As Long
Declare Function CloseHandle Lib "kernel32" (ByVal hObject As Long) As Long
And there are two constants used that you need to declare:
Code:
Private Const SYNCHRONIZE = &H100000
Private Const INFINITE = -1&
If the program you are calling expects the current working directory to be set then you should search for the "SetCurrentDirectory" API function.


If you want to open, say, a text file with the user's default program do something like this:
Code:
If OpenLocation(sTextFilePath) < 32 Then Call frmMain.WriteLogEntry("Error opening " & sTextFilePath, LogMsgBox)
Or a URL with their default web browser:
Code:
If OpenLocation(sWebsite) < 32 Then Call MsgBox("Unable to open " & sWebsite, vbOKOnly + vbInformation, App.Title)
OpenLocation is a function I made (see: copied from someone else) that simplifies a call to an API function:
Code:
Public Function OpenLocation(ByVal WhichFilePath As String, Optional sParams As String = "", Optional sStartIn As String = vbNullString, Optional lngOpenMode As Long = 1) As Long
    OpenLocation = ShellExecute(0, "Open", WhichFilePath, sParams, sStartIn, lngOpenMode)
End Function
And ShellExecute is declared thusly:
Code:
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

There are other ways to do this but those are the ways I use the most.

You may need to play around with quotes and short/long filenames depending on what you're calling and where on the hard drive it is.

You can find most of the info you need on the web, you just gotta think how to structure your search. I learnt what I know 10% taught at school, 60% web searching and 30% trial and error.


RE: Visual Basic questions - Professor_Tesla - 25.07.2008

Vielen Dank.


RE: Visual Basic questions - DCoder - 25.07.2008

Do bear in mind that file paths with spaces need to be enclosed in double quotes to work properly.