Intel® Fortran Compiler
Build applications that can scale for the future with optimized code designed for Intel® Xeon® and compatible processors.
28381 Discussions

Writing a registry character variable containing a char(0)

ferrad1
New Contributor I
716 Views

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
Honored Contributor II
701 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

0 Kudos
6 Replies
andrew_4619
Honored Contributor II
702 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

0 Kudos
ferrad1
New Contributor I
691 Views
0 Kudos
IanH
Honored Contributor II
677 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
New Contributor I
669 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?

0 Kudos
andrew_4619
Honored Contributor II
660 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.

0 Kudos
ferrad1
New Contributor I
651 Views

Thanks, REG_BINARY is just what I am looking for.

0 Kudos
Reply