That code fails because OleFunction() will internally take ownership of the
memory that the whar_t* pointer is pointing to. When OleFunction() returns,
it tries to free the owned memory. But it can't because the memory being
pointed to is in the executable's static read-only memory and cannot be
freed programmably at all. It is freed only when the executable is
terminated.
> however, if I use "path2.c_bstr()" instead of "path3" on
> call OleFunction load(), then run is normal and no stop
The call to OleFunction() will run without problem, but the WideString will
crash later on when it goes out of scope. If you pass a BSTR to
OleFunction(), it will take ownership of the BSTR and then free it when
OleFunction() returns. So, if you use the c_bstr() method of WideString,
OleFunction() will free the BSTR that the WideString contains, leaving the
WideString pointing to a non-invalid BSTR, which the WideString's destructor
then tries to free again. It would be better to pass the WideString itself
instead of the BSTR that it contains, as OleFunction() will make a copy of
the BSTR and then take ownership of that copy, and WideString will maintain
ownership of the original BSTR.
Variant xml = Variant::CreateObject("Microsoft.XMLDOM");
xml.OlePropertySet("async", VARIANT_FALSE);
WideString path = "D:\\temp\\12345\\1.xml";
Variant t = xml.OleFunction("load", path);
if( t )
ShowMessage("Load ok");
else
ShowMessage("Load False");
> However from COM specification, not it should use wchar_t
> (OLECHAR) instread of char ? why it can success ??
Using a char* is fine because OleFunction() will internally convert the
value to a BSTR, which it then takes ownership of.