Results 1 to 7 of 7

Thread: [RESOLVED] Registy Setvalue Works in 2k/xp Not in 9X

  1. #1

    Thread Starter
    PowerPoster Fazi's Avatar
    Join Date
    Aug 2005
    Location
    Underworld
    Posts
    2,525

    Resolved [RESOLVED] Registy Setvalue Works in 2k/xp Not in 9X

    Hello !

    VB Code:
    1. Public Sub SearchString()
    2. regkey = "Software\My Software"
    3. strname = Form1.text1.Text
    4. lngresult = RegOpenKeyEx(HKEY_CURRENT_USER, regkey, REG_SZ, keyallaccess, path)
    5. If lngresult = 0 Then
    6.    lngresult=RegSetValueEx (path, "Search Now", 0, REG_SZ, ByVal strname, Len(strname)
    7. End If
    8. lngresult = RegCloseKey(path)
    9. Exit Sub

    Now assume Text1 has no text. A blank text box.

    when we execute SearchString() the Strname will be > strname=""

    Problem is :

    RegSetValueEx returns 87 in Windows98Se. So the function fail to set the value data.

    RegSetValueEx returns 0 successfully on XP and 2K.

    Friends! What is the problem?

  2. #2
    I'm about to be a PowerPoster! Joacim Andersson's Avatar
    Join Date
    Jan 1999
    Location
    Sweden
    Posts
    14,649

    Re: Registy Setvalue Works in 2k/xp Not in 9X

    The problem is that if strname is an empty string then Len(strname) would be 0. Which means that you want to store 0 bytes of a REG_SZ type, but that type is null terminated and the last argument should be the size of the string including the terminating null character. So try to add 1 to Len(strname).

  3. #3
    Fanatic Member schoolbusdriver's Avatar
    Join Date
    Jan 2006
    Location
    O'er yonder
    Posts
    1,020

    Re: Registy Setvalue Works in 2k/xp Not in 9X

    Quote Originally Posted by Fazi
    strname = Form1.text1.Text
    VB Code:
    1. strname = Form1.text1.Text & vbNullChar  'Note how a null character must be appended to the string.

  4. #4
    I'm about to be a PowerPoster! Joacim Andersson's Avatar
    Join Date
    Jan 1999
    Location
    Sweden
    Posts
    14,649

    Re: Registy Setvalue Works in 2k/xp Not in 9X

    That is not really necassary since VB will convert the string you pass to a null terminating string. However, as I already mentioned RegSetValueEx requires to get the length of the string including the terminating null character so you can simply add 1 to the last argument.

  5. #5

    Thread Starter
    PowerPoster Fazi's Avatar
    Join Date
    Aug 2005
    Location
    Underworld
    Posts
    2,525

    Re: Registy Setvalue Works in 2k/xp Not in 9X

    Hai jacim and schoolbus driver !!

    Jacim i tried your solution.

    VB Code:
    1. lngresult=RegSetValueEx (path, "Search Now", 0, REG_SZ, ByVal strname, Len(strname) + 1)

    Result : 98SE Shows NO MERCY. .87 Returned
    if i debug.print strptr(strname) in 98se the debug windw out puts 0. so strname uses no memory. if iam corrct.

    'thanks bush mobile for strptr, it is somthing new for me..

    So i tried this way

    VB Code:
    1. strname = Form1.text1.Text
    2. if strname="" then strname=chr$(0)
    3. ....
    4. ....
    5.    lngresult=RegSetValueEx (path, "Search Now", 0, REG_SZ, ByVal strname, Len(strname) + 1)
    6. ....
    7. ...

    98SE Is happy and returned 0 :-)

    the debug.print shown strptr(strname) somthing assume like 1909809 (not remember the numbers exactly. i am in xp now)
    so i assume strname uses memory.

    So jacim and others... Shall i go ahead using this way ?

  6. #6
    Fanatic Member schoolbusdriver's Avatar
    Join Date
    Jan 2006
    Location
    O'er yonder
    Posts
    1,020

    Re: Registy Setvalue Works in 2k/xp Not in 9X

    Quote Originally Posted by Fazi
    So jacim and others... Shall i go ahead using this way ?
    Yes, I think so. There is another possibility that will remove the "Search Now" key and its value if you do not need it:-

    VB Code:
    1. regkey = "Software\My Software"
    2.    strname = Form1.text1.Text
    3.    lngresult = RegOpenKeyEx(HKEY_CURRENT_USER, regkey, REG_SZ, KEYALLACCESS, path)
    4.    
    5.    If Trim(strname) = "" Then
    6.       lngresult = RegDeleteValue(path, "Search Now")
    7.    Else
    8.       lngresult = RegSetValueEx(path, "Search Now", 0, REG_SZ, ByVal strname, Len(strname) + 1)
    9.    End If
    10.    
    11.    lngresult = RegCloseKey(path)

  7. #7

    Thread Starter
    PowerPoster Fazi's Avatar
    Join Date
    Aug 2005
    Location
    Underworld
    Posts
    2,525

    Re: Registy Setvalue Works in 2k/xp Not in 9X

    Hai jacim and schoolbusdriver

    Thank you for the contributions !!!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width