Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Passing BSTR as an argument to a function

Posted on 2004-08-25
5
Medium Priority
?
427 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 500 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

Industry Leaders: 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

Introduction This article is the first in a series of articles about the C/C++ Visual Studio Express debugger.  It provides a quick start guide in using the debugger. Part 2 focuses on additional topics in breakpoints.  Lastly, Part 3 focuses on th…
  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 …
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

610 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