Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Obtaining program files dir

Status
Not open for further replies.

WilliamGS

Programmer
Jan 21, 2004
54
PE
Hello everybody. I need the default program files directory ("C:\Program Files" or "C:\Archivos de programa" in spanish Windows), there are funtions to obtain another system dirs like GetCurrentDirectory(), GetSystemDirectory(), GetWindowsDirectory(); is there a function to obtain the program files dir?

Thanks in advance,
William GS
 
I thought there was, but I didn't find one in MSDN.
Try the ExpandEnvironmentStrings() function and pass it the "%ProgramFiles%" environment variable. I only have an English machine, so I don't know if the Environment variable names are also localized?
 
Here's a function i once wrote to get it.
It tries the following:
1. like cpjust wrote, using the ProgramFiles environment variable. My machine is Dutch and it isn't localized.
2. if step 1 failed, call SHGetFolderA from shfolder.dll. Because this dll might not be present on some (older) systems, i call it dynamic to make sure the program runs even when the dll is not present. Note: if you are using unicode you should call SHGetFolderW instead of SHGetFolderA.
3. if step 2 also failed, find the windows directory, and replace everything after the last '\' with 'Program Files' and check if that is a valid directory.

Code:
#include <windows.h>

extern "C" BOOL GetProgramFilesDirectory ( char *Dir, DWORD cbDir )
{
  char * ProgramFiles    = "ProgramFiles";
  if ( GetEnvironmentVariable ( ProgramFiles, Dir, cbDir ))
     { return TRUE; }

  char * SHFolderDll     = "shfolder.dll";
  char * SHGetFolderPath = "SHGetFolderPathA";
  char   pf[MAX_PATH]    = "";

  HMODULE hm = LoadLibrary ( SHFolderDll );

  if ( hm != NULL )
     { BOOL fSuccess = FALSE;
       FARPROC fp = GetProcAddress ( hm, SHGetFolderPath );
       if ( fp != NULL )
          { char   *cpf = &pf[0];
            HRESULT hResult;
            DWORD StPtr;
            __asm { mov  StPtr, esp
                    push cpf
                    push 0
                    push 0
                    push 38
                    push 0
                    call fp
                    mov  hResult, eax
                    mov  esp, StPtr }
            if ( SUCCEEDED ( hResult ))
               { if ( (DWORD)(lstrlen ( pf )) < cbDir )
                    { fSuccess = TRUE;
                      lstrcpy ( Dir, pf ); } } }
       FreeLibrary ( hm );
       return fSuccess; }

  if ( !GetWindowsDirectory ( pf, sizeof ( pf )))
     { return FALSE; }

  int i1 = (int)(lstrlen(pf)) - 1;
  while ( i1 >= 0 && pf[i1] != '\\' )
     { i1--; }

  if ( i1 < 0 )
     { return FALSE; }

  lstrcpy ( &pf[i1], "\\Program Files" );

  DWORD Attr = GetFileAttributes ( pf );
  if ( Attr == 0xffffffff ||
       ( Attr & FILE_ATTRIBUTE_DIRECTORY ) != FILE_ATTRIBUTE_DIRECTORY )
     { return FALSE; }

  if ( (DWORD)(lstrlen ( pf )) < cbDir )
     { lstrcpy ( Dir, pf );
       return TRUE; }

  return FALSE; }

Marcel
 
Uhrm...

I've never actually done this before, but it seems that it's a terribly easy thing to do... As I've never done this specific thing before, I may be way off base, but it seems that you can query programfiles environment variable to get this info...

Code:
#include <cstdio>
#include <cstdlib>

int main()
{
	const char* env = getenv( "programfiles" );
	printf( env );
}

-----------------------------------------------
"The night sky over the planet Krikkit is the least interesting sight in the entire universe."
-Hitch Hiker's Guide To The Galaxy
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top