Solved

BSTR Problem!

Posted on 2001-06-13
18
783 Views
Last Modified: 2013-11-20
Hi,

I am using an automation component which was developed by someone else?


After importing the corresponding *.tlb file,
the complier will genearte one *.tlh file which contain the following code!

__declspec(implementation_key(191))
HRESULT IFnVersionableDual::Checkout ( BSTR * CheckoutPath, _bstr_t Directory, _bstr_t FileName, const _variant_t & Options );

When I use the Checkout method,
I want to know how can I enter the paramter correctly.

I have tried this:

MyObject->Checkout("c:\\test.txt", "c:\\", "test.txt");

But it cannot be complied

The erros is cannot convert parameter 1 from 'char [1]' to 'unsigned short ** '.

How can I correct this problem?

Thanks!
0
Comment
Question by:cplau
  • 7
  • 3
  • 3
  • +3
18 Comments
 
LVL 2

Expert Comment

by:missionImpossible
ID: 6185040
use the _T()-macro:

MyObject->Checkout(
  _T("c:\\test.txt"),
  _T("c:\\"),
  _T( "test.txt")  );


0
 
LVL 42

Expert Comment

by:sedgwick
ID: 6185045
use AllocSysString():

     CString str("Hockey is Best!");
     BSTR bstr = str.AllocSysString();

good luck
0
 
LVL 2

Expert Comment

by:missionImpossible
ID: 6185064
sorry, for the first parameter you should do the following:

_bstr_t CheckOutPath ( _T("c:\\test.txt") );

MyObject->Checkout(
 CheckOutPath,
 _T("c:\\"),
 _T( "test.txt")  );
0
 

Expert Comment

by:SrinivasaRao
ID: 6185069
BSTR was defined as unsigned short* . So please use unsigned short** in the place of BSTR*. I hope this will solve your problem.
BSR
0
 
LVL 6

Expert Comment

by:snoegler
ID: 6185197
>>     CString str("Hockey is Best!");
>>    BSTR bstr = str.AllocSysString();

If you use this code, you have to add an ::SysFreeString(bstr); later!

Instead of allocating a BSTR this way, you can also

1. #include <afxpriv.h>
2. Place an USES_CONVERSION; as the first line of the function.
3. Use T2OLE() to convert from the TCHAR to BSTR/OLECHAR.

->
#include <afxpriv.h>

void doit()
{
USES_CONVERSION;
MyObject->Checkout(T2OLE(_T("c:\\test.txt")), T2OLE(_T("c:\\")), T2OLE(_T("test.txt")));
}
0
 

Author Comment

by:cplau
ID: 6185279
Hi All,

In the tlh file, I also found this code,
 __declspec(property(get=GetNumber))
    _bstr_t Number;

When I try to read this object's properties like this

MyObject->Number

Do I need to do any coversion to get the correct value?

Thanks!
0
 
LVL 42

Expert Comment

by:sedgwick
ID: 6185295
i still think that it better that u maintain your allocation and free instead of rely on the systme

0
 
LVL 2

Expert Comment

by:missionImpossible
ID: 6185296
_bstr_t is a wrapper-class for BSTR, so you can read the Number-property like you suggest. There is no conversion needed.
0
 
LVL 9

Expert Comment

by:ShaunWilde
ID: 6185375
first of all is CheckoutPath an [in],[out],[in,out] parameter - what is in the idl?

this will determine how we deal with it - because it is a BSTR* then I suspect it is an [out] or [in,out] - however it could have just been defined incorrectly

if [out]

_bstr_t bstrCheckout;
MyObject->Checkout(&bstrCheckout, "c:\\", "test.txt");

if [in,out]
_bstr_t bstrCheckout(_T("c:\\test.txt"));
MyObject->Checkout(&bstrCheckout, _T("c:\\"), _T("test.txt"));

I hope this helps



0
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.

 
LVL 9

Expert Comment

by:ShaunWilde
ID: 6185390
however you may have to do it like this if _bstr_t doesn't support & operator to return a pointer to its internal BSTR (which I know CComBSTR does)

if [out]

BSTR bstrCheckout=NULL;
MyObject->Checkout(&bstrCheckout, "c:\\", "test.txt");

::SysFreeString(bstrCheckout);


if [in,out]
BSTR bstrCheckout=::SysAllocString(T2W((_T("c:\\test.txt"))));
MyObject->Checkout(&bstrCheckout, _T("c:\\"), _T("test.txt"));

::SysFreeString(bstrCheckout);
0
 

Author Comment

by:cplau
ID: 6185438
Dear All,

At my first question,

__declspec(implementation_key(191))
HRESULT IFnVersionableDual::Checkout ( BSTR * CheckoutPath, _bstr_t Directory, _bstr_t FileName, const
_variant_t & Options );

The fourth paramter is the optional paramter

CString str("c:\\yyee.txt");
BSTR bstr = str.AllocSysString();
CString str1("c:\\");
BSTR bstr1 = str1.AllocSysString();
CString str2("yyee.txt");
BSTR bstr2 = str2.AllocSysString();

COleVariant name(1);

Temp->Checkout(&bstr,bstr1,bstr2,name);

But the following error :
COleVariant::COleVariant' : ambiguous call to overloaded function

I've also tried using this
ColeVairant name("%d", 1);

But the program crash while running!

What mistake had I made?
How can I input the fourth parameter with a value of 1?

Thanks!
0
 
LVL 42

Expert Comment

by:sedgwick
ID: 6185633
cplau: initialaize the 'name' like this:

COleVariant name(1.0);

and it will work... :)
0
 
LVL 42

Expert Comment

by:sedgwick
ID: 6185634
cplau: initialaize the 'name' like this:

COleVariant name(1.0);

and it will work... :)
0
 
LVL 42

Expert Comment

by:sedgwick
ID: 6185648
or even better:

     COleVariant name((short)1); !!!

since there's no constructor for int...

cheer
0
 
LVL 9

Accepted Solution

by:
ShaunWilde earned 150 total points
ID: 6186161
remember to free your BSTRs that you allocated or else it will leak

CString str("c:\\yyee.txt");
BSTR bstr = str.AllocSysString();

// your code


::SysFreeString(bstr);


> COleVariant::COleVariant' : ambiguous call to overloaded function

use COleVariant name((short)1); // for VT_I2
or COleVariant name((long)1); // for VT_I4
or COleVariant name(1,VT_I2);
or COleVariant name(1,VT_I4);

> ColeVairant name("%d", 1);
COleVariant does not do string formatting - you were telling the variant that the string being passed in was actually a VT_NULL




0
 
LVL 42

Expert Comment

by:sedgwick
ID: 6190149
great, using my comment to get the points
0
 
LVL 6

Expert Comment

by:snoegler
ID: 6190180
Isn't this the point there was a quite lengthy discussion about a short time ago?
:-/
0
 
LVL 42

Expert Comment

by:sedgwick
ID: 6190581
whatever
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

Suggested Solutions

Title # Comments Views Activity
List out all word 7 254
How to determine file rename from EventLog ? 2 41
maxBlock challenge 30 111
Way to decrease size of apk file 9 40
Introduction: Dynamic window placements and drawing on a form, simple usage of windows registry as a storage place for information. Continuing from the first article about sudoku.  There we have designed the application and put a lot of user int…
Introduction: Database storage, where is the exe actually on the disc? Playing a game selected randomly (how to generate random numbers).  Error trapping with try..catch to help the code run even if something goes wrong. Continuing from the seve…
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.
I designed this idea while studying technology in the classroom.  This is a semester long project.  Students are asked to take photographs on a specific topic which they find meaningful, it can be a place or situation such as travel or homelessness.…

948 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

22 Experts available now in Live!

Get 1:1 Help Now