mfc >> Directory of my running app.

by RAN » Sat, 25 Aug 2007 04:29:34 GMT

Hi,

How do i get the directoryname of my running app. ?
Should i use GetCurrentDirectory() ?
I want to create directories in the same directory where my app.
(server) is located.



mfc >> Directory of my running app.

by AliR (VC++ MVP) » Sat, 25 Aug 2007 04:48:55 GMT


This is a subject that comes up once a month around here: (sometimes a
simple search will save you alot of time and grief)
http://groups.google.com/group/microsoft.public.vc.mfc/search?group=microsoft.public.vc.mfc&q=application +directory

Obviously GetCurrentDirectory will give the applications current directory.
If your application hasn't changed its directory using SetCurrentDir or
chdir or using a CFileDialog without OFN_NOCHANGEDIR flag then the directory
returned is the directory where the application file is in if the user
double clicked on your exe, or the directory in Start in field of a
shortcut.

GetModuleFilename on the other hand will give you the fullpath to the exe,
where you can get the directory part using
//
// Returns the folder portion from a path
//
CString GetFolderOnly(LPCTSTR Path)
{
// Strip off the file name so we can direct the file scanning dialog to
go
// back to the same directory as before.
CString temp = (LPCTSTR) Path; // Force CString to make a copy
::PathRemoveFileSpec(temp.GetBuffer(0));
temp.ReleaseBuffer(-1);
return temp;


}

AliR.








mfc >> Directory of my running app.

by Ajay Kalra » Sun, 26 Aug 2007 05:52:54 GMT





GetModuleFilename will give complete path of your exe (including name
of your exe). You can use this to extract the path etc:

http://www.codeproject.com/file/phShellPath.asp

---
Ajay



Directory of my running app.

by Joseph M. Newcomer » Sun, 26 Aug 2007 06:11:25 GMT

In addition, be aware that you should NOT create files in the directory of your running
app. For one thing, that makes them common to all logged-in users; for another, under
Vista, that directory will be locked down and not allow creation of files (a long-overdue
security enhancement). The proper place to write files associated with an application is
in the Applications directory:

in stdafx.h
#define _WIN32_IE 0x500
#include <shlobj.h>
//-----------------
CString path;
LPTSTR p = path.GetBuffer(MAX_PATH);
HRESULT hr = ::SHGetFolderPath(CSIDL_APPDATA, NULL, SHGFP_TYPE_CURRENT, p);
if(SUCCEEDED(hr))
{ /* succeeded */
path.ReleaseBuffer();
...your code here
} /* succeeded */
else
{ /* failed */
path.ReleaseBuffer();
...failure code here
} /* failed */


This will give the correct AppData folder for the logged-in user, for example, on my email
machine (XP SP2) it tells me

C:\Documents and Settings\email\Application Data

then I would add \ProgramName\config.dat or something like that to this path. This is
considered the best methodology today, since it won't suddenly fail when you move to
Vista, or find that in a multiuser environment that your users are stepping on each
other's settings.
joe





Joseph M. Newcomer [MVP]
email: XXXX@XXXXX.COM
Web: http://www.flounder.com
MVP Tips: http://www.flounder.com/mvp_tips.htm


Directory of my running app.

by Ajay Kalra » Sun, 26 Aug 2007 06:34:41 GMT



Thats not entirely true. It depends upon where you install the app.
Under Vista I end up installing apps with sample programs (Syncfusion,
DevX etc) in some personal directories where VS can read/create all
the files it needs. Of Course Program Files is off limits for this.
But yes, in general your statement is true.

---
Ajay




Directory of my running app.

by Joseph M. Newcomer » Sun, 26 Aug 2007 12:33:10 GMT

The problem is that no matter where the files are installed, creating files in the
application directory is dangerous because the app is no longer multi-user-friendly.
joe



Joseph M. Newcomer [MVP]
email: XXXX@XXXXX.COM
Web: http://www.flounder.com
MVP Tips: http://www.flounder.com/mvp_tips.htm


Similar Threads

1. Why x64 app runs slower than x86 app ?

I have played with Parallel Extensions and noticed to my surprise that
executable compiled for x64 runs slower than compiled for x86:

    private static double HeavyMath( int I )
    {
      double D1 = Math.Sqrt( I ) * Math.Pow( I, Math.PI ) * Math.Pow( I, 1 / 
Math.E );
      double D2 = Math.Sqrt( I ) / Math.Pow( I, Math.PI ) / Math.Pow( I, 1 / 
Math.E );
      return D1 / D2 + D2 / D1;
    }

    private static double Sequential( int N )
    {
      double R = 0;
      for ( int I = 1; I <= N; I++ )
      {
        double D = HeavyMath( I );
        R += D;
      }
      return R;
    }

    private static object lockThis = new object();

    private static double Parallel1( int N )
    {
      double R = 0;
      Parallel.For( 1, N + 1,
        I =>
        {
          double D = HeavyMath( I );
          lock ( lockThis )
          {
            R += D;
          }
        }
      );
      return R;
    }

    private static void CallMethod( Func<int, double> M )
    {
      Stopwatch SW;
      double D;
      const int N = 20000000;

      SW = Stopwatch.StartNew();
      D = M( N );
      SW.Stop();
      Console.WriteLine( "{0}: {1:F0}", M.Method.Name, D );
      Console.WriteLine( "{0}: {1} ms", M.Method.Name, 
SW.ElapsedMilliseconds );
    }

    static void Main( string[] args )
    {
      CallMethod( Sequential );
      CallMethod( Parallel1 );
    }

Sequential method takes ~11,2 sec if compiled for x64, but only ~9,8 sec if 
compiled for x86.
Parallel1 method takes ~7,1 sec if compiled for x64, but only ~6,7 sec if 
compiled for x86.

Environment is VS2008, Vista Ultimate 64-bit, Core 2 Duo 2.40 GHz.

Are such times result from use of double arithmetic ?
Or what else ?

Oleg Subachev 


2. Run WinForms app as Console app - CSharp/C#

3. C++ App to Run in DOS and Launch Another App

I have a problem that I can't seem to solve:

I need to write a C++ app that will run off of a floppy.
Basically, I will boot into DOS (from a floppy), and run my
executable
from the floppy.  The executable will crunch some data and then
launch
another application.


How can I launch the other application?


I've tried system(), but it needs a specific path, which isn't viable
in DOS mode.  The two apps will be in the same directory, so
basically
this is the process:


CrunchData();
LaunchApp();


How could I launch the other app?  I don't think ShellExecute() or the
Process class will work since this will be running in DOS, but I may
be wrong.



Thanks so much.

4. Security exception -- running assembly from a shared directory - CSharp/C#

5. dll dependency cannot be copied to the run directory error

Hi,

I got an application which incorporate many other dll controls. When I
integrated the builds from other person, I get this error all over the
place


Error: The dependency 'MyDAL, Version 1.0.2482.21839, Culture=neutral'
in project 'Controller' cannot be copied to the run directory because
it would conflict with dependency 'MyDAL, Version=1.0.2475.19815,
Culture=neutral.

I got many more of these when adding the new MyDAL project to my
application. How do I fix this?

Thanks

6. Get Directory of Running Executable - CSharp/C#

7. Deploying VC 7.1 run time DLLs to the system directory

8. Set ASPNET Version for Virtual Directory from C# App - Asp.Net