The code writing to Windows registry was good and working perfectly for the
last 10 years and now suddenly it stops working intermittently.
By " intermittently" I mean that when on the first run the code fails (there
is not any error returned, just the registry is not written), the repeated
attempt to run the same code is mostly successful like that sample below:
-----------------------
rtn = OSRegSetValueEx(phkResult, "My Key", 0, 1, ByVal Release,
Len(Release))
rtn = OSRegSetValueEx(phkResult, "My Key", 0, 1, ByVal Release,
Len(Release))
-----------------------
And, that happens on the different user computers in different countries(!),
and it happened (although once only) on my computer, when testing user
complaint (Windows XP)
I appreciate any comments on that.
Jack
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
When you say there is no error returned do you mean that the rtn value is
zero for the call to RegCreateKey ?
"Jack" <replyto@it> wrote in message
news:OCI2KKZJ...@TK2MSFTNGP02.phx.gbl...
"Bill McCarthy" <Bi...@localhost.com> wrote in message
news:%23cVYu7c...@TK2MSFTNGP05.phx.gbl...
Strange indeed. Is the problem on XP or is it Vista ? The RegCreateKey API
is meant for support for 16 bit applications. The docs actually say don't
use it, rather use RegCreateKeyEx
<quote>
Note This function is provided only for compatibility with 16-bit versions
of Windows. Applications should use the RegCreateKeyEx function.
</quote>
So I'm wondering if this is a compatibility issue. Have you tried using
RegCreateKeyEx ?
"Jack" <replyto@it> wrote in message
news:ufS3mjjJ...@TK2MSFTNGP06.phx.gbl...
I swear I've had this problem, but can't recall the details or the "fix"...
Here's the registry class I use... usage couldn't be much easier <g>
Complete Registry control
http://www.vbaccelerator.com/home/VB/Code/Libraries/Registry_and_Ini_Files/Complete_Registry_Control/article.asp
...and here's a complete sample that looks similar to the code you're using.
How To Use the Registry API to Save and Retrieve Setting
http://kbalertz.com/145679/Registry-Retrieve-Setting.aspx
btw, what AV are you using? AVG? Are you running Ad-Watch, by any chance?
What ever you're using, try disabling everything for a while, just to make
sure they're not the cause... if no help there, try writing to a different
location, just for test purposes... sad part is, I remember the problem
well... but can't remember the fix! It may've been as simple as changing
KEY_ALL_ACCESS (which is the setting you find in most samples) to
KEY_SET_VALUE for writes....
Here's someone with the same problem, but to read the answer you have to
"join", so...
SaveSetting in a VB6 won't write to the registry on a particular machine
http://www.experts-exchange.com/Programming/Languages/Visual_Basic/Q_21758791.html?qid=21758791
the link on that page to a wrapper by Daniel Henry goes to Hashpipe.org....
imagine my surprise!
:-)
someone's smokin somethin ... the least they could do is share...<g>
mark
Here's the Google cache version, which shows the
answers, but despite there being a "sage" and a "guru"
involved, there doesn't seem to be much useful info.
in the thread. :)
http://64.233.169.104/search?q=cache:c26kF7Q7uRkJ:www.experts-exchange.com/P
rogramming/Languages/Visual_Basic/Q_21758791.html%3Fqid%3D21758791+%22VB6+ap
p+uses+SaveSetting.+On+4+test+machines%22&hl=en&ct=clnk&cd=1&gl=us
LOL!... I'll have to save that link and pass it on to those who expect VB6
to "suddenly disappear" because something "new" hit the market.
--
Ken Halter
Part time groupie
I figured as much... that's one reason I don't like "joining" those groups.
iirc, it took quite a while for Experts Exchange to finally stop sending me
spam.
I bet the above is the issue. I once had a problem with a 3rd party software
that opens a key and checks something several times a second and doesn't
close it. The interval is variable between 3 times a second and every 2
seconds. After few hours to 24 hours, it misbehaves.
Normally, Task Manager shows 10000 to 20000 handles on the performance tab.
After running that software for few hours, I see the number of handles is
slightly over 66000. Using Regmon, it shows that error 1450: "Insufficient
system resources exist to complete the requested service. " is returned when
that software tries to open the key.
I think that software was using RegCreateKeyEx, rather than RegCreateKey,
but I am not sure. The OS is XP Pro + SP2.