Announcement

Collapse
No announcement yet.

Can't write "null" binary data to registry

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

  • Can't write "null" binary data to registry

    I want to write the following binary data to a key in my registry: [40 18 00 00 00 c0 46 c4] The first two bytes write, but when I reach the first "00", everything after that fails to write.

    Here is my code:
    '===========================
    'Write binary data: [40 18 00 00 00 c0 46 c4] to registry @ HKLM\SYSTEM\Setup\test
    #COMPILE EXE
    $INCLUDE "WIN32API.INC"

    FUNCTION SetReg(BYVAL hLocation AS DWORD, BYVAL sSubKeys AS STRING, BYVAL sValueName AS STRING, BYVAL sData AS STRING) AS LONG
    LOCAL hKey AS DWORD, zRegName AS ASCIIZ * 20, zRegVal AS ASCIIZ * 20, dwType AS DWORD, dwSize AS DWORD
    zRegVal = sData
    zRegName = sValueName
    IF RegCreateKeyEx(hLocation, TRIM$(sSubKeys, "\"), 0, "", 0, %KEY_WRITE, BYVAL %Null, hKey, BYVAL %Null) = %ERROR_SUCCESS THEN
    ' Set length of data...
    dwSize = LEN(zRegVal)
    'dwType = %REG_SZ
    dwType = %REG_BINARY
    IF RegSetValueEx(hKey, zRegName, 0, dwType, zRegVal, dwSize) = %ERROR_SUCCESS THEN FUNCTION = %True
    RegCloseKey hKey
    END IF
    END FUNCTION

    FUNCTION PBMAIN () AS LONG

    hData$=CHR$(&H40)& CHR$(&H18)& CHR$(0)& CHR$(0)& CHR$(0)& CHR$(&H46)& CHR$(&HC4)

    IF SetReg(%HKEY_LOCAL_MACHINE, "SYSTEM\Setup", "test", hData$) THEN
    STDOUT "Success"
    ELSE
    STDOUT "Error"
    END IF

    END FUNCTION

  • #2
    Try this

    Code:
    #Compile Exe
    $Include "WIN32API.INC"
    
    Function SetReg(ByVal hLocation As Dword, ByVal sSubKeys As String, ByVal sValueName As String, sData As String) As Long
    Local hKey As Dword, zRegName As Asciiz * 20, zRegVal As Asciiz * 20, dwType As Dword, dwSize As Dword
    zRegVal = sData
    zRegName = sValueName
    If RegCreateKeyEx(hLocation, Trim$(sSubKeys, "\"), 0, "", 0, %KEY_WRITE, ByVal %Null, hKey, ByVal %Null) = %ERROR_SUCCESS Then
    ' Set length of data...
    dwSize = Len(sData)
    'dwType = %REG_SZ
    dwType = %REG_BINARY
    If RegSetValueEx(hKey, zRegName, 0, dwType, ByVal StrPtr(sData), dwSize) = %ERROR_SUCCESS Then Function = %True
    RegCloseKey hKey
    End If
    End Function
    
    Function PBMain () As Long
    
    hData$=Chr$(&H40)& Chr$(&H18)& Chr$(0)& Chr$(0)& Chr$(0)& Chr$(&H46)& Chr$(&HC4)
    
    If SetReg(%HKEY_LOCAL_MACHINE, "SYSTEM\Setup", "test", hData$) Then
    STDOUT "Success"
    Else
    STDOUT "Error"
    End If
    
    End Function
    There is now no need for zRegVal.
    Last edited by David Roberts; 7 Feb 2008, 05:26 AM. Reason: Replaced MsgBox with STDOUT - I default to using PBWin!

    Comment


    • #3
      Your coding worked beautiful!

      David,
      Thanks so much for the fix. It not only works but it is beautiful in form compared to the mess of unused variables I had before. I'm quite inexperienced in this but the love of Powerbasic's small and independent exe's is what drives me. If you are ever in the Evansville, IN area, please drop by for a visit. Being ancestorially linked to England (see my last name), I love watching Brit, Ian Wright, on "Globe Trekker" travel videos here in the U.S.

      Alan Clarke
      www.atechworld.com

      Comment


      • #4
        > If you are ever in the Evansville, IN area

        The wood furniture capital of the world, right?

        (I was there about 20 years ago).

        UPDATE:

        No , that's wrong... I flew into Evansville on my WAY to the wood furniture capital of the world: Jasper, IN
        Michael Mattias
        Tal Systems (retired)
        Port Washington WI USA
        [email protected]
        http://www.talsystems.com

        Comment


        • #5
          I have a similar situation but different senario.

          my 2.8 ghz emachines windows XP i use to call into a clients, running a non-windows OS. when printing HP PCL raster graphics on my hp deskjet it works
          fine.

          At the client. One of the users is using the same terminal emulation software on a direct connect, with the same printer settings. The computer is a Compaq ~3ghz win XP. When the pcl raster graphics are sent to it, it loses the chr$(0) s or at least most of them making the raster graphic strings the wrong length and resulting in garbage. I ended up changing all chr$(0) to chr$(1) which will get me by though some 'shading' is noticeable on some graphics.

          I just don't understand why the same settings (WinPrint > Text) all rest pretty standard would cause this difference. I have tried different printers so it's not the printer but the way the 2 computers handle the strings or streams sent. I've been up for 36 hours approx -- it's got me frustrated.

          Just to clear things up -- both win computers are acting as terminals to the remote Operating system.
          Client Writeup for the CPA

          buffs.proboards2.com

          Links Page

          Comment


          • #6
            Thanks Alan. Now that you have it working you may like to consider treating zRegName in a similar manner and remove the restriction of a fixed length string and have another unused variable.

            Added: Since a null will not be in the value name it can go through BYREF but it will be advisable to add Chr$(0)to the RegSetValueEx parameter.
            Last edited by David Roberts; 7 Feb 2008, 10:42 PM. Reason: Spelling!

            Comment


            • #7
              Hi David,

              you have used a ASCIIZ variable. This type of String will use all chars from left until a chr$(0) ($NULL) is comming. The RegSetValue() function waits for a pointer and a length. This is what I read in Win32 Programmer's Referenz. So it might be wrong defined in the Win32Api.inc - but it will work if the last parameter cbData has the right len of needed chars.

              I think your code failed in this line:

              dwSize = Len(sData)

              AS STRING: cData = "123"+$null+"456" -> len(cData) -> 7
              AS ASCIIZ: sData = "123"+$null+"456" -> len(sData) -> 3
              Last edited by Hubert Brandel; 8 Feb 2008, 09:57 AM.
              Regards,
              Hubert

              ------------------------------------
              http://familie-brandel.de/index_e.html

              Comment


              • #8
                Hubert, you have Alan's and my code mixed up.

                Alan's code failed because of dwSize = LEN(zRegVal)

                Comment


                • #9
                  Originally posted by David Roberts View Post
                  Hubert, you have Alan's and my code mixed up.
                  Alan's code failed because of dwSize = LEN(zRegVal)
                  not only the code, sorry for that.
                  Regards,
                  Hubert

                  ------------------------------------
                  http://familie-brandel.de/index_e.html

                  Comment

                  Working...
                  X