Archive: String terminated with 2 NULLs


String terminated with 2 NULLs
Hello. I'm creating an installer for my WinAmp plugin. One of the things I need to do is write a REG_MULTI_SZ string to the registry, and I was able to do this, but I'm concerned I'm hiding a potential bug.

The problem is the string that I pass into the call is NULL terminated, but the MSDN docs for RegSetKeyValueA require me to have two NULLs at the end of the string to signify the actual end of the buffer.

I can't figure out how to create such a string - the NSIS parser doesn't seem to support '\0' as an escape character.

For now, I'm telling RegSetKeyValueA that my buffer is one byte bigger than it actually is, and this happens to work because a second NULL happens to follow my NULL terminated string, but that is just luck and I figure sometime somewhere this will fail.

Is there a way to embed additional NULLs in my string? Is there a way to define my string as a hex array?

Thanks,
Brian


1. I think if you call the instructions provided by NSIS to write a string to the registry (e.g. WriteRegStr or WriteRegExpandStr), NSIS will automatically append the required NULL chars, so you don't need to pass them explicitly.

2. You can call WriteRegBin to write a block of binary data to the registry. This data has to be provided as a hexadecimal string.


Thanks for the info. I assume WriteRegStr, WriteRegExpandStr, and WriteRegBin create values of type REG_SZ, REG_EXPAND_SZ, and REG_BINARY respectively.

Unfortunately, I need the resulting value to be of type REG_MULTI_SZ because the application that will act on these values will fail if the type isn't correct.

Thanks,
Brian


If you feel unsure you may try the registry plugin:

http://nsis.sourceforge.net/Registry_plug-in


Thanks for the link! It looks like that will solve the problem, but I'm going to try to solve it without requiring another DLL.

It looks like NSIS zeros out local buffers of a larger size than what you request before each use, so I'm probably safe as is, but I'm going to use a silly trick as a 'just-in-case'.

I StrCpy a 40 byte string to $8 in one call (so the 40th element will be NULL terminated), then use a 2nd StrCpy to copy in a 39 byte string to $8 (so now the 39th and 40th elements are now NULL).

It works, but again, I think NSIS is solving my problem and my silly trick is redundant.

Thanks,
Brian


this is undefined behavior (could change at any time)

The proper way to do this would be to use the system plugin (allocate a structure, and call the registry)