How to convert UnicodeString to BSTR?

602 views Asked by At

I'm having a problem when calling OpenDatabase function (DAO). It's prototipe is:

  virtual HRESULT STDMETHODCALLTYPE OpenDatabase(BSTR Name/*[in]*/, VARIANT Options/*[in,opt]*/, 
                                             VARIANT ReadOnly/*[in,opt]*/, 
                                             VARIANT Connect/*[in,opt]*/, 
                                             Dao_tlb::Database** ppDb/*[out,retval]*/) = 0; // [-1]

So, when I do this:

if(OpenDialog1->Execute() != true) return;

The selected filename is saved in OpenDialog1->FileName. Then I call the function above:

    pDatabasePtr = pDBEngine->OpenDatabase(WideString(OpenDialog1->FileName).c_bstr(), myOpts, myRead, myCon);

and this works! But, the problem is when I try to set the filename to something else:

OpenDialog1->FileName = ParamStr(1); // OpenDatabase don't work in runtime - file not recognised!

or even set the filename inside a function:

    pDatabasePtr = pDBEngine->OpenDatabase(WideString(L"SomeDB.mdb").c_bstr(), myOpts, myRead, myCon);

In both cases I get strange errors and never able to open a database. So, I probably convert UnicodeString/WideString to BSTR incorrectly.

So, why this function (OpenDatabase) works with

if(OpenDialog1->Execute() != true) return;

and does not work with

OpenDialog1->FileName = ParamStr(1); 

How do I set the conversion correctly?

1

There are 1 answers

1
Tracer On BEST ANSWER

I found answer in here if anyone else needs it: https://forums.embarcadero.com/thread.jspa?messageID=498776