> Is it correct that there is a limit to the BSTR size
> that can be passed and returned
A BSTR has a 4-byte length value associated with it. In theory, it is
limited to (4+(sizeof(wchar_t)*2)+2 bytes of memory. In practice, since not
many people have 4+GB of RAM memory available, it is limited by available
memory.
> or just a problem in my code
What does your actual code look like? What is the actual problem?
> I'm trying to return large text files.
How large exactly? What exactly are you trying to accomplish by passing the
actual text content via interfaces?
Gambit
WideString temp;
temp=StringList1->Text;
OutStr=(BSTR)temp; // used in a function call F(BSTR Inkster, BSTR& OutStr)
Here is the code in the client:
WideString wstrData(MemoOut->Text);
BSTR bstrData=(BSTR)wstrData;
BSTR bstrResult;
bstrResult = FM.Function1(bstrData);
/*
Function1 defined in the library:
HRESULT _stdcall Function1([in] BSTR StrIn, [out, retval] BSTR *
StrOut );
*/
WideString wstrResult;
wstrResult.Attach(bstrResult);
MemoIn->Lines->Add(String(wstrResult)); //here I get access violation in
vcl50.bpl when the original ASCII text larger then 25kB.
wstrResult.Detach();
Second option:
MemoIn->Lines->Add(bstrResult); //here I get access vilolation in
kernell32.dll with files larger than about 25kB.
MemoIn->Text=bstrResult;
Both code versions work with smaller than 20kB ASCII text with no problem.
Thanks again,
Alex
"Remy Lebeau (TeamB)" <gamb...@yahoo.com> wrote in message
news:3ed7...@newsgroups.borland.com...
> OutStr=(BSTR)temp;
Here is the cause of your error. You need to Detach() ownership of the BSTR
in that last statement:
OutStr = temp.Detach();
Or, you can combine all three statements to simply this:
OutStr = WideString(StringList1->Text).Detach();
Either way, because the OutStr parameter is marked as [out], it is the
responsibility of Function1() to allocate the BSTR memory and then pass
ownership of the memory to the caller. It then becomes the caller's
responsibility to free the BSTR when it is finished using it.
However, in your code, you are not releasing ownership of the BSTR, so it is
still attached to the WideString when the function returns. When the
WideString goes out of scope, the BSTR will be freed automatically, and the
caller will be left working with invalid memory. That is why you are
getting a runtime error. That fact that the code works with BSTR values up
to 25K is irrelevant, because the behavior was undefined as soon as the
memory became invalid. Anything could have happened.
> WideString wstrData(MemoOut->Text);
> BSTR bstrData=(BSTR)wstrData;
You don't need to use a separate BSTR variable, you can pass the WideString
directly:
bstrResult = FM.Function1(wsaData);
Or simply just:
bstrResult = FM.Function1(WideString(MemoOut->Text));
> wstrResult.Attach(bstrResult);
> MemoIn->Lines->Add(String(wstrResult));
Here, because the WideString is attached to invalid memory, you got the
runtime error.
> wstrResult.Detach();
Do not Detach() the BSTR returned by Function1(). It is the code's
responsibility to free it. Leave it attached to the WideString so it will
take care of freeing it automatically for you. Otherwise, you'll have a
memory leak (assuming you Detach() the original memory n Function1() so it
remains valid for use).
Gambit
"Remy Lebeau (TeamB)" <gamb...@yahoo.com> wrote in message
news:3ed852f2$1...@newsgroups.borland.com...