-3

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?

Tracer
  • 2,544
  • 2
  • 23
  • 58
  • how is `OpenDialog1->FileName` declared? – AndersK Dec 28 '13 at 21:38
  • It is not declared. It is initialized by calling OpenDialog1->Execute(). – Tracer Dec 28 '13 at 21:39
  • but you must have some kind of declaration e.g. function/variable/datatype what not. You attempt to assign to it, so FileName must be a variable then of some kind? – AndersK Dec 28 '13 at 21:40
  • There is no need for any declaration since Execute() sets the FileName after user selects the file. I'm just trying to bypass Execute and set the FileName to something else - like to ParamStr(1). – Tracer Dec 28 '13 at 21:43
  • Regardless of what you're trying to do, @claptrap's question still remains: How is `FileName` declared? It must have a data type. What is that type? – Carey Gregory Dec 28 '13 at 21:51
  • You say you have a problem with conversion BSTR and Unicode but you fail to say what type "FileName" is so it is not possible to give you a proper answer. Also consider that Execute may do things behind that scene that are related to "FileName" so setting it to another value may be dicey – AndersK Dec 28 '13 at 21:53
  • FileName is UnicodeString. – Tracer Dec 28 '13 at 21:57
  • declared as? wstring? wchar_t* , LPSTR, LPWSTR ... ? – AndersK Dec 28 '13 at 21:58
  • Don't understand what are you asking me? FileName from OpenDialog is declared as UnicodeString. – Tracer Dec 28 '13 at 21:59

1 Answers1

0

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

Tracer
  • 2,544
  • 2
  • 23
  • 58