Solved

Thread in Dll's

Posted on 2003-11-10
6
568 Views
Last Modified: 2010-08-05
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
0
Comment
Question by:aspdoc
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
6 Comments
 
LVL 6

Expert Comment

by:DaFox
ID: 9717845
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
0
 
LVL 12

Accepted Solution

by:
Lee_Nover earned 250 total points
ID: 9720453
this is not crossplatform
instead simply use TThread descendants
here's a small example: http://leenover.homeip.net/isapi/pas2html.dll/pas2html?File=/delphi/ThdDll/
you simply call the exported function with some params
you could also create a list of managed threads (somekind of a thread pool)
depends on what you wanna do
0
 
LVL 17

Expert Comment

by:geobul
ID: 9721443
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
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:aspdoc
ID: 9725291
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?
0
 
LVL 12

Expert Comment

by:Lee_Nover
ID: 9726031
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 ;-)
0
 

Author Comment

by:aspdoc
ID: 9731063
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
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
find a node in VST 2 89
JAudiorecorder record freezing the app 29 89
How to insert .png images to TClientDataSet .cds file? 2 80
Using MMsystem To change audio input 1 35
The uses clause is one of those things that just tends to grow and grow. Most of the time this is in the main form, as it's from this form that all others are called. If you have a big application (including many forms), the uses clause in the in…
Introduction I have seen many questions in this Delphi topic area where queries in threads are needed or suggested. I know bumped into a similar need. This article will address some of the concepts when dealing with a multithreaded delphi database…
Attackers love to prey on accounts that have privileges. Reducing privileged accounts and protecting privileged accounts therefore is paramount. Users, groups, and service accounts need to be protected to help protect the entire Active Directory …

734 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question