Page 1 of 1

Find both Program Files paths on 64bit OS

Posted: Fri Dec 09, 2011 1:36 am
by IdeasVacuum
I made a little 32bit exe to collect the two program files paths on Win 64bit OS (Tested on Win7), 'Program Files' and 'Program Files (x86)'. Although many countries just use the US-English folder names, some very significant ones do not. Now, I thought this would work:

Code: Select all

Procedure.s GetSysFolder(iVal.i)
;-------------------------------

    #CSIDL_PROGRAM_FILES = $26           ;C:\Program Files
    #CSIDL_PROGRAM_FILESX86 = $2A        ;32bit C:\Program Files (x86) on 64bit

    Protected iFolderID.i, iSlashTotal.i, iSlashPosn.i, sSpecialFolderLocation.s

    If SHGetSpecialFolderLocation_(0, iVal, @iFolderID) = 0

             sSpecialFolderLocation = Space(#MAX_PATH)
             SHGetPathFromIDList_(iFolderID, @sSpecialFolderLocation)

             If sSpecialFolderLocation

                     If Right(sSpecialFolderLocation, 1) <> "\"

                              sSpecialFolderLocation + "\"

                     EndIf

                     ;Double backslashes:
                     iSlashTotal.i = CountString(sSpecialFolderLocation, "\")
                      iSlashPosn.i = 1

                     For i = 1 To iSlashTotal

                                         iSlashPosn = FindString(sSpecialFolderLocation,  "\", iSlashPosn)
                             sSpecialFolderLocation = InsertString(sSpecialFolderLocation, "\", iSlashPosn)
                                         iSlashPosn = iSlashPosn + 2 

                     Next

             EndIf

             CoTaskMemFree_(iFolderID)

    EndIf

       ProcedureReturn sSpecialFolderLocation

EndProcedure
....but it returns 'Program Files (x86)' in both cases. Perhaps because the exe itself is 32bit? It seems strange. The exe only queries #CSIDL_PROGRAM_FILESX86 if the OS has been verified as 64bit, so I thought that would be bullet proof......... :?

Re: Find both Program Files paths on 64bit OS

Posted: Fri Dec 09, 2011 12:07 pm
by Bisonte
try this :

Code: Select all

Debug GetEnvironmentVariable("ProgramW6432") 
or compile your app to x64.....

Re: Find both Program Files paths on 64bit OS

Posted: Fri Dec 09, 2011 12:42 pm
by luis
Unfortunately you can't use the CSIDL appropriate to the 64 bit path with a 32 bit process.

It's documented.

You can try to read what you want from the registry:

http://stackoverflow.com/questions/1958 ... irectory-o

Re: Find both Program Files paths on 64bit OS

Posted: Fri Dec 09, 2011 5:03 pm
by IdeasVacuum
...I'm going to try having both a 32bit and 64bit version of the checker. Grr Microsoft :mrgreen:

Re: Find both Program Files paths on 64bit OS

Posted: Fri Dec 09, 2011 6:22 pm
by luis
Anyway... if you change your mind and for anyone interested, this seem to work:

Code: Select all

CompilerIf Defined(KEY_WOW64_32KEY, #PB_Constant) = 0
 #KEY_WOW64_64KEY= $0100 ; Access a 64-bit key from either a 32-bit or 64-bit application.
 #KEY_WOW64_32KEY= $0200 ; Access a 32-bit key from either a 32-bit or 64-bit application.
CompilerEndIf

Procedure.s GetProgramFilesDir (Wow64Flag)
 Protected hReg
 Protected RetBuf$ = Space(255), RetSize = 255
 
 If RegOpenKeyEx_(#HKEY_LOCAL_MACHINE, "SOFTWARE\Microsoft\Windows\CurrentVersion", 0, #KEY_READ | Wow64Flag, @hReg) = #ERROR_SUCCESS
    If RegQueryValueEx_(hreg, "ProgramFilesDir", 0, 0, @RetBuf$, @RetSize) <> #ERROR_SUCCESS
        RetBuf$ = ""
    EndIf
    RegCloseKey_(hReg)        
 EndIf
  
 ProcedureReturn Trim(RetBuf$)
EndProcedure

Debug GetProgramFilesDir (#KEY_WOW64_32KEY) ; C:\Program Files (x86)
Debug GetProgramFilesDir (#KEY_WOW64_64KEY) ; C:\Program Files

Re: Find both Program Files paths on 64bit OS

Posted: Fri Dec 09, 2011 11:42 pm
by IdeasVacuum
That is very nice luis, thanks for sharing. I try to avoid looking-up registry values because it's not dependable, even with MS's own entries, but of course when needs must - I'm already using the following to verify OS bits:

Code: Select all

Procedure.l QueryValueEx(lhkey.i, szValueName.s)
;-----------------------------------------------
  Define.i cch, lrc, lType, lValue
  Define.s sValue
  vValue.s
  cch    = 255
  sValue = Space(255)
  lrc    = RegQueryValueEx_(lhkey, szValueName, 0, @lType, @sValue, @cch)
  If lrc = 0
    vValue = Left(sValue, cch - 1)
  Else
    vValue = "Empty"
  EndIf
  ProcedureReturn lrc

EndProcedure

Procedure.s OSbits()
;-------------------
     lRetVal.l = 0
 lHKeyhandle.l = 0
       lhkey.l = 0
     sOSbits.s = "32bit"
lTopLevelKey.l = #HKEY_LOCAL_MACHINE
sRemMachName.s = ""
    sKeyName.s = "Software\Microsoft\Windows\CurrentVersion"
  sValueName.s = "CommonW6432Dir"
       lRetVal = RegConnectRegistry_(sRemMachName, lTopLevelKey, @lHKeyhandle)
       lRetVal = RegOpenKeyEx_(lHKeyhandle, sKeyName, 0,#KEY_READ, @lhkey)
       lRetVal = QueryValueEx(lhkey, sValueName)

       RegCloseKey_(lhkey)

       If lRetVal = 0 : sOSbits = "64bit" : EndIf

     ProcedureReturn sOSbits

EndProcedure
According to MSDN, 'Program Files' on an Italian PC is 'Programmi'. Is that true on your PC? I ask because they also say that a German PC will have 'Programme' but a number of my German friends see the US-English 'Program Files' on their machines.

Re: Find both Program Files paths on 64bit OS

Posted: Sat Dec 10, 2011 12:00 am
by luis
It's "Programmi" looking around with Explorer, but it's actually "Program Files" on disk.

With Win7 (with Vista I believe actually) this is changed. It was time.

The code I posted return the "English" path on an Italian installation.


Using: Dir /al

Code: Select all

 Volume in drive C is WIN_7          
 Directory of  C:\*

14/07/2009  06:08    <JUNCTION>    Documents and Settings [C:\Users]
13/02/2011  16:38    <JUNCTION>    Programmi [C:\Program Files]
              0 bytes in 0 files and 2 dirs
178.126.536.704 bytes free

Re: Find both Program Files paths on 64bit OS

Posted: Sat Dec 10, 2011 12:07 am
by IdeasVacuum
aha! Another generous donation by MS towards my cultivation of grey hair........ :mrgreen:

Re: Find both Program Files paths on 64bit OS

Posted: Sat Dec 10, 2011 12:12 am
by luis
A nice link posted by someone else some days ago....

http://www.svrops.com/svrops/articles/jpoints.htm