Link to home
Start Free TrialLog in
Avatar of a_saleem
a_saleem

asked on

how to use REG_MULTI_SZ data type in registry?

this should be any easy one for u.

i want to store valses in resistry like

value name:fruits
data:"apple banana orange"

i am trying to use REG_MULTI_SZ data type for the purpose,but instead of charcter data the ascii values of the data get saved in the registry, i want character data there, not ascii values.

this is very urgent so i am asking here

regds

a_saleem
Avatar of Kevin_Elrod
Kevin_Elrod

I've found that the registry functions can be tricky.  It may be that one of the parameters you've set is incorrect.  Could you display the function?
>>i want character data there, not ascii values.

Could you please explain what you mean by this statement.  You seem to be contradicting yourself as in many cases "character data" is "ascii".

Note that a REG_MULTI_SZ is a collection of ONE OR MORE strings that are each NULL terminated and the END OF THE LIST is a double-NULL.  In C/C++ there is no direct support for this kind of string since C/C++ strings are terminated at the first NULL.  So you must build the buffer yourself.  If I have two strings, lpszA and lpszB:

char *lpszA = "This is string A";
char *lpszB = "This is string B";

I need a buffer that will hold both of these + one extra NULL.  So use a BYTE buffer:

DWORD dwSize = strlen(lpszA) + strlen(lpszB) + 1;
BYTE *cBuffer = new BYTE[dwSize];

I usually fill the buffer with NULLs to ensure that the last 2 bytes are NULL NULL:

memset(cBuffer, 0, dwSize);

Now we need to copy the data into the correct place:

memcpy(cBuffer, lpszA, strlen(lpszA) + 1);
memcpy(cBuffer + strlen(lpszA) + 1, lpszB, strlen(lpszB) + 1);

Now you have a REG_MULTI_SZ string that can be used by RegSetValueEx().

Don't forget to delete the buffer when you are done...

delete [] cBuffer;
Avatar of a_saleem

ASKER

i am fine with buffering stuff the problem is i want the regitry value to be

"abc"

and it is

61 62 63

the whole code works well with data type REG_SZ(yes with only one string) problem arises when REG_MULTI_SZ comes in



 
There is no difference.  You are being confused by RegEdit's inability to display REG_MULTI_SZ entries in textual form.

If you don't want this, use REGEDT32 or avoid the use of REG_MULTI_SZ.
but i have seen entries in correct form with REG_MULTI_SZ data type in regedit,

***and i now realize that these entries are not even ascii values, ascii of a is 97 but 61 is stored there at "a" place, but when i read from my program i am getting exactly what i am storing, that is "abc", ***strange***
> DWORD dwSize = strlen(lpszA) + strlen(lpszB) + 1;

As an aside.  If the resulting buffer is to contain a total of three NULs, each of strlen(...) calls should have "+ 1" added to their return value, because strlen(...) does not count the terminating NUL in the value it returns.

    DWORD dwSize = ( ( ::strlen( lpszA ) + strlen( lpszB ) ) + 3 );

-=- James.
ooo expert

i am not following jhance for buffering, i am using CString for the purpose and it is doing fine, do some thing for registry storage, what is 61 doing at place of "a"
ASKER CERTIFIED SOLUTION
Avatar of jhance
jhance

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
>>DWORD dwSize = ( ( ::strlen( lpszA ) + strlen( lpszB ) ) + 3 );

Thanks....  Must be "brain fade"
NP.  Long day here, too! :|

-=- James.
great work jhance, i will give points tommorow after testing the other apps, i mean what i am changing in the registry is  shared data, if other apps go fine with this hex stuff, every thing in the world will be again as beatifull as it was before this project:)

Regds

Saleem
> i am not following jhance for buffering, i am using CString for the purpose and it is doing fine,

The CString object wraps "standard C-style" strings (which is what a REG_SZ string is).  That means, that it wraps a char/wchar_t buffer that looks like this:

   "This Is A String\0"  (where "\0" is the NUL terminator)

A string of registry data-type REG_MULTI_SZ looks like this:

   "This Is A String\0This Is Another String\0\0" (Anyone remember building the "file-type" list in the standard file dialogs? :)

Which is two (or possibly more) strings contained within a single buffer.  The CString object does *not* support this kind of string.  (Now, having said that, I am sure someone will bring up the ability to dick around with CString::GetBuffer(...) to get a multi-string buffer in there.  Yes you CAN do that, but that does not mean that it is CORRECT to do so.)

FWIW, if you want to store compound data in the registry, just build a SINGLE string to do it, and parse it upon reading it.  Change:

    value name:fruits
    data:"apple banana orange"

To:

   value name:fruits;data:"apple banana orange"

What would be the problem with that?

-=- James.
Ahh, your comment came in while I was writing...  That *may* explain why you cannot use a simple string...

-=- James.
well on windows NT/2000 there is no hex stuff its only on win 98:)