Announcement

Collapse
No announcement yet.

Reading the registry REDUX

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • Reading the registry REDUX

    I'm looking for a better example of how to use the PB registry functions posted here: http://www.powerbasic.com/support/pb...ad.php?t=24777

    There are some good examples in there, and they all work. But when I try to read my own registry values, I get nothing. Below is my code that's having a problem. Instead of "(Default)", I've tried "REG_SZ" and "Value REG_SZ" and others. I'm trying to recover: "C:\PBWin80\Bin\PBWin.exe" %1

    Code:
    RegDataGet(%HKEY_CLASSES_ROOT, _
       "Applications\PBWin.exe\shell\open\command", _
       "(Default)", sBuffer, dwRegType)
    Thanks,
    Christopher
    Last edited by Christopher Becker; 24 Aug 2009, 10:44 AM. Reason: url
    Christopher P. Becker
    signal engineer in the defense industry
    Abu Dhabi, United Arab Emirates

  • #2
    What is the function returning? If it's not returning zero, then the call failed and the return value is an error code you can interrogate.

    i.e., change your code to
    Code:
    retval = RegDataGet(%HKEY_CLASSES_ROOT, _
       "Applications\PBWin.exe\shell\open\command", _
       "(Default)", sBuffer, dwRegType)
    
    IF retVal = %ERROR_SUCCESS THEN 
       you expect something in sBuffer
    ELSE
      you don't and retval contains an error code 
      for which you can report a text message 
      using FormatMessage()
    END IF
    ...
    Michael Mattias
    Tal Systems (retired)
    Port Washington WI USA
    [email protected]
    http://www.talsystems.com

    Comment


    • #3
      Michael,
      With your code example, retval is set to error code 2. I hadn't really considered looking into the error because I assumed the syntax of my function call was incorrect and I was looking for a correction. If I go into regedit, I can clearly see there is an entry for HKEY_CLASSES_ROOT\Applications\PBWin.exe\shell\open\command. Anyone with PBWin will also have it. This registry entry can be read when I use the scripting language AutoIt, and I am looking for the right syntax to get it in PBWin.
      Thanks,
      Christopher
      Christopher P. Becker
      signal engineer in the defense industry
      Abu Dhabi, United Arab Emirates

      Comment


      • #4
        > Anyone with PBWin will also have it

        Well, I DON'T have that key, but maybe because when I installed I said "no thanks" to "make PB the default program for *.bas" files.

        But If I had said yes, I would think it would have been for pbedit.exe.

        But I digrtess....

        Code 2 means "file not found." That makes no sense in context.

        Show COMPILABLE example of problem.

        MCM
        Michael Mattias
        Tal Systems (retired)
        Port Washington WI USA
        [email protected]
        http://www.talsystems.com

        Comment


        • #5
          >Instead of "(Default)", I've tried "REG_SZ" and "Value REG_SZ" and others.

          Did you try ""
          This worked on my PC..
          Code:
          #Compile EXE
          #Dim All
          #Register NONE
          #INCLUDE "WIN32API.INC"
           
          FUNCTION ErrorMsg (ByVal ErrorCode As Dword) AS STRING
           LOCAL szMsg AS ASCIIZ * 255
            FormatMessage %FORMAT_MESSAGE_FROM_SYSTEM, ByVal 0, ErrorCode, 0, szMsg, SIZEOF(szMsg), ByVal 0
           FUNCTION = Str$(ErrorCode) +" "+ szMsg
          END FUNCTION 
          '------------------/ErrorMsg
           
          ' TT Pierre Bellisle > [URL]http://www.powerbasic.com/support/pbforums/showthread.php?t=24777[/URL]
          FUNCTION RegDataGet(hMainKey AS DWORD, sKeyName AS STRING, sValueName AS STRING, _
                              sData AS STRING, dwRegType AS DWORD) AS LONG
           'Set sData and dwRegType. Return %ERROR_SUCCESS (0) if no error.
           'dwRegType can be set to NULL if not required.
           'sData can be set to NULL if not required.
           'RegDataGet(%HKEY_LOCAL_MACHINE, "SOFTWARE\A_registry_test", "Value REG_DWORD-1", sBuffer, dwRegType)
           'RegDataGet(%HKEY_LOCAL_MACHINE, "SOFTWARE\A_registry_test", "Value REG_SZ", sBuffer, dwRegType)
           'MsgBox "RegType: " & STR$(dwRegType) & $CRLF & "LEN: " & STR$(LEN(sBuffer)) & $CRLF &"Value: " & sBuffer
           LOCAL Retval AS LONG
           LOCAL dwSize AS DWORD
           LOCAL hKey   AS DWORD
           
           sData = ""
           dwRegType = 0
           
           Retval = RegOpenKeyEx(hMainKey, BYVAL STRPTR(sKeyName), BYVAL %Null, %KEY_READ, hKey)
           IF Retval = %ERROR_SUCCESS Then
             Retval = RegQueryValueEx(hKey, BYVAL STRPTR(sValueName), BYVAL %Null, _
                                      BYVAL %Null, BYVAL %Null, dwSize)
             IF Retval = %ERROR_SUCCESS THEN
               sData = STRING$(dwSize, 0)
               Retval = RegQueryValueEx(hKey, BYVAL STRPTR(sValueName), BYVAL %Null, _
                                        dwRegType, BYVAL STRPTR(sData), dwSize)
               SELECT CASE dwRegType
                 CASE %REG_SZ, %REG_EXPAND_SZ, %REG_MULTI_SZ
                   sData = RTRIM$(sData, $NUL) 'Remove one or two ending $NUL
                 'CASE %REG_SZ, %REG_EXPAND_SZ
                 '  sData = LEFT$(sData, dwSize - 1) 'Remove ending $NUL
                 CASE %REG_DWORD
                   sData = FORMAT$(CVDWD(sData)) 'Convert dword to string
               END SELECT
             END IF
             RegCloseKey(hKey)
           END IF
           FUNCTION = Retval
           'MsgBox STR$(dwRegType) & $CRLF & STR$(dwSize) & $CRLF & sData
           
          END FUNCTION
          '------------------/RegDataGet
           
          FUNCTION PbMain()
           DIM sBuffer$, dwRegType???, sData$, Retval&
           
            sData = ""  ' equiv (Default) -  otherwise = "Name of String Value" (or Binary Value, DWORD etc)
           
            Retval = RegDataGet(%HKEY_CLASSES_ROOT, "Applications\PbEdit.exe\shell\open\command", _
                                sData, sBuffer, dwRegType)
              IF Retval <> %ERROR_SUCCESS THEN MsgBox ErrorMsg(Retval),,"System Error"
           
            MsgBox "RegData: "+sbuffer+",  RegType:"+str$(dwRegType)+",  sData: "+sData
           'Result. RegData: C:\PbWin90\Bin\PbEdit.exe %1  ,RegType: 1,  sData: 
           
          END FUNCTION
          '------------------/PbMain
           
          '"If the function fails, the return value is a nonzero error code defined in Winerror.h. 
          'You can use the FormatMessage function with the FORMAT_MESSAGE_FROM_SYSTEM flag to get 
          'a [I]generic[/I] description of the error". (I got error '2' also on failure - non existing key)
          Rgds, Dave

          Comment


          • #6
            Dave, it worked. Thanks.

            My problem is solved, but perhaps I should expand on my objective for the sake of discussion. I'm peeking into the registry to discover the application associated with certain file types. In my example, PBWin.exe opens *.BAS files. What I'm really crafting is a function that will return the path and executable name for other file types (such as .PPT and .PDF). This is necessary if I want my PowerBasic program to open a PDF document because different users will have different versions of Adobe Acrobat (or other PDF browser). Some users might have MS Office 11, and others might have Office 12, and so on.

            For the sake of the topic, let me ask, is this the best and most reliable way to launch/open various files in their native application from within PB?

            I turned to the registry because I write programs for users who only have basic user privileges (no admin rights) on their PCs. Peeking into this part of the registry doesn't seem to cause any problems.
            Christopher P. Becker
            signal engineer in the defense industry
            Abu Dhabi, United Arab Emirates

            Comment


            • #7
              Have you looked into FindExecutable() ?
              Rgds, Dave

              Comment


              • #8
                >> I'm peeking into the registry to discover the application associated with
                >> certain file types.

                > Have you looked into FindExecutable() ?

                Ain't it amazin' what happens when you explain the WHAT rather than a pre-selected HOW?

                MCM
                Michael Mattias
                Tal Systems (retired)
                Port Washington WI USA
                [email protected]
                http://www.talsystems.com

                Comment


                • #9
                  No question is wasted but some comments are just.. chaff
                  Rgds, Dave

                  Comment


                  • #10
                    I used to just use the SHELL statement and passed it a command of "START document.pdf" but FindExecutable looks like a much better way to go.
                    Jeff Blakeney

                    Comment


                    • #11
                      I used to just use the SHELL statement and passed it a command of "START document.pdf" but FindExecutable looks like a much better way to go.
                      Take a look at ShellExecute and/or ShellExecuteEx... that will find the associated program for you AND open the target file with same in one swell poop.

                      Mr. Becker's problem was different....
                      I'm peeking into the registry to discover the application associated with certain file types....I want my PowerBasic program to open a PDF document [even if user has another type registered for *.PDF]
                      Maybe he's going to offer the user a choice, because simply
                      Code:
                      {SHELL |ShellExecute} MyPbProgram.exe PDFFile
                      .. would open a PDF file with his program.

                      MCM
                      Michael Mattias
                      Tal Systems (retired)
                      Port Washington WI USA
                      [email protected]
                      http://www.talsystems.com

                      Comment

                      Working...
                      X