Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

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

0 views
Skip to first unread message

Jack

unread,
Oct 3, 2008, 3:59:32 PM10/3/08
to
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.
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))
=======================


MikeD

unread,
Oct 3, 2008, 4:53:51 PM10/3/08
to

"Jack" <replyto@it> wrote in message news:OCI2KKZJ...@TK2MSFTNGP02.phx.gbl...

> 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

expvb

unread,
Oct 3, 2008, 5:25:51 PM10/3/08
to
Are you calling RegCloseKey? If not, then your code may be leaking handles
and something stops working after a while.


Bill McCarthy

unread,
Oct 3, 2008, 11:11:35 PM10/3/08
to
Hi Jack,

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...

Jack

unread,
Oct 4, 2008, 11:50:22 AM10/4/08
to
Thats right.
The return value is zero but the registry is not written.
Without changing anything, (BTW, it is a setup program) runing the setup
again puts that value in the registry.
To add to the puzzle:
I am using similiar setup program (having exactly the same code in question)
in 5 different applications, and
only one, particular setup program has developed that problem recently.
To add even more to the puzzle.
I did not touch (modify) that setup program for over a year, but problem
started just about a month or two ago.
Jack

"Bill McCarthy" <Bi...@localhost.com> wrote in message
news:%23cVYu7c...@TK2MSFTNGP05.phx.gbl...

Bill McCarthy

unread,
Oct 4, 2008, 10:13:27 PM10/4/08
to
Hi Jack,

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...

Ken Halter

unread,
Oct 8, 2008, 11:07:42 PM10/8/08
to

"Jack" <replyto@it> wrote in message
news:ufS3mjjJ...@TK2MSFTNGP06.phx.gbl...

> Thats right.
> The return value is zero but the registry is not written.

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


MP

unread,
Oct 9, 2008, 11:20:56 AM10/9/08
to

"Ken Halter" <Ken_Halter@Use_Sparingly_Hotmail.com> wrote in message
news:utDfTvbK...@TK2MSFTNGP04.phx.gbl...

> "Jack" <replyto@it> wrote in message
> news:ufS3mjjJ...@TK2MSFTNGP06.phx.gbl...
>> Thats right.
>> The return value is zero but the registry is not written.
>
> 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
>

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


mayayana

unread,
Oct 9, 2008, 12:07:09 PM10/9/08
to
> 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_2175879
1.html?qid=21758791
>

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


Ken Halter

unread,
Oct 9, 2008, 12:14:29 PM10/9/08
to
"MP" <NoS...@Thanks.com> wrote in message
news:%238mKhKi...@TK2MSFTNGP03.phx.gbl...

>
> 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

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


Ken Halter

unread,
Oct 9, 2008, 12:12:37 PM10/9/08
to
"mayayana" <mayaX...@rcXXn.com> wrote in message
news:ex89NjiK...@TK2MSFTNGP04.phx.gbl...

>
> 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

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.

expvb

unread,
Oct 9, 2008, 12:30:39 PM10/9/08
to
"expvb" <nob...@cox.net> wrote in message
news:%23secs6Z...@TK2MSFTNGP03.phx.gbl...

> Are you calling RegCloseKey? If not, then your code may be leaking handles
> and something stops working after a while.

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.

0 new messages