Solved

Passing BSTR as an argument to a function

Posted on 2004-08-25
5
419 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

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

Question has a verified solution.

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

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…
Templates For Beginners Or How To Encourage The Compiler To Work For You Introduction This tutorial is targeted at the reader who is, perhaps, familiar with the basics of C++ but would prefer a little slower introduction to the more ad…
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.
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

776 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