Re: Very strange: the code was good for the last 10 years and now it is stumbling!!!




"Jack" <replyto@it> wrote in message news:OCI2KKZJJHA.5900@xxxxxxxxxxxxxxxxxxxxxxx
Hello,
Maybe someone else had the same problem.

The code writing to Windows registry was good and working perfectly for the last 10 years and now suddenly it stops working
intermittently.

Below, is the code in question:
=====================
Declare Function OSRegCreateKey Lib "advapi32" Alias "RegCreateKeyA" (ByVal hKey As Long, ByVal lpszSubKey As String, phkResult As
Long) As Long
Declare Function OSRegSetValueEx Lib "advapi32" Alias "RegSetValueExA" (ByVal hKey As Long, ByVal lpszValueName As String, ByVal
dwReserved As Long, ByVal fdwType As Long, lpbData As Any, ByVal cbData As Long) As Long

hKey = HKEY_CURRENT_USER
lpszSubKey = "Software\Me\My Software"
rtn = OSRegCreateKey(hKey, lpszSubKey, phkResult)
rtn = OSRegSetValueEx(phkResult, "My Key", 0, 1, ByVal Release, Len(Release))
=======================



Code does not just stop working. *Something* is different. You need to figure out what that is (different version of Windows, new
service pack, some other code change that could be affecting this code, etc., etc.).

It would have helped if you had included the declarations for your variables. Perhaps you inadvertantly changed a variable's data
type. The data type for your Release variable isn't a variant by any chance, is it? A variant should never be passed to an API
function.

Based on the above, I see some things you're doing wrong and/or should do differently IMO.

When writing a string value, the value you pass for the cbData parameter of RegSetValueEx must be 1 more than the length of the
string. This is documented in MSDN Library and the Platform SDK ("If the data is of type REG_SZ, REG_EXPAND_SZ, or REG_MULTI_SZ,
cbData must include the size of the terminating null character or characters.") So, you should be passing Len(Release) +1. This is
to account for the terminating null character in the string (which VB "appends" automatically for you when passing a string to an
API function). It's possible that a service pack or other Windows update (hotfix, etc.) has made this a stricter requirement. It
wouldn't be the first time an update has done that very thing.

Get rid of "magic" numbers. Instead of passing 1 for fdwType, declare the constant REG_SZ and pass that. Makes your code MUCH
clearer and cleaner.

I'd also recommend assigning Len(Release) + 1 to a variable of type Long and passing that variable for cbData, but I doubt that's
the problem.

I noticed you're using RegCreateKey. While I would not suspect that to be the problem either, you *really* should be using
RegCreateKeyEx Even in the October 2001 edition of MSDN Library, MS states for RegCreateKey: "This function is provided only for
compatibility with 16-bit versions of Windows. "


--
Mike



--
Mike



.



Relevant Pages

  • Re: [PHP] Pass Windows credentials
    ... I want to create a PHP page where users can authenticate themselves passing their Windows logon credentials trasparently. ...
    (php.general)
  • Re: [opensuse] Enigmail header problem in Thunderbird on SuSE...
    ... to do with the handling of this info when passing the relevant parts of ... the message to gpg. ... Didnt look at that one as it is flagged as Windows only.. ... Comment: Using GnuPG with SUSE - http://enigmail.mozdev.org ...
    (SuSE)
  • Re: HDD suddenly failed, please help - Update
    ... Windows XP SP2. ... The first warnings I can see in Event Manager ... Then select that particular drive thats doubtful and post that data ... OK: Always passing ...
    (comp.sys.ibm.pc.hardware.storage)
  • Re: HDD suddenly failed, please help - Update
    ... Windows XP SP2. ... The drive shows up in Device Manager with no warnings ... OK: Always passing ... Urk, thats the problem, thats an utterly obscene reallocated sector count. ...
    (comp.sys.ibm.pc.hardware.storage)
  • Re: HDD suddenly failed, please help - Update
    ... Windows XP SP2. ... The drive shows up in Device Manager with no warnings ... OK: Always passing ... Urk, thats the problem, thats an utterly obscene reallocated sector count. ...
    (comp.sys.ibm.pc.hardware.storage)