Recent

Author Topic: How to: create DLL file for Windows 10 64-Bit Pro  (Read 33120 times)

paule32

  • Sr. Member
  • ****
  • Posts: 280
Re: How to: create DLL file for Windows 10 64-Bit Pro
« Reply #15 on: April 08, 2024, 09:02:26 am »
okay, I get it.
I used batch scripts to automate the compile stage.
There was a typo, that build an error, and the rest of compile was sitting on this error, and produced error on errors, ...

Sorry for my crying.

paule32

  • Sr. Member
  • ****
  • Posts: 280
Re: How to: create DLL file for Windows 10 64-Bit Pro
« Reply #16 on: April 08, 2024, 05:47:17 pm »
next problem:

Code: Text  [Select][+][-]
  1. E:\Projekte\fpc-qt\src\tests\units>ppas.bat
  2. Assembling system
  3. Assembling qt_string
  4. Assembling fpc_rtl
  5. Linking E:\Projekte\fpc-qt\src\tests\units\fpc_rtl.dll
  6. E:\FPC\3.2.0\bin\i386-win32\dlltool.exe: Path components stripped from dllname, 'E:\Projekte\fpc-qt\src\tests\units\fpc_rtl.dll'.
  7. exp.$$$:fake:(.edata+0x34): undefined reference to `_SYSTEM_$$_MOVE$formal$formal$LONGDWORD
  8. An error occurred while linking E:\Projekte\fpc-qt\src\tests\units\fpc_rtl.dll
  9.  
move is declared as:

Code: Pascal  [Select][+][-]
  1. // interface part
  2. {$ifdef winexe}
  3. const rtl_dll = 'fpc_rtl.dll';
  4. procedure move(const source; var dest; count: LongDWord); assembler; stdcall; external rtl_dll name 'move';
  5. {$endif}
  6. {$ifdef windll}
  7. procedure move(const source; var dest; count: LongDWord); assembler; stdcall; public name 'move';
  8. {$endif}
  9.  
  10. // implementation part
  11.  
  12. {$ifdef windll}
  13. procedure move(const source; var dest; count: LongDWord); assembler; nostackframe; stdcall;
  14. asm
  15. ...
  16. end;
  17. {$endif}

How to fix this ?

paule32

  • Sr. Member
  • ****
  • Posts: 280
Re: How to: create DLL file for Windows 10 64-Bit Pro
« Reply #17 on: April 08, 2024, 10:17:27 pm »
In cross posting of:
https://forum.lazarus.freepascal.org/index.php/topic,66908.0.html

I asked for, why the .dll has wrong format.
I dont know how to give a option the linker that tell him, to create a well formed image.

I thinking @kodezwerg  has something write to this issue...
For answers, please use this Thread - Thank you
« Last Edit: April 12, 2024, 04:52:44 pm by paule32 »

paule32

  • Sr. Member
  • ****
  • Posts: 280
Re: How to: create DLL file for Windows 10 64-Bit Pro
« Reply #18 on: April 12, 2024, 06:39:22 pm »
now, I added a little PowerShell GUI Script, which helps the developer (me) you,
to click a Icon on the Explorer instead going into the command line console implicitly.

The file path's are all relative, and optimized for my developer system.
Feel free and drop a message if you have question's.

paule32

  • Sr. Member
  • ****
  • Posts: 280
Re: How to: create DLL file for Windows 10 64-Bit Pro
« Reply #19 on: April 13, 2024, 04:56:45 pm »
when I start debugger, I get this:

Code: Bash  [Select][+][-]
  1. # pacman -S mingw-w64-x86_64-gdb-multiarch
  2.  
  3. packages (1) mingw-w64-x86_64-gdb-multiarch-13.2-3
  4.  
  5. total size of pakage:                      30,05 MiB
  6. total difference after install:             1,03 MiB
  7.  
  8. :: continue installation ? [Y/n] y
  9. (1/1) check keys of bundle                 [#####################] 100%
  10. (1/1) check package intigration            [#####################] 100%
  11. (1/1) package files was found              [#####################] 100%
  12. (1/1) check for conflicts                  [#####################] 100%
  13. (1/1) check available disk space...        [#####################] 100%
  14. :: process package data …
  15. (1/1) update of mingw-w64-x86_64-gdb-m...  [#####################] 100%
  16.  
  17. #
  18.  
  19. # pacman -S python3
  20.  
  21. # gdb -n
  22. Python path configuration:
  23.   PYTHONHOME = 'E/Python310'
  24.   PYTHONPATH = 'E:\Python310'
  25.   program name = 'e:/msys64/mingw64/bin/python.exe'
  26.   isolated = 0
  27.   environment = 1
  28.   user site = 1
  29.   safe_path = 0
  30.   import site = 1
  31.   is in build tree = 0
  32.   stdlib dir = 'E/Python310/lib/python3.11'
  33.   sys._base_executable = 'e:/msys64/mingw64/bin/python.exe'
  34.   sys.base_prefix = 'E/Python310'
  35.   sys.base_exec_prefix = 'E/Python310'
  36.   sys.platlibdir = 'lib'
  37.   sys.executable = 'e:/msys64/mingw64/bin/python.exe'
  38.   sys.prefix = 'E/Python310'
  39.   sys.exec_prefix = 'E/Python310'
  40.   sys.path = [
  41.     'E:/Python310',
  42.     'E/Python310/lib/python311.zip',
  43.     'E/Python310/lib/python3.11',
  44.     'E/Python310/lib/python3.11/lib-dynload',
  45.   ]
  46. Error occurred computing Python errormessage.
  47. Python not initialized
  48.  
  49. # export PYTHONHOME=
  50.  
  51. # gdb -n
  52. GNU gdb (GDB) 13.2
  53. Copyright (C) 2023 Free Software Foundation, Inc.
  54. License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
  55. This is free software: you are free to change and redistribute it.
  56. There is NO WARRANTY, to the extent permitted by law.
  57. Type "show copying" and "show warranty" for details.
  58. This GDB was configured as "x86_64-w64-mingw32".
  59. Type "show configuration" for configuration details.
  60. For bug reporting instructions, please see:
  61. <https://www.gnu.org/software/gdb/bugs/>.
  62. Find the GDB manual and other documentation resources online at:
  63.     <http://www.gnu.org/software/gdb/documentation/>.
  64.  
  65. For help, type "help".
  66. Type "apropos word" to search for commands related to "word".
  67. (gdb) q
  68.  
  69. #

when I start strace, I get this:

Code: Bash  [Select][+][-]
  1. # strace ./test1.exe
  2. --- Process 22124 created
  3. --- Process 22124 loaded C:\Windows\System32\ntdll.dll at 00007ffc82c00000
  4. --- Process 22124 loaded C:\Windows\System32\kernel32.dll at 00007ffc82030000
  5. --- Process 22124 loaded C:\Windows\System32\KernelBase.dll at 00007ffc804b0000
  6. --- Process 22124 loaded C:\Windows\System32\user32.dll at 00007ffc820f0000
  7. --- Process 22124 loaded C:\Windows\System32\win32u.dll at 00007ffc80a20000
  8. --- Process 22124 thread 5296 created
  9. --- Process 22124 loaded C:\Windows\System32\gdi32.dll at 00007ffc81bd0000
  10. --- Process 22124 loaded C:\Windows\System32\gdi32full.dll at 00007ffc80270000
  11. --- Process 22124 loaded C:\Windows\System32\msvcp_win.dll at 00007ffc80840000
  12. --- Process 22124 loaded C:\Windows\System32\ucrtbase.dll at 00007ffc80390000
  13. --- Process 22124 thread 10168 created
  14. --- Process 22124 thread 21296 created
  15. --- Process 22124 loaded E:\Projekte\fpc-qt\src\tests\fpc_rtl.dll at 00007ffc789d0000
  16. --- Process 22124, exception c0000005 at 00007ffc82c3cba3
  17. --- Process 22124 thread 10168 exited with status 0xc0000005
  18. --- Process 22124 thread 5296 exited with status 0xc0000005
  19. --- Process 22124 thread 21296 exited with status 0xc0000005
  20. --- Process 22124 exited with status 0xc0000005
  21. Segmentation fault

when I start the gdb debugger, I get this (with back trace):

Code: Text  [Select][+][-]
  1. E:\msys64\mingw64\bin\gdb.exe E:\Projekte\fpc-qt\src\tests\test1.exe
  2. GNU gdb (GDB) 13.2
  3. Copyright (C) 2023 Free Software Foundation, Inc.
  4. License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
  5. This is free software: you are free to change and redistribute it.
  6. There is NO WARRANTY, to the extent permitted by law.
  7. Type "show copying" and "show warranty" for details.
  8. This GDB was configured as "x86_64-w64-mingw32".
  9. Type "show configuration" for configuration details.
  10. For bug reporting instructions, please see:
  11. <https://www.gnu.org/software/gdb/bugs/>.
  12. Find the GDB manual and other documentation resources online at:
  13.     <http://www.gnu.org/software/gdb/documentation/>.
  14.  
  15. For help, type "help".
  16. Type "apropos word" to search for commands related to "word"...
  17. Reading symbols from E:\Projekte\fpc-qt\src\tests\test1.exe...
  18. (No debugging symbols found in E:\Projekte\fpc-qt\src\tests\test1.exe)
  19. (gdb) r
  20. Starting program: E:\Projekte\fpc-qt\src\tests\test1.exe
  21. [New Thread 17316.0x38b4]
  22. [New Thread 17316.0x2be0]
  23. [New Thread 17316.0x58f4]
  24.  
  25. Thread 4 received signal SIGSEGV, Segmentation fault.
  26. 0x00007ffc82c3cba3 in ntdll!RtlLeaveCriticalSection () from C:\Windows\SYSTEM32\ntdll.dll
  27. (gdb) bt
  28. #0  0x00007ffc82c3cba3 in ntdll!RtlLeaveCriticalSection () from C:\Windows\SYSTEM32\ntdll.dll
  29. #1  0x00007ffc82c510a3 in ntdll!RtlGetActiveActivationContext () from C:\Windows\SYSTEM32\ntdll.dll
  30. #2  0x00007ffc82c5101e in ntdll!RtlGetActiveActivationContext () from C:\Windows\SYSTEM32\ntdll.dll
  31. #3  0x00007ffc82c69b7a in ntdll!TpCallbackIndependent () from C:\Windows\SYSTEM32\ntdll.dll
  32. #4  0x00007ffc82c17086 in ntdll!EtwNotificationRegister () from C:\Windows\SYSTEM32\ntdll.dll
  33. #5  0x00007ffc820453e0 in KERNEL32!BaseThreadInitThunk () from C:\Windows\System32\kernel32.dll
  34. #6  0x00007ffc82c0485b in ntdll!RtlUserThreadStart () from C:\Windows\SYSTEM32\ntdll.dll
  35. #7  0x0000000000000000 in ?? ()
  36. (gdb)

So, I thinking, the image or the tools I used does not fit together.

paule32

  • Sr. Member
  • ****
  • Posts: 280
Re: How to: create DLL file for Windows 10 64-Bit Pro
« Reply #20 on: April 14, 2024, 03:25:21 am »
I got it.
Windows Error 0xc0000005
This means, that the application tries to access memory area, that is not available/access able/reserved for it.

I commented all my test lines, and the error fly away.
So I have check the code for the libraries.

paule32

  • Sr. Member
  • ****
  • Posts: 280
Re: How to: create DLL file for Windows 10 64-Bit Pro
« Reply #21 on: April 14, 2024, 09:46:25 am »
I did some research, and found out, that PASCALMAIN is the third entry, at running step.
I can give the parameter -XM to define a binary entry point.
But someone is wrong with the FPC imp<lib>.a files.
When I use import's from such files, I get 0xc0005.

How can I add/change the start symbol by using IDA Free ?

I can stream out assembly to file.
Which assembler can do compile this code again - or have I to do reformat it ?

In the attachment's, some information's screen shot's.

paule32

  • Sr. Member
  • ****
  • Posts: 280
Re: How to: create DLL file for Windows 10 64-Bit Pro
« Reply #22 on: April 14, 2024, 09:04:04 pm »
Hello,

now, I tried the following code bellow:
The DLL file will be loaded, and the library code entry will be called from inside the DLL without error's.
But the TestTest function display nothing.

Code: Pascal  [Select][+][-]
  1. {$mode delphi}
  2. program test1;
  3.  
  4. function LoadLibrary( lpLibFileName: LPCSTR ): HMODULE; stdcall; external 'kernel32.dll' name 'LoadLibraryA';
  5. function FreeLibrary( hLibModule: HMODULE ): BOOL;      stdcall; external 'kernel32.dll' name 'FreeLibrary';
  6.  
  7. function GetProcAddress(modulname: HMODULE; lpProcName: LPCSTR): FARPROC; stdcall; external 'kernel32.dll' name 'GetProcAddress';
  8.  
  9. procedure Entry; stdcall; public name '_mainCRTStartup';
  10. var
  11.     s1, s2: String;
  12.     p1, p2: PChar;
  13.     hm: HMODULE;
  14.     ap: procedure;
  15. begin
  16.     s1 := 'mufo   Lo aalo';
  17.     s2 := 'Hello World  --> ' + s1;
  18.     MessageBox(0,s2,s1,0);
  19.  
  20.     HM := LoadLibrary('fpc_rtl.dll');
  21.       ap := GetProcAddress(hm, 'TestTest');
  22.       ap ;
  23.     FreeLibrary(HM);
  24.    
  25.     ExitProcess(0);
  26. end;
  27.  
  28. begin
  29. end.

rvk

  • Hero Member
  • *****
  • Posts: 6572
Re: How to: create DLL file for Windows 10 64-Bit Pro
« Reply #23 on: April 14, 2024, 11:33:21 pm »
What's the source of the dll?

Why did you create an Entry; stdcall; ?
If this is a normal program (and not a dll) can't you just call Entry between the begin/end. ?

Why do you need _mainCRTStartup?

KodeZwerg

  • Hero Member
  • *****
  • Posts: 2269
  • Fifty shades of code.
    • Delphi & FreePascal
Re: How to: create DLL file for Windows 10 64-Bit Pro
« Reply #24 on: April 15, 2024, 01:48:55 am »
without error's.
Maybe you have also rewritten the API parts but I do see 0 GetLastError calls to really be able to say from side of API = 0 error.
Since you implement API on your own, recheck the validity if your implementation is correct.
Code: Pascal  [Select][+][-]
  1. type
  2.   FARPROC = Pointer;
  3.   LPCSTR = PAnsiChar;
  4.   THandle = {Cardinal (32bit) or} UInt64 {(64bit)};
  5.   HINST = THandle;
  6.   HMODULE = HINST;
  7.   LongBool = False..Boolean(4294967295);
  8.   BOOL = LongBool;
  9.  
  10. function LoadLibraryA(lpLibFileName: LPCSTR): HMODULE; stdcall; external 'kernel32.dll' name 'LoadLibraryA';
  11. function FreeLibrary(hLibModule: HMODULE): BOOL; stdcall; external 'kernel32.dll' name 'FreeLibrary';
  12. function GetProcAddress(hModule: HMODULE; lpProcName: LPCSTR): FARPROC; stdcall; external 'kernel32.dll' name 'GetProcAddress';
« Last Edit: April 15, 2024, 01:57:46 am by KodeZwerg »
« Last Edit: Tomorrow at 31:76:97 xm by KodeZwerg »

paule32

  • Sr. Member
  • ****
  • Posts: 280
Re: How to: create DLL file for Windows 10 64-Bit Pro
« Reply #25 on: April 15, 2024, 09:29:33 am »
@rvk:
Windows EXE application's have WinMain.
Windows DLL libraries have DllMain.
_mainCRTStartup is for EXE - this is fixed by the Windows linker

@KodeZwerg:
This part is correct ? :

Code: Pascal  [Select][+][-]
  1. var
  2.   ap: procedure;
  3. begin
  4.   ap := GetProcAddress(hm, 'TestTest');

cdbc

  • Hero Member
  • *****
  • Posts: 1644
    • http://www.cdbc.dk
Re: How to: create DLL file for Windows 10 64-Bit Pro
« Reply #26 on: April 15, 2024, 09:44:29 am »
Hi
Quote
var
  ap: procedure;
begin
  ap := GetProcAddress(hm, 'TestTest');
Hmmm, wouldn't that be more like:
Code: Pascal  [Select][+][-]
  1. var
  2.   ap: procedure;
  3. begin
  4.   pointer(ap) := GetProcAddress(hm, 'TestTest');
Regards Benny
If it ain't broke, don't fix it ;)
PCLinuxOS(rolling release) 64bit -> KDE5 -> FPC 3.2.2 -> Lazarus 2.2.6 up until Jan 2024 from then on it's: KDE5/QT5 -> FPC 3.3.1 -> Lazarus 3.0

rvk

  • Hero Member
  • *****
  • Posts: 6572
Re: How to: create DLL file for Windows 10 64-Bit Pro
« Reply #27 on: April 15, 2024, 10:44:43 am »
now, I tried the following code bellow:
The DLL file will be loaded, and the library code entry will be called from inside the DLL without error's.
But the TestTest function display nothing.
I gave/give up on that build.bat of yours.

It tried to compile ...\Qt_FPC\src\units\fpc-rtl\%A.s and %A.pas at some point.
(due to incorrect batch file use with expansion)

The exit exists out of the cmd.exe I had open (you can better use goto :eof or "exit /b"  in that case, NOT "exit").
I installed msys64 and gcc but it still failed in the build.

I'm also not sure why you involve gcc in the process?
You build the .exe with fpc but in the next step... you involve assembling exe files from created .s files.
And after that you copy the same test1.exe over the one created by fpc.

I probably missing the entire point as to what you are trying to accomplish.

The loadlibrary of the fpc version (created by your build) at least works for me.
(While depending on the automatic loading of dll during startup fails with the fpc version)


paule32

  • Sr. Member
  • ****
  • Posts: 280
Re: How to: create DLL file for Windows 10 64-Bit Pro
« Reply #28 on: April 15, 2024, 12:46:32 pm »
@rvk:
did you start the build.bat ch file under dosbox or dosemu or Windows console ?
For me, this automated script does all the steps that are need to create in one step
the DLL (first), and in an other step the EXE (second).

The paths contains dos/windows compatible names...
E:\ is my develop storage media where the "\Projekte\fpc-qt" exists.

I test the build.bat twice:
- on times under Windows 10 Console
- on times under msys64

each build stage's run without fatal error's.

I let FPC produce assembly files, so I could better inspect the code, what
FPC created, and what my application is doing (with included debugger).

As debugger/analyzer, I use IDA Free.

My second goal is it, to minimize the produced data, which results into a
smaller binary image.
For the tool so called with SED - the stream editor.
It is available under msys64, too.

Like I said, programmers are lazy, and as such, they programming helper tools
like the build.bat ch, and build.ps1 - a graphical script that I use/created with
Windows PowerShell - no needs to install other programmer tools (exception like
the FPC, and MSYS tools.

The PowerShell script is not ready in all, yet.
You ask, why I use PowerShell script ?
Simply answer:
I have a second PowerShell script that is used to sign the executable file.

You can do all the step's manually per hand - but this is a little tired at some time...
So I have created a automated script for these steps.

I do not expect, that all this automation steps works on all developer computers.
But I would be expect, that, if you interesting in to build your own copy of this project,
you can change the build.bat to your needs, as long the PowerShell script is ready to
use.

The thing with %%A or %A is a Windows Command Console feature, to support FOR
loops. I used this syntax style/feature to minimize the batch file size, and make some
things maintainable better.

The involved gcc is an artifact, where I tested the FPC LD linker, as I was not sure, if the
FPC linker does'nt link the DLL and EXE correctly.
Because the view in my mind was, that there is a incompatible tools collide together:
32 bit with 64 bit or 64 bit with 32 bit.

On this reason, I does copied the EXE files from a 64 Bit Version of FPC 3.2.0 to the
already existing folder of a 32 Bit Version of FPC 3.2.0.

Then, I have two copies, but one environment value:
- SET PATH=E:\FPC\3.2.0;%PATH% (for windows)
- export path=/E/fpc/3.2.0:$path  (for msys64)

rvk

  • Hero Member
  • *****
  • Posts: 6572
Re: How to: create DLL file for Windows 10 64-Bit Pro
« Reply #29 on: April 15, 2024, 03:08:03 pm »
did you start the build.bat ch file under dosbox or dosemu or Windows console ?
I use cmd.exe (I open this manually).

When you use exit in a batch file, the complete cmd.exe will be closed.
This is unwanted behavior.
Or use exit /b (which will only close the batch file) or use goto :eof.
NEVER use exit without parameter in a batch file. NEVER EVER !!!

For me, this automated script does all the steps that are need to create in one step
the DLL (first), and in an other step the EXE (second).
[..]
each build stage's run without fatal error's.
Yep.
This works correctly.

Maybe the %A is because of the echo to console (you don't have an echo off).


I let FPC produce assembly files, so I could better inspect the code, what
FPC created, and what my application is doing (with included debugger).
You also let fpc create the dll and exe. They work ok.

The second stage to assemble the .o files with gcc fail for me.
Probably due to my installation of msys64/gcc.

My second goal is it, to minimize the produced data, which results into a
smaller binary image.
Doesn't FPC with your custom RTL do that already?

Like I said, programmers are lazy, and as such, they programming helper tools
like the build.bat ch, and build.ps1 - a graphical script that I use/created with
Windows PowerShell - no needs to install other programmer tools (exception like
the FPC, and MSYS tools.
No, they are not. But your build.bat was a mess with hard coded paths etc.
And still now... you have fpc64.exe, fpc32.exe and all tools with xx32.exe/xx64.exe.
They are not part of a standard FPC installation.
Also... fpc64.exe isn't normally in a i386-win32 directory (unless you use the cross-compile).

So... it's not a standard installation and you need to copy lots of utililies over to the correct directory.
Even after doing so... my msys64/gcc complained about libxxx.dll files not being found.
But maybe that's a path problem too.

Anyway... the files (.exe and .dll) created by your build by FPC work correctly.
The automatic loading of dll fails with the known 0xc0005 AV
and the loadlibrary version of the same dll work correctly.

I can't test your gcc/assembler version (but if automatic loading of dll fail in the fpc version it's only logical it will also fail with the gcc version).

Edit: O, and if you look at the exact output of your build batch you should also have seen a line like this fail: ( note the , instead of . )
Code: Text  [Select][+][-]
  1. del   %prjdir%\tests\test1,exe    /F /S /Q
with
Quote
Could Not Find xxxxx\Qt_FPC\src\tests\test1
Could Not Find xxxxx\Qt_FPC\src\exe

And normally I check if a directly exists before trying to delete or create it.
Code: Text  [Select][+][-]
  1. if not exist extras\ md extras
Code: Text  [Select][+][-]
  1. if exist units\ rmdir units /S/Q
In that case you can do an echo off and only report errors when they occur (not clutter the entire cmd console so you can't see the errors occur like now).
« Last Edit: April 15, 2024, 03:21:14 pm by rvk »

 

TinyPortal © 2005-2018