Community
cancel
Showing results for 
Search instead for 
Did you mean: 
ferrad1
Novice
123 Views

Writing a registry character variable containing a char(0)

Jump to solution

I am writing a 40 character variable to the registry using:

iSt = RegSetValueEx(hKey, sValueName, 0, REG_SZ, loc(sValue), iLen)

However sValue (length 40) may contain char(0) bytes, it is an encrypted string.  iLen is 40.

The call is only writing to the registry variable up to one character before the char(0), as it is thinking it is NULL terminated.  How do I get it write the full 40 character variable?

0 Kudos
1 Solution
andrew_4619
Valued Contributor III
108 Views

lpData

The data to be stored.

For string-based types, such as REG_SZ, the string must be null-terminated. With the REG_MULTI_SZ data type, the string must be terminated with two null characters.

 

Use REG_MULTI_SZ type and double terminate

View solution in original post

6 Replies
andrew_4619
Valued Contributor III
109 Views

lpData

The data to be stored.

For string-based types, such as REG_SZ, the string must be null-terminated. With the REG_MULTI_SZ data type, the string must be terminated with two null characters.

 

Use REG_MULTI_SZ type and double terminate

View solution in original post

ferrad1
Novice
98 Views

Thanks, that worked.

 

IanH
Black Belt
84 Views

To write arbitrary binary data to a registry value (which is what you have if your encryption method spits out null characters), use REG_BINARY as the type.  The type of data being written to the registry is here a separate concept from the type of the variable being used to manipulate the data in your Fortran program.

(If you use REG_MULTI_SZ, then one day your encryption method will spit out two null characters in a row, and the problem will reoccur (note also the API requires double null termination for REG_MULTI_SZ - do you ensure that?).  Because you've said the data is a string (i.e. a sequence of characters that humans can "read") it is also be subject to code page translation on the way in and out of your very-likely-not-unicode application.  Beyond null characters, does your encryption method guarantee that the "string" is a valid multibyte sequence in the current code page ?  How will the application respond to a later change in code page once the registry key has been written?)

ferrad1
Novice
76 Views

Your last point of 2 null characters in a row came into in mind during my walk, and I was going to post  the question when I got back, but you beat me to it!  So if I use REG_BINARY will that get also around the code page issue?

andrew_4619
Valued Contributor III
67 Views

I had not appreciated that you were not writing "text" data , REG_BINARY is arbitrary binary data in any form. The interpretation of that data when you read it back is your responsibility, you shouldl get back exactly what you put there.

ferrad1
Novice
58 Views

Thanks, REG_BINARY is just what I am looking for.

Reply