Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Visual Basic questions
#1
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.
"The present is theirs. The future, for which I really worked, is mine."
- Nikola Tesla

"My - y - my - your - my vision has permutated. My - y - my - your - my plans have followed a path unpredicted by the union of Nod and GDI. Your - my - our - our directives must be reassessed." - Kane/CABAL

[Image: 9853.png]
Reply
#2
Marshall will probably comment in more detail, but the most universal way would be to use ShellExecute.

Worth playing: 1 | 2 | 3
Reply
#3
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.
Ever wondered what the hell is going on?
Believe me friend you're not the only one.
--Lysdexia

Check out Launch Base for RA2/YR - http://marshall.strategy-x.com
Also home to the Purple Alert mod, 1.002 UMP, and the YR Playlist Manager.
Reply
#4
Vielen Dank.
"The present is theirs. The future, for which I really worked, is mine."
- Nikola Tesla

"My - y - my - your - my vision has permutated. My - y - my - your - my plans have followed a path unpredicted by the union of Nod and GDI. Your - my - our - our directives must be reassessed." - Kane/CABAL

[Image: 9853.png]
Reply
#5
Do bear in mind that file paths with spaces need to be enclosed in double quotes to work properly.

Worth playing: 1 | 2 | 3
Reply




Users browsing this thread: 1 Guest(s)