Solved

Passing BSTR as an argument to a function

Posted on 2004-08-25
5
421 Views
Last Modified: 2008-01-09
Hi,
  I'm building a COM component in VC++. I will access this component from a client also developed in VC++.
The client will get a string from the user (thru some UI) and this string has to be sent to a method in the component.
The component should write this value into the registry (under a predefined key).

1. I am getting the input as a BSTR. How do I pass this to the component?
2. What should the signature of the component side method be?
3. I'm not able to find samples for writing values into the Registry using the RegSetValueEx methods :-(

I have something like this right now.....is this the right way to go about it?

Component:
                        WriteString([in] BSTR bstrText) {........}
 Client:
           BSTR bstrText = ::SysAllocString( L"HelloWorld" );    
           <code to initialize COM and get the interface ptr>
           ptr->WriteString(bstrText);
0
Comment
Question by:manoj_johar
  • 4
5 Comments
 
LVL 16

Expert Comment

by:nonubik
ID: 11893498
Almost.
You need to free the alocated bstr after using it

ptr->WriteString(bstrText);
::SysFreeString(bstrText);

For the registry part:

RegSetValueEx(hMyKey, "MyValueName", 0, (LPBYTE)bstrText, SysStringLen(bstrText));
0
 

Author Comment

by:manoj_johar
ID: 11899595
Hi !
   Well it almost worked!  This is wat I did. We do have to include the 4th parameter REG_SZ right?

Client call:
           BSTR bstrText = ::SysAllocString( L"HelloWorld" );    
           <code to initialize COM and get the interface ptr>
           ptr->WriteString(bstrText);

Component:
                        WriteString([in] BSTR bstrText)
                       {
                             RegSetValueEx(hMyKey, "MyValueName", 0, REG_SZ, (LPBYTE)bstrText, SysStringLen(bstrText));
                       }

The result was that in the registry, only the first character (H) of the string (HelloWorld) was written!  Am i missing something?
0
 
LVL 16

Expert Comment

by:nonubik
ID: 11900617
Right about the 4th param.
Can try REG_MULTI_SZ
0
 
LVL 16

Expert Comment

by:nonubik
ID: 11900707
Or

WriteString([in] BSTR bstrText)
{
   CString strText(bstrText)
   RegSetValueEx(hMyKey, "MyValueName", 0, REG_SZ, (LPBYTE)strText.GetBuffer(), strText.GetLength());
}
0
 
LVL 16

Accepted Solution

by:
nonubik earned 125 total points
ID: 11900826
In fact depends on whether you want a UNICODE string or an ANSI one written in registry.

for ANSI you can choose the CString variant
for UNICODE can choose the first variant with either REG_MULTI_SZ or REG_BINARY
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

  Included as part of the C++ Standard Template Library (STL) is a collection of generic containers. Each of these containers serves a different purpose and has different pros and cons. It is often difficult to decide which container to use and …
This article will show you some of the more useful Standard Template Library (STL) algorithms through the use of working examples.  You will learn about how these algorithms fit into the STL architecture, how they work with STL containers, and why t…
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

749 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