Solved

Passing BSTR as an argument to a function

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

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

Introduction This article is a continuation of the C/C++ Visual Studio Express debugger series. Part 1 provided a quick start guide in using the debugger. Part 2 focused on additional topics in breakpoints. As your assignments become a little more …
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
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 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…

930 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

20 Experts available now in Live!

Get 1:1 Help Now