Delphi - working with dll's for beginners

2019-07-27 22:22发布

问题:

I'm a total newbie regarding to DLL. And I don't need to creat them I just need to use one. I've read some tutorials, but they weren't as helpful as I hoped.

Here's the way I started: I've downloaded the SDK which I need to use (ESTOS Tapi Server). I read in the docs and spotted out the DLL which I need to use, which is the ENetSN.dll, and so I registered it.

Next I've used the Dependency Walker to take a look at the DLL - and I was wondering because there are only these functions: DllCanUnloadNow, DllGetClassObject, DllRegisterServer and DllUnregisterServer, and these are not the functions mentioned in the docs.

I think I have to call DllGetClassObject to get an object out of the DLL with which I can start to work. Unfortunately the tutorials I found doesn't mentioned how this is done (or I didn't understood it).

There are also 3 exmaples delivered for VB and C++, but I wasn't able to 'translate' them into delphi.

If somebody knows a tutorial where this is explained or could give me a pointer to the right direcetion, I would be very thankful .

回答1:

The the 3 exported functions indicate that its a COM/ActiveX DLL, If you have registered it with a bit of luck you can get at it via Project->Import Type Library.



回答2:

Forget about the library being a DLL. Judging from the fact that you had to register the DLL and from the functions it exports, it's a COM/OLE/ActiveX-library. For these you don't care about them being DLLs. Instead you work with them by creating instances of the COM-classes contained therein. Lookup CreateComObject, CreateAutoObject and similar methods. When using the type library importer (see Alex K's post) you might even get a couple of (likely non-visual) components to work with.



回答3:

You may want to check out Delphi 2010's new feature of delayed loading libraries (DLLs). See http://www.drbob42.com/examines/examinC1.htm for an article and more details.



回答4:

I wrote a tutorial on Delphi DLLs many years ago, you can find it on DLLs made simple. I don't know if it will be of any use since I haven't worked properly with Delphi for a while, but I can say that it will help to explain the basics to a total beginner. If it is useless, let me know and I'll remove this "answer"...



回答5:

If you want to call the Dll Procedure or function in your application, then follow this process

procedure TForm1.DllBtnClick(Sender: TObject);

  {Step: 1 To call DLL procedure/function from your program, you first have to 
  declare a type that describes the procedure:}

type
  TSayHello = procedure(pParam: Parameters in the Dll Procedure); 
var
  DLLInstance : THandle;
  SayHello : TSayHello;
begin

  {Step 2: You must load the library}

  DLLInstance := LoadLibrary(`c:\Mydll.dll');
  if DLLInstance = 0 then begin
    MessageDlg(`Unable to load DLL.', mtError, [mbOK], 0);
    Exit;
  end;

  {Step : 3 Get the address of the procedure using GetProcAddress. }

  @SayHello := GetProcAddress(DLLInstance, 'SayHello');
  if @SayHello <> nil then

  {Step: 4 Call your function}

    SayHello(Self)  
  else
    MessageDlg(`Unable to locate procedure.', mtError, [mbOK], 0);

  {Step: 5 Free the Dll}

  FreeLibrary(DLLInstance);
end;


回答6:

NirSoft's DLL export viewer (http://www.nirsoft.net/utils/dll_export_viewer.html) is capable of listing COM DLL methods.



标签: delphi dll