INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Jobs

API Functions

Read the Windows Registry by jimoo
Posted: 6 Jan 04

Here is the API code to read the Windows Registry.  You will want to do this is you use 3rd party OCX or DLL files, or if you create your own DLLs using VFP.

By default, a VFP DLL is registered on the computer when it is created on.  However, if you need to distribute the application it must be registered on the other computers.  One way to handle this is by reading the registry, and register it using REGSRV32 if it does not exist.


Example of registering an OCX or DLL
RUN /n regsvr32 myDLL.DLL /S


Code to read the registry.  In this example I am checking for the ôProduct IDö of Visual Foxpro 7.0.  You can change the version in the program and try it for other version.

* To test this code:  copy from here on down and paste into a program.  Run it.


*-- Registry Read by Jim Osieczonek û www.deltabg.com
LOCAL lcVersion
lcVersion = "7.0"

PUBLIC goApp
goApp = CREATEOBJECT('capp')
MESSAGEBOX(goApp.ReadReg('HLM','Software\Microsoft\VisualFoxpro\' + lcVersion +;
    '\Registration','ProductID'))

**************************************************
*
DEFINE CLASS capp AS container

    *-- Reads the windows Registry.
    PROCEDURE readreg
        * Start of Code
        LPARAMETERS tcKey,tcSubKey,tcValue
        IF PCOUNT() = 2
            tcValue = ""
        ENDIF

        * setup environment

        LOCAL nKey, cSubKey, cValue,  cValueRead

        #DEFINE HKEY_USERS                  -2147483645
        #DEFINE HKEY_LOCAL_MACHINE          -2147483646
        #DEFINE HKEY_CURRENT_USER           -2147483647
        #DEFINE HKEY_CLASSES_ROOT            -2147483648

        DO CASE
        CASE m.tcKey == "HCR"
            nKey = HKEY_CLASSES_ROOT
        CASE m.tcKey == "HLM"
            nKey = HKEY_LOCAL_MACHINE
        CASE m.tcKey = "HCU"
            nKey = HKEY_CURRENT_USER
        CASE m.tcKey = "HCR"
            nKey = HKEY_CLASSES_ROOT
        OTHERWISE
            nKey = m.tcKey
        ENDCASE

        cSubKey = m.tcSubKey
        cValue  = m.tcValue

        * example 1
        * nKey = HKEY_LOCAL_MACHINE
        * cSubKey = "Software\VfpRegTest"
        * cValue = "TestREG_SZ"

        * example 2
        * nKey = HKEY_CLASSES_ROOT
        * cSubKey = "Spin.SpinButton\CLSID"
        * cValue = ""


        * here is where we will actually read the registry
        cValueRead = THIS.ReadREG_SZ(nKey, cSubKey, cValue)

        IF (EMPTY(cValueRead)) THEN
            cValueRead = "REGISTRY KEY NOT FOUND"
           * MESSAGEBOX("Function Not Successful.")  && testing only
        ELSE
           * MESSAGEBOX("Function Successful.    " + cValueRead)  && testing only
        ENDIF

        RETURN cValueRead
    ENDPROC


    *-- API's used by the ReadReg method.
    PROCEDURE readreg_sz
        * This function reads a REG_SZ value from the registry. If successful,
        * it will return the value read. If not successful, it will return an empty string.

        PARAMETERS  nKey, cSubKey, cValue
        * nKey The root key to open. It can be any of the constants defined below.
        *  #DEFINE HKEY_CLASSES_ROOT           -2147483648
        *  #DEFINE HKEY_CURRENT_USER           -2147483647
        *  #DEFINE HKEY_LOCAL_MACHINE          -2147483646
        *  #DEFINE HKEY_USERS                  -2147483645
        * cSubKey The SubKey to open.
        * cValue The value that is going to be read.

        * Constants that are needed for Registry functions
        #DEFINE REG_SZ   1

        * WIN 32 API functions that are used
        DECLARE Integer RegOpenKey IN Win32API ;
            Integer nHKey, String @cSubKey, Integer @nResult
        DECLARE Integer RegQueryValueEx IN Win32API ;
            Integer nHKey, String lpszValueName, Integer dwReserved,;
            Integer @lpdwType, String @lpbData, Integer @lpcbData
        DECLARE Integer RegCloseKey IN Win32API Integer nHKey

        * Local variables used
        LOCAL nErrCode      && Error Code returned from Registry functions
        LOCAL nKeyHandle    && Handle to Key that is opened in the Registry
        LOCAL lpdwValueType && Type of Value that we are looking for
        LOCAL lpbValue      && The data stored in the value
        LOCAL lpcbValueSize && Size of the variable
        LOCAL lpdwReserved  && Reserved Must be 0

        * Initialize the variables
        nKeyHandle = 0
        lpdwReserved = 0           
        lpdwValueType = REG_SZ
        lpbValue = ""
           
        nErrCode = RegOpenKey(nKey, cSubKey, @nKeyHandle)
        * If the error code isn't 0, then the key doesn't exist or can't be opened.
        IF (nErrCode # 0) THEN
            RETURN ""
        ENDIF

        lpcbValueSize = 1
        * Get the size of the data in the value
        nErrCode=RegQueryValueEx(nKeyHandle, cValue, lpdwReserved, @lpdwValueType, @lpbValue, @lpcbValueSize)

        * Make the buffer big enough
        lpbValue = SPACE(lpcbValueSize)   
        nErrCode=RegQueryValueEx(nKeyHandle, cValue, lpdwReserved, @lpdwValueType, @lpbValue, @lpcbValueSize)
           
        =RegCloseKey(nKeyHandle)
        IF (nErrCode # 0) THEN
            RETURN ""
        ENDIF

        lpbValue = LEFT(lpbValue, lpcbValueSize - 1)
        RETURN lpbValue
    ENDPROC


ENDDEFINE
*
*-- EndDefine: capp
**************************************************
 


Back to Microsoft: Visual FoxPro FAQ Index
Back to Microsoft: Visual FoxPro Forum

My Archive

Resources

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close