Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 154
  • Last Modified:

Shell to an executable but have it run from another directory

Hi All!
(first time posting so please forgive any procedure errors)

My question is this:
I am trying to shell out to an executable - mylog.exe - but the mylog.exe looks to the CURRENT running directory to write to a log file.  Since VB kicks this process off from the "C:\Program Files\Microsoft Visual Studio\VB98" and not the directory that the executable is under I would like to ask if there is a way to do this?  to let the 'running' directory be the directory from the executables location.
  • 2
1 Solution
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 Const SW_SHOWNORMAL = 1

public sub Command1_Click()
   Dim lRes as Long
   lRes = ShellExecute (0, "open", "<path to exe>", "<params, vbNullString if none>", "<***working directory***>", SW_SHWONORMAL)
end sub
You're running into the directory problem because you're running it under the development environment, when you create a .EXE file and run it, your path should be straightened out, because it's running your app and not VB6 interpreting your code.
ZaarAuthor Commented:
Thanks man!  That did the trick!  How cool is this site??? Post a question and it gets answered right away - AWESOME!

Anyway, thanks for the comment.  I called a friend also about an hour ago and he gave me a suggestion:

ChDir = App.Path

I tried both of the answers and found them both useful in particular circumstances.  If one does not want to change the entire app's working directory and single out just the .exe (or .bat or .cmd) then your code Bobbit31 is the way to go, if a more simple solution is needed and one does not mind changing the entire apps working dir to the .exe path then ChDir works.  I am just unaware of the drawbacks of ChDir yet.

Thanks for the help guys, I have just found a GREAT Q and A forum!!!!!


Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now