Shell to an executable but have it run from another directory

Posted on 2003-02-20
Medium Priority
Last Modified: 2010-05-01
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.
Question by:Zaar
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
LVL 18

Accepted Solution

bobbit31 earned 200 total points
ID: 7989862
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
LVL 18

Expert Comment

ID: 7989863

Expert Comment

ID: 7991417
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.

Author Comment

ID: 7997023
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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I’ve seen a number of people looking for examples of how to access web services from VB6.  I’ve been using a test harness I built in VB6 (using many resources I found online) that I use for small projects to work out how to communicate with web serv…
Introduction In a recent article (http://www.experts-exchange.com/A_7811-A-Better-Concatenate-Function.html) for the Excel community, I showed an improved version of the Excel Concatenate() function.  While writing that article I realized that no o…
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
Suggested Courses
Course of the Month9 days, 13 hours left to enroll

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question