Solved

How to get the BSTR value set through  property in ATL COM Dll

Posted on 2002-03-28
6
360 Views
Last Modified: 2013-11-25
Hi,

I have created a property in my COM Dll and the property type is BSTR.I have retrieved a char * and want to get it thru property .How to do that ????

e.g:
STDMETHODIMP XYZ::get_Path(BSTR AppName, BSTR *pVal)
{
  //Retieved a chsr * value lets say "Hello".
  How can I get it as return value in pVal????
Can I Type cast it.If yes,how???
}
0
Comment
Question by:goodami
6 Comments
 
LVL 86

Accepted Solution

by:
jkr earned 50 total points
ID: 6902752
Use

STDMETHODIMP XYZ::get_Path(BSTR AppName, BSTR *pVal)
{
char*p = "HELLO";

*pVal = SysAllocStringByteLen ( p, lstrlen ( p));

}
0
 

Author Comment

by:goodami
ID: 6902790
Thanks jkr,

It Works but now when I call this function from a VB client ,and try to retrieve the value its coming out to be junk.

I am trying like this in VB:

Private Sub Command1_Click()
Dim x As New TESTDLL
Dim y As String

y = x.Path("XY")
msgbox(y)
End Sub

If I print the message using AfxMessageBox("Hello");
it works fine but here I get ????? as message box in VB.Can you pls help me
0
 

Author Comment

by:goodami
ID: 6902817
Thanks jkr,

It Works but now when I call this function from a VB client ,and try to retrieve the value its coming out to be junk.

I am trying like this in VB:

Private Sub Command1_Click()
Dim x As New TESTDLL
Dim y As String

y = x.Path("XY")
msgbox(y)
End Sub

If I print the message using AfxMessageBox("Hello");
it works fine but here I get ????? as message box in VB.Can you pls help me
0
Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

 

Author Comment

by:goodami
ID: 6902850
Thanks jkr,

It Works but now when I call this function from a VB client ,and try to retrieve the value its coming out to be junk.

I am trying like this in VB:

Private Sub Command1_Click()
Dim x As New TESTDLL
Dim y As String

y = x.Path("XY")
msgbox(y)
End Sub

If I print the message using AfxMessageBox("Hello");
it works fine but here I get ????? as message box in VB.Can you pls help me
0
 
LVL 4

Expert Comment

by:pagladasu
ID: 6902930
How about trying this:
STDMETHODIMP XYZ::get_Path(BSTR AppName, BSTR *pVal)
{
 //Retieved a chsr * value lets say "Hello".
 char *ptr = "Hello";
 int len = _tcslen(ptr);
#ifdef _UNICODE
 *pVal = ::SysAllocStringLen(ptr, len);
#else
 int nLen=MultiByteToWideChar(CP_ACP,0,ptr,len,NULL,NULL);
 *pVal = ::SysAllocStringLen(ptr, nLen);
 MultiByteToWideChar(CP_ACP, 0, ptr, len, *pVal, nLen);
#endif

}
0
 
LVL 23

Expert Comment

by:naveenkohli
ID: 6906406
It could be as simple as

STDMETHODIMP XYZ::get_Path(BSTR AppName, BSTR *pVal)
{
TCHAR *p = _T("HELLO");
USES_CONVERSION;
*pVal = SysAllocString (T2OLE(p));
}
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

For most people, the WrapPanel seems like a magic when they switch from WinForms to WPF. Most of us will think that the code that is used to write a control like that would be difficult. However, most of the work is done by the WPF engine, and the W…
A theme is a collection of property settings that allow you to define the look of pages and controls, and then apply the look consistently across pages in an application. Themes can be made up of a set of elements: skins, style sheets, images, and o…
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA. http://www.experts-exchange.com/videos/1478/Excel-Error-Handlin…

813 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

15 Experts available now in Live!

Get 1:1 Help Now