Solved

Passing BSTR as an argument to a function

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

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

Many modern programming languages support the concept of a property -- a class member that combines characteristics of both a data member and a method.  These are sometimes called "smart fields" because you can add logic that is applied automaticall…
Container Orchestration platforms empower organizations to scale their apps at an exceptional rate. This is the reason numerous innovation-driven companies are moving apps to an appropriated datacenter wide platform that empowers them to scale at a …
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
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.

757 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

17 Experts available now in Live!

Get 1:1 Help Now