Delphi DLL - TClientSocket events
I have a DLL with a TClientSocket component, it is used to talk to a Telephone System Machine. The DLL only have PChar parameters in the exports methods, and is not using packages.
When I load the DLL with Delphi app, all t开发者_Python百科he events works fine, no problem so far.
My customer is calling this DLL from a console Win32 Cobol program, and the TClientSocket do not trigger the events when its happen, it uses a main loop to call a check method in DLL to known if is there any returning from the Telephone system, if it returns OK then it call the Get Method, and here is where the problem happen:
In TClientSocket.OnRead event, I call TClientSocket.Socket.ReceiveText, and there are several returns from server app, what make me think that the event is only triggered when I call a method from DLL, and the TClientSocket is holding several returns in the buffer.
The problem is that I cant find any Delimiter to split this Return.
How can I fix this? Is there anything I can add to my DLL to make sure that the OnRead event will be triggered every time when it is not called from a Delphi Program?
you probably need a message loop in your dll .. (Console applications lack a message pump ..). SO implement something like this in your dll constructor:
var Msg : TMsg;
res : Integer;
.
.
.
While true Do Begin
res := Integer( GetMessage(Msg, 0, 0, 0 ));
If res = -1 Then
Exit // error
else if res = 0 then
exit // WM_QUIT received
else begin
TranslateMessage( Msg );
DispatchMessage( Msg );
end;
End; { While }
Take a look at a similar thread http://www.mofeel.net/1102-comp-lang-pascal-delphi-misc/2763.aspx
recently , i encountered a similar problem as you , my clientsocket in dll works ok with delphi-exe, but not with c-console exe , and i remembered tclientsocket
is using select-event mode, which needs the main-thread to process the message loop, so ,
if your are using a tclientsocket with nonblock mode in a dll, the host should NEVER block the main-thread, and must do the message-loop (for ex, when calling in a console program).
sometimes we can not modify the host code (the case i meet), then we can do like this
socket.sendtext();
repeat s :=socket.recevtext;
until timeout or length(s)>0;
of course you need to check if s is the complete packet or so.
精彩评论