Solved

Thread in Dll's

Posted on 2003-11-10
6
555 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
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
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 

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

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Intraweb submit form as a POST request 4 229
APK file name 7 64
How to use Hashing sha1 in Delphi 2010 4 162
LAN or WAN ? 11 63
Objective: - This article will help user in how to convert their numeric value become words. How to use 1. You can copy this code in your Unit as function 2. than you can perform your function by type this code The Code   (CODE) The Im…
Introduction The parallel port is a very commonly known port, it was widely used to connect a printer to the PC, if you look at the back of your computer, for those who don't have newer computers, there will be a port with 25 pins and a small print…
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…

760 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

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now