Recent

Author Topic: How to build DLL Files for Windows Desktop ?  (Read 5266 times)

paule32

  • Sr. Member
  • ****
  • Posts: 280
How to build DLL Files for Windows Desktop ?
« on: February 26, 2024, 09:39:57 pm »
I would form a DLL by using the follpwing command:

C:\lazarus\x86_64\fpc\3.2.2\bin\x86_64-win64\fpc.exe -Twin64 -Mdelphi -dwindows -dwin64 -v0 -dwindll -O2 -Os -vl -Anasmwin64 -al -Fu..\units\fpc-rtl -Fu..\units\fpc-sys -Fu..\units\fpc-win -Fu..\units\fpc-qt -Fu.\units -FE.\units test2.pas

I get the .DLL, but, when I run the host Application, that involve the DLL (member function as external declaration) I get OS Error Message 0xc0007b.

I use the same command, to build an Executable - and there, I don't get OS Error Messages.

Give it special commands für building .DLL ?

I know the difference between "program, library, and unit" in the first Line of Code.

rvk

  • Hero Member
  • *****
  • Posts: 6658
Re: How to build DLL Files for Windows Desktop ?
« Reply #1 on: February 26, 2024, 09:46:01 pm »
I get the .DLL, but, when I run the host Application, that involve the DLL (member function as external declaration) I get OS Error Message 0xc0007b.
Show the external declaration and the code of the host app calling it.

marcov

  • Administrator
  • Hero Member
  • *
  • Posts: 12025
  • FPC developer.
Re: How to build DLL Files for Windows Desktop ?
« Reply #2 on: February 26, 2024, 09:51:33 pm »
Why do you add -Anasmwin64 ?

paule32

  • Sr. Member
  • ****
  • Posts: 280
Re: How to build DLL Files for Windows Desktop ?
« Reply #3 on: February 26, 2024, 09:51:44 pm »
Code: Pascal  [Select][+][-]
  1. {$mode delphi}
  2. program test1;
  3.  
  4. procedure zum; stdcall; external 'test2.dll' name 'test';
  5. procedure test;
  6. begin
  7.   MessageBox(0,'halllo','toitt',0);
  8.   zum;
  9. end;
  10.  
  11. begin
  12.   test;
  13. end.

paule32

  • Sr. Member
  • ****
  • Posts: 280
Re: How to build DLL Files for Windows Desktop ?
« Reply #4 on: February 26, 2024, 09:52:45 pm »
@marcov

not related - only to see the Assembly Output.

rvk

  • Hero Member
  • *****
  • Posts: 6658
Re: How to build DLL Files for Windows Desktop ?
« Reply #5 on: February 26, 2024, 09:57:57 pm »
And how does the code for the dll look like?

And when do you get the error?
When calling (after the messagebox} or at the start of the host program?

You have name 'test' but procedure zum. Why the rename?

paule32

  • Sr. Member
  • ****
  • Posts: 280
Re: How to build DLL Files for Windows Desktop ?
« Reply #6 on: February 26, 2024, 10:04:22 pm »
Code: Pascal  [Select][+][-]
  1. {$mode delphi}
  2. library test2;
  3.  
  4. procedure test; export;
  5. begin
  6.     MessageBox(0,'zuzu','foo',0);
  7. end;
  8.  
  9. exports test;
  10.  
  11. end.

I get the Error after starting the Host Application.
When I use MessageBox only (without DLL code (direct or in-direct), I get no Error (only Code in Exe).

Why rename?
To test if it possible.

marcov

  • Administrator
  • Hero Member
  • *
  • Posts: 12025
  • FPC developer.
Re: How to build DLL Files for Windows Desktop ?
« Reply #7 on: February 26, 2024, 10:09:16 pm »
Doesn't compiler here, tested

3.2.2 win32
3.3.1 win32
3.3.1 win64

All seem to miss messagebox (missing uses windows ?)

If I add that it works for me with only -Sd as parameter, but I only have 3.3.1 for 64-bit.

paule32

  • Sr. Member
  • ****
  • Posts: 280
Re: How to build DLL Files for Windows Desktop ?
« Reply #8 on: February 26, 2024, 10:17:04 pm »
take a look to:
Code: Pascal  [Select][+][-]
  1. https://github.com/paule32/Qt_FPC/tree/main/src/tests

and the Rest of the Source Codes.

440bx

  • Hero Member
  • *****
  • Posts: 4986
Re: How to build DLL Files for Windows Desktop ?
« Reply #9 on: February 26, 2024, 10:20:26 pm »
Doesn't compiler here, tested
That was my thought too.  How could that code even compile given there is no resolution for MessageBox.


I get the Error after starting the Host Application.
That does not sound possible.  As stated above, the code you presented cannot even compile.

Add "uses Windows;" in your library code.  After that, not only it will compile, it might even run as expected.

(FPC v3.0.4 and Lazarus 1.8.2) or (FPC v3.2.2 and Lazarus v3.2) on Windows 7 SP1 64bit.


marcov

  • Administrator
  • Hero Member
  • *
  • Posts: 12025
  • FPC developer.
Re: How to build DLL Files for Windows Desktop ?
« Reply #11 on: February 26, 2024, 10:32:03 pm »
Ok, so custom RTL stuff. Many things changing at once.

Can't really tell easily, only thing I can recommend is to try to compile with -Xe, just in case nasm produces something the internal linker can't handle.

rvk

  • Hero Member
  • *****
  • Posts: 6658
Re: How to build DLL Files for Windows Desktop ?
« Reply #12 on: February 26, 2024, 10:35:03 pm »
I can replicate it with your custom FPC source.

But you might want to mention in EVERY following topic of yours, that you work with an custom FPC source.

440bx

  • Hero Member
  • *****
  • Posts: 4986
Re: How to build DLL Files for Windows Desktop ?
« Reply #13 on: February 26, 2024, 10:38:21 pm »
I must be missing something...

I've looked at your code and my impression is that you're making your life way more complicated than it needs to be.

Why don't you simply use the "Windows" unit instead of creating your own unit ? are you trying to write your own RTL ? is that what you're trying to do ?

In addition to that, you're re-implementing in assembler routines that not only are already part of the FPC system but, in addition to that, are already present in some  of the Windows dlls as well.  Is there any reason why you're re-inventing the wheel ?  if you're trying to write your own RTL and, you know the target is exclusively Windows then, you can use low level routines that are already present (not to mention well  optimized) in kernel32 and ntdll (such as the memory functions/procedures.)
(FPC v3.0.4 and Lazarus 1.8.2) or (FPC v3.2.2 and Lazarus v3.2) on Windows 7 SP1 64bit.

paule32

  • Sr. Member
  • ****
  • Posts: 280
Re: How to build DLL Files for Windows Desktop ?
« Reply #14 on: February 26, 2024, 10:47:01 pm »
@440bx
I try to modulization of FPC Components.
But not the Question.

The Question from me is: How to Create DLL Files - with or without API Level Member Functions.

If you comment in the "external" line, and the "zum" Line, then you can realize that the Host Application does work.
But than, the EXEcutable is static.

So my guess is, that FPC does not Handle the EXPORTS keyword correctly.

Because, when I use LoadLibrary/FreeLibrary, the test2.dll seems to be load (see test1.pas)

 

TinyPortal © 2005-2018