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

x
  • 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.
0
Zaar
Asked:
Zaar
  • 2
1 Solution
 
bobbit31Commented:
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
0
 
bobbit31Commented:
oops, SW_SHWONORMAL should be, SW_SHOWNORMAL
0
 
FRoeCassNetCommented:
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.
0
 
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!!!!!

Regards,
Jason
0

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