Hi, All

Ok - first the good news... despite tearing out some hair and losing a good
few hours of sleep, my new TWSocket-based DLL with the worker thread is
working brilliantly :-)

Now for the bad news... I am getting some strange and intermittent
"corruption" of function return values.

I'm not sure if this is an issue related specifically to D7 but I've
experienced it calling an exported function within the DLL (resolved by not
exporting it) and now from my calling application (written in C++).

The problem only seems to arise when there is conversion between AnsiString
and PAnsiChar types in an exported function so I'm wondering if anyone else
has seen this behaviour and/or has a solution because I certainly can't see
what's wrong.

Essentially, the DLL has a global TStringList which I'm using as a message
buffer. The worker thread adds new lines to the end of the TStringList as
they are received from the server because the burst rate is far higher than
the calling application can process them. The calling application then
simply requests the 1st message in the list when it's ready for it and that
entry is then deleted from the TStringList so that all incoming messages get
processed in sequence.

The actual code I'm using is here:

function ReadMessage: PAnsiChar; stdcall;
begin
  EnterCriticalSection(CritSectn);

  if (NotesList.Count > 0) then
  begin
    Result := PAnsiChar(NotesList.Strings[0]);
    NotesList.Delete(0);
  end
  else
    Result := '';

  OutputDebugStringA(Result);

  LeaveCriticalSection(CritSectn);
end;


Now, a typical entry added to the list would be something like (without the
quotes) "MESSAGE:Hello World!" or just "MESSAGE:".

The correct strings do seem to be getting added to the list as I'm
monitoring the messages using DebugView but, my calling application is
getting some return values truncated with one or two unprintable characters
after the truncated text. For example, "MESSAGE:" might be received as
"MESSAG<"

Here is a live example taken from the application log - the actual message
logged by the DLL (so should be the content of the TStringList entry) is
"TRADE:" but the calling application received "TRAD"

I've tried playing around with the return type i.e. PChar / PAnsiChar /
PWideChar but it makes no difference

Hopefully someone can provide a little insight as it's driving me crazy.

I'd also be grateful if anyone could confirm whether I actually need the
critical section code or not... I thought I might as the worker thread could
be trying to add a new string as the main thread was deleting string [0] but
I read a note somewhere on a Delphi site that TStringLists were thread-safe
so I'm a bit confused about it.

Regards, Adam


--
To unsubscribe or change your settings for TWSocket mailing list
please goto http://lists.elists.org/cgi-bin/mailman/listinfo/twsocket
Visit our website at http://www.overbyte.be

Reply via email to