PDA

View Full Version : Wish registry functions worked with variables for rootkeys


nsfis
11th March 2004, 15:14
I started to write this piece of code to decide which root key to use for storing certain configuration values in registry.


${If} $OSgen == "Win9x" ; Windows 95/98/ME
StrCpy $RootKey "HKLM"
${Else} ; WinNT/Win2000,WinXP
${Select} $UserRights
${Case2} "User" "Guest"
StrCpy $RootKey "HKCU"
${Case2} "Admin" "Power"
${If} ${Context} == "1" ; context selected during instalation, 1=All users, 0=Current user
StrCpy $RootKey "HKLM"
${Else}
StrCpy $RootKey "HKCU"
${EndIf}
${EndSelect}
${EndIf}

And I wanted to use it this way:

WriteRegStr $RootKey "Software\${Company}\${ProductName}" "Key1" "Value"

But finally I have found that registry functions don't accept variables for rootkey. So this is my feature suggestion for next version of NSIS.

zimsms
11th March 2004, 15:34
Use quotes around $rootkey:

WriteRegStr "$RootKey" "Software\${Company}\${ProductName}" "Key1" "Value"

Joost Verburg
11th March 2004, 16:39
That won't help. Variables are not supported there.

nsfis
12th March 2004, 09:14
And one more note:

It would be also nice if Start menu would support variable rootkey. Currently it is defined during compilation (!define MUI_STARTMENUPAGE_REGISTRY_ROOT "HKLM") and does not allow to set it in runtime based on user environment.

zimsms
12th March 2004, 17:11
I created a macro for writing to the registry, in which I pass HKLM or HKCU depending and then reference it like a normal macro would. This appears to work fine for me.

!insertmacro Write2Reg "HKLM" "Software\MyCompany" "Chicken" "Egg"

!macro Write2Reg ROOT KEY NAME VALUE
WriteRegStr "${ROOT}" "${KEY}" "${NAME}" "${VALUE}"
!macroend


Are you saying, Joost, that this isn't supported?

Joost Verburg
12th March 2004, 17:45
I have not checked the details. It it works fine for you, you have found a way to avoid the check.

zimsms
12th March 2004, 17:47
So should I change this, or will the above be supported?

Joost Verburg
12th March 2004, 18:38
If it works there is no need to change it.

nsfis
15th March 2004, 07:59
zimsms,

your macro works, but it doesn't solve what I wanted. Macro only expands its definition during compilation, nothing more. I wanted to use variable (defined in runtime) for a root key. If I use your macro with variable ($R1):

!insertmacro Write2Reg $R1 "Software\MyCompany" "Chicken" "Egg"

it does not work and I get the same error during compilation:

Usage: WriteRegStr rootkey subkey entry_name new_value_string
root_key=(HKCR|HKLM|HKCU|HKU|HKCC|HKDD|HKPD)

because WriteRegStr function does not allow first parameter to be variable. Macro cannot change anything with this.

zimsms
15th March 2004, 13:15
Ahh, sorry I missed that. I only needed to know at compile time. I agree that would be a nice feature request.

Joost Verburg
15th March 2004, 14:41
I did not check that code. It's indeed just a macro that is processed on compile-time.

deguix
16th March 2004, 21:32
Summarizing:

Macros parameters in which are used codes are like defines, it is like just putting another name to a variable, like:

Section

StrCpy $R0 Test
!define TEST $R0
MessageBox MB_OK ${TEST}

SectionEnd

As the professional way to say it, when you use ${TEST} it is just linking to the buffer that contains the string, but not the string itself, so if a parameter doesn't support variables feature, it doesn't look inside that buffer for the string, it returns the variable name as a string (i.e. $R0).

Vytautas
17th March 2004, 00:18
I think I might have designed a macro that should fix your problem. If you could test the attached macro and if it works I shall convert all of the reg functions into similar macros.

Vytautas

nsfis
17th March 2004, 09:47
Vytautas,

yes, it is a solution. I wrote something similar for HKLM and HKCU only. But the disadvantage of using macros is that each usage of your WriteRegStr macro (and I have lot of them) is expanded during compilation to 21 instructions. That's why I wrote my wish so some core NSIS developer could enable variable rootkey in WriteRegStr instruction.

Thanks anyway.