1

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 the 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?

Cesar Romero
  • 4,027
  • 1
  • 25
  • 44

2 Answers2

2

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

G-Man
  • 7,232
  • 18
  • 72
  • 100
  • I did that in the DLL main procedure and the first interaction the DLL stop in GetMessage, and dont move forward. – Cesar Romero Jul 01 '10 at 16:26
  • Now I know GetMessage wait until it get a message, so I change to PeekMessage, but it returns -1, so it goes out. if I remove the test then it never load the application. – Cesar Romero Jul 01 '10 at 17:09
  • Cesar, maybe the culprit is the COBOL app see another similar thread (it is .net but seems to be dealing with the same issue) http://www.devnewsgroups.net/windowsforms/t10952-messageloop-thread.aspx – G-Man Jul 01 '10 at 18:22
  • GX, I think the solution will be move the TClientSocket to a thread inside DLL and then add the loop to the execute method. Now Im working in a parser to make the current version works, and Ill start a new version using threads. Ill post the result here when it is done. Thank you for your help so far. – Cesar Romero Jul 05 '10 at 11:53
0

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.

Harald Brinkhof
  • 4,375
  • 1
  • 22
  • 32
melice
  • 1