PDA

View Full Version : Registry.DoesValueExist()


csd214
03-21-2005, 10:23 AM
Several forum members have asked for this action by analogy with Registry.DoesKeyExist(). Even without this action it is not difficult to write a working code:

sValueData = Registry.GetValue(HKEY_CURRENT_USER, sMyRegKey, "AppDir", true);
if sValueData == "" then
-- what to do when the value does NOT exist
else
-- do something when value does exist
end

There is a disadvantage with this code; the setup log will have en "Error" entry. I do not want the end user to believe there is a malfunction in my setup.

To avoid the log error message, I have to add this test:

nTypeValue = Registry.GetValueType(nFolder, sKey, sValue);
if nTypeValue == 0 then
-- the value does NOT exist
else
-- OK, read the value
end

Sometimes I want the data from several values assigned to the same key. Another way to avoid the log error:

sRegAppDir = "?"; sRegUninDate = "0000-00-00"; -- init
sRegInstDate = "0000-00-00"; sRegVer = "?"; -- init

sIrpKey = SessionVar.Expand("Software\\%CompanyName%\\%ProductName%");

tValueNames = Registry.GetValueNames(HKEY_CURRENT_USER, sIrpKey);
for i, d in tValueNames do
if d == "AppDir" then
sRegAppDir = Registry.GetValue(HKEY_CURRENT_USER, sIrpKey, "AppDir", true);
elseif d == "Uninstalled" then
sRegUninDate = Registry.GetValue(HKEY_CURRENT_USER, sIrpKey, " Uninstalled ", true);
elseif d == "Installed" then
sRegInstDate = Registry.GetValue(HKEY_CURRENT_USER, sIrpKey, " Installed ", true);
elseif d == "Version" then
sRegVer = Registry.GetValue(HKEY_CURRENT_USER, sIrpKey, "Version", true);
end
end

Obviously the test for the existence of a value is a recurring operation. Today I decided to write a generic function and put it in a table like Lorne has taught us (AMS Examples (http://www.indigorose.com/forums/showthread.php?t=7780)).

To put functions into tables is a great feature. My collection is increasing!

If I use the table 'Registry', I can use my function this way:

bAppDirExist = Registry.DoesValueExist("HKCU", sRegKey, sValueName);

At last I come to my question:

Is it dangerous to use a table with the name 'Registry' because that is a group of internal actions in SUF70 (and other IR products)?

(If you are interested, you will find the function in the attached file.)
BTW, Moderator, could .lua be an permitted file extension for upload?

csd214
03-23-2005, 05:20 AM
I should highly appreciate your answer:

Is there any disadvantage in using an 'internal table name' when creating a user defined function?

Example: Registry.DoesValueExist() as in my previous post.

I'm now going to publish a project with an 'action extension' like that; but I shouldn't if the method is "dangerous".

Brett
03-23-2005, 09:12 AM
There is no real "danger" per-se. The only risk being run is if we ever added that function to the product. Although even then yours would overwrite it so if you like your function, then there is no problem.

Personally if I were making a library of extended registry functions I would call it something like RegistryEx or RegistryExtended. But either way is fine.

csd214
03-23-2005, 09:28 AM
Thanks for your time, Brett.

I'll go for RegistryEx (I liked that suggestion); it is less confusing both at present and in the future.

BTW, my published function should start with a Registry.DoesKeyExist() action.