Solved

Thread in Dll's

Posted on 2003-11-10
6
559 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

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 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…
This Micro Tutorial will give you a basic overview how to record your screen with Microsoft Expression Encoder. This program is still free and open for the public to download. This will be demonstrated using Microsoft Expression Encoder 4.
In this video I am going to show you how to back up and restore Office 365 mailboxes using CodeTwo Backup for Office 365. Learn more about the tool used in this video here: http://www.codetwo.com/backup-for-office-365/ (http://www.codetwo.com/ba…

920 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

15 Experts available now in Live!

Get 1:1 Help Now