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

Writing a registry character variable containing a char(0)

ferrad1
Neuer Beitragender I
1.357Aufrufe

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 Lösung
andrew_4619
Geehrter Beitragender III
1.342Aufrufe

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

Lösung in ursprünglichem Beitrag anzeigen

6 Antworten
andrew_4619
Geehrter Beitragender III
1.343Aufrufe

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

ferrad1
Neuer Beitragender I
1.332Aufrufe
IanH
Geehrter Beitragender III
1.318Aufrufe

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
Neuer Beitragender I
1.310Aufrufe

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
Geehrter Beitragender III
1.301Aufrufe

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
Neuer Beitragender I
1.292Aufrufe

Thanks, REG_BINARY is just what I am looking for.

Antworten