Link to home
Start Free TrialLog in
Avatar of aspdoc
aspdoc

asked on

Thread in Dll's

I need to develop one DLL with Thread method.  
This DLL will be used to a client side for access to a server in TCP/IP.  
In the same machine one or more applications must be to access the DLL, in a concept of three layers : application  - is the front-end to the end-user  
            DLL contain business rules and  
           server via TCP/IP.  
 
I would like to know a basic code for to begin the library with Thread method.  
If is possible, I need that DLL run in Windows and in Linux (the same code, but different compilations D7 and K3).  
 
Thanks ind advance
Avatar of DaFox
DaFox

Hi,

I would make use of the Win32API only in this case. Sth. like this:

library ThreadDLL;

uses
  Windows,
  Messages;

function ThreadFunc(Param: Pointer): DWORD; stdcall;
begin
  // place your code here !
end;

var
  hThread: THandle;
  ThreadID: DWord;

function start(): LongBool; stdcall;
begin
  if (hThread = 0) then
    hThread := CreateThread(nil, 0, @ThreadFunc, nil, 0, ThreadID);
  Result := hThread <> 0;
end;

procedure stop(); stdcall;
var
  hThreadExitCode: DWORD;
begin
  if hThread <> 0 then
  begin
    if GetExitCodeThread(hThread, hThreadExitCode) and (hThreadExitCode = STILL_ACTIVE) then
      TerminateThread(hThread, 0);
    CloseHandle(hThread);
  end;
  hThread := 0;
end;

procedure LibraryProc(Reason: Integer);
begin
  case Reason of
    DLL_PROCESS_ATTACH:
      begin
        DisableThreadLibraryCalls(HInstance);
        start;
      end;
    DLL_PROCESS_DETACH:
      stop;
  end;
end;

begin
  DllProc := @LibraryProc;
  LibraryProc(DLL_PROCESS_ATTACH);
end.

Regards,
Markus
ASKER CERTIFIED SOLUTION
Avatar of Lee_Nover
Lee_Nover

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Hi,

I can't understand why you need a thread. All apps using that dll will have their own copy of that dll and can use its exported functions when they need them instead of developing some kind of interthreading communication (between VCL threads of the apps and the DLL thread). DLL functions simply encapsulate and offer services to the calling apps. The same could be done using COM/DCOM.

Regards, Geo
Avatar of aspdoc

ASKER

Thank you, Lee_Nover

But :

a) Could you say that way to execute the thread ?  
b) That way I can come back the result of the call?
a) - sorry .. I don't understand the question
b) the point in working threads is you leave them running in the background and don't wait for the result
  you will be notified when the thread finishes .. that's what the AOnDone param is for
  it's a pointer to a procedure that will be called when the thread finishes
ofcourse you can change the parameter list of all the functions .. my code is merely a simple example

enjoy threading ;-)
Avatar of aspdoc

ASKER

Hi Geo  
 
Thanks for your comment.  
 
I will try to explain my possible problem.  
 
a) I intend to use the same code in D7 and K3;  
 
b) in Win32 the DLL is called from the IIS and by apps from end-users;  
 
c) in Linux the SO is called from the Apache and  by apps from end-users;  
 
d) in Win32 the ISS call the DLL and load to the memory a new copy of it.  
 
Question :  
 
The Apache Server running under Linux have the same way of the ISS under Win32 ( a new copy of  SO every each request from the browser ?  
 
If the answer is yes, no problem. I know enough to develop a single DLL or SO without thread.  
But, if is no, I need to develop a solution that work with multithreading.  
 
Regards,  Aspdoc