Solved

how to use REG_MULTI_SZ data type in registry?

Posted on 2001-08-28
14
1,683 Views
Last Modified: 2007-11-27
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
0
Comment
Question by:a_saleem
  • 5
  • 4
  • 4
  • +1
14 Comments
 

Expert Comment

by:Kevin_Elrod
ID: 6432879
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?
0
 
LVL 32

Expert Comment

by:jhance
ID: 6432966
>>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;
0
 

Author Comment

by:a_saleem
ID: 6433117
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



 
0
 
LVL 32

Expert Comment

by:jhance
ID: 6433162
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.
0
 

Author Comment

by:a_saleem
ID: 6433421
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***
0
 
LVL 4

Expert Comment

by:jtwine100697
ID: 6433576
> 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.
0
 

Author Comment

by:a_saleem
ID: 6433635
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"
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 32

Accepted Solution

by:
jhance earned 20 total points
ID: 6433663
The last time I checked, 0x61 = 'a'.
0
 
LVL 32

Expert Comment

by:jhance
ID: 6433667
>>DWORD dwSize = ( ( ::strlen( lpszA ) + strlen( lpszB ) ) + 3 );

Thanks....  Must be "brain fade"
0
 
LVL 4

Expert Comment

by:jtwine100697
ID: 6433714
NP.  Long day here, too! :|

-=- James.
0
 

Author Comment

by:a_saleem
ID: 6433725
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
0
 
LVL 4

Expert Comment

by:jtwine100697
ID: 6433749
> 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.
0
 
LVL 4

Expert Comment

by:jtwine100697
ID: 6433764
Ahh, your comment came in while I was writing...  That *may* explain why you cannot use a simple string...

-=- James.
0
 

Author Comment

by:a_saleem
ID: 6437816
well on windows NT/2000 there is no hex stuff its only on win 98:)
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

When writing generic code, using template meta-programming techniques, it is sometimes useful to know if a type is convertible to another type. A good example of when this might be is if you are writing diagnostic instrumentation for code to generat…
Many modern programming languages support the concept of a property -- a class member that combines characteristics of both a data member and a method.  These are sometimes called "smart fields" because you can add logic that is applied automaticall…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

895 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now