Solved

how to use REG_MULTI_SZ data type in registry?

Posted on 2001-08-28
14
1,680 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
Comment Utility
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
Comment Utility
>>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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
> 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
Comment Utility
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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
LVL 32

Accepted Solution

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

Expert Comment

by:jhance
Comment Utility
>>DWORD dwSize = ( ( ::strlen( lpszA ) + strlen( lpszB ) ) + 3 );

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

Expert Comment

by:jtwine100697
Comment Utility
NP.  Long day here, too! :|

-=- James.
0
 

Author Comment

by:a_saleem
Comment Utility
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
Comment Utility
> 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
Comment Utility
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
Comment Utility
well on windows NT/2000 there is no hex stuff its only on win 98:)
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Errors will happen. It is a fact of life for the programmer. How and when errors are detected have a great impact on quality and cost of a product. It is better to detect errors at compile time, when possible and practical. Errors that make their wa…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.

743 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

12 Experts available now in Live!

Get 1:1 Help Now