Recent

Author Topic: Cannot Write to Textfile using DLL  (Read 494 times)

shonay

  • Full Member
  • ***
  • Posts: 169
Cannot Write to Textfile using DLL
« on: January 27, 2020, 11:03:29 am »
Hey everyone,

I am trying to code a DLL with Lazarus Pascal. I want to know what i am getting wrong as Apparently i am trying to code a Library and let it open a text file and then write something to it. But it does not open the text file neither does it write any document to it.

My code looks like this

DLL

Code: Pascal  [Select][+][-]
  1. library openFilehandler;
  2.  
  3. {$mode objfpc}{$H+}
  4.  
  5. uses
  6.   Classes , Windows
  7.   { you can add units after this };
  8.  
  9. procedure OpenFileHandler;
  10. var
  11.    File1:file of Word;
  12. begin
  13.       AssignFile(File1,'C:\Users\user\Desktop\testdelphi.txt');
  14.       try
  15.         Rewrite(File1);
  16.         Write(File1,1234);
  17.       finally
  18.         CloseFile(File1);
  19.       end;
  20. end;
  21.  
  22. exports OpenFileHandler;
  23. begin
  24. end.
  25.  
  26.  

And the code to trigger the dll looks like this

Code: Pascal  [Select][+][-]
  1. unit DllTest;
  2.  
  3. {$mode objfpc}{$H+}
  4.  
  5. interface
  6.  
  7. uses
  8.   Classes, SysUtils, Forms, FileUtil, Controls, Graphics, Dialogs, StdCtrls, Dynlibs;
  9.  
  10. type
  11.  
  12.   TMyDLL = function (): integer; stdcall;
  13.  
  14.   { TForm1 }
  15.  
  16.   TForm1 = class(TForm)
  17.     Button1: TButton;
  18.     Label1: TLabel;
  19.     procedure Button1Click(Sender: TObject);
  20.   private
  21.  
  22.   public
  23.  
  24.   end;
  25.  
  26. var
  27.   Form1: TForm1;
  28.  
  29. implementation
  30.  
  31. {$R *.lfm}
  32.  
  33. { TForm1 }
  34.  
  35. procedure TForm1.Button1Click(Sender: TObject);
  36. var
  37.  
  38.   MyHandle: TLibHandle;
  39.   MyDLLFunc: TMyDLL;
  40. begin
  41.     MyHandle := SafeLoadLibrary('openFilehandler.dll');
  42.      if MyHandle<>0 then
  43.      begin
  44.        MyDLLFunc := TMyDLL(GetProcedureAddress(MyHandle,'OpenFileHandler'));
  45.        if Assigned(MyDLLFunc) then
  46.        begin
  47.          try
  48.             ShowMessage('Done!');
  49.             Label1.Caption:= 'File Opened and Saved';
  50.          finally
  51.          end;
  52.        end;
  53.      end;
  54. end;
  55.  
  56. end.
  57.  
  58.  

What do I appear to be missing?
When the power of love overcomes the love of power, the world would know Peace

- Jimi Hendrix.

Thaddy

  • Hero Member
  • *****
  • Posts: 10516
Re: Cannot Write to Textfile using DLL
« Reply #1 on: January 27, 2020, 11:48:49 am »
Code: Pascal  [Select][+][-]
  1. library openFilehandler;
  2. {$mode objfpc}{$H+}{$I-} // added IOResult support instead of exceptions
  3.  
  4. procedure OpenFileHandler;
  5. var
  6.    File1:file of Word;
  7. begin
  8.    Assign(File1,'C:\Users\user\Desktop\testdelphi.txt'); // no need for assignfile
  9.    try
  10.      Rewrite(File1);
  11.      Write(File1,1234);
  12.      {Check IOResult here...
  13.       Assert(IOresult = 0); // or something like that}
  14.    finally
  15.      Flush(File1); // Always flush upon close.
  16.      {Check IOResult here...
  17.       Assert(IOresult = 0); // or something like that}
  18.      Close(File1); // no need for closefile
  19.      {Check IOResult here...
  20.       Assert(IOresult = 0); // or something like that}
  21.    end;
  22. end;
  23.  
  24. exports OpenFileHandler;
  25. begin
  26. end.
« Last Edit: January 27, 2020, 11:56:29 am by Thaddy »

Zvoni

  • Sr. Member
  • ****
  • Posts: 410
Re: Cannot Write to Textfile using DLL
« Reply #2 on: January 27, 2020, 11:51:51 am »
2 things:
1) IIRC, if omitted the standard calling convention in FPC is cdecl, not stdcall. You omit the calling convention in your library, but in your calling program you use stdcall
EDIT: OK, you don't pass any parameters, so my point is moot, but i still think it's good habit to use explicit calling conventions that match
2) you're only retrieving the pointer to the procedure, you're not executing it --> https://wiki.freepascal.org/Lazarus/FPC_Libraries
See section "Loadlibrary - dynamically loading a dynamic library"
Code: Pascal  [Select][+][-]
  1. uses ...dynlibs...
  2.  
  3. procedure UseDLL;
  4. type
  5.   TMyFunc=function (aInt:Integer; aStr: string):String; StdCall;
  6. var
  7.   MyLibC: TLibHandle= dynlibs.NilHandle;
  8.   MyFunc: TMyFunc;
  9.   FuncResult: string;
  10. begin
  11.   MyLibC := LoadLibrary('libc.' + SharedSuffix);
  12.   if MyLibC = dynlibs.NilHandle then Exit;  //DLL was not loaded successfully
  13.   MyFunc:= TMyFunc(GetProcedureAddress(MyLibC, 'MyFunc');
  14.   FuncResult:= MyFunc (5,'Test');  //Executes the function
  15.   if MyLibC <>  DynLibs.NilHandle then if FreeLibrary(MyLibC) then MyLibC:= DynLibs.NilHandle;  //Unload the lib, if already loaded
  16. end;
  17.  

EDIT2:
3) in your library it's a procedure, but your prototype in your calling program is function?!?!?
« Last Edit: January 27, 2020, 11:56:16 am by Zvoni »
One System to rule them all, One IDE to find them,
One Code to bring them all, and to the Framework bind them,
in the Land of Redmond, where the Windows lie
---------------------------------------------------------------------
People call me crazy, because i'm jumping out of perfectly fine aircraft

Thaddy

  • Hero Member
  • *****
  • Posts: 10516
Re: Cannot Write to Textfile using DLL
« Reply #3 on: January 27, 2020, 11:57:45 am »
Yes, there's even more wrong apart from my remarks. Looks complete now.

marcov

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 8793
  • FPC developer.
Re: Cannot Write to Textfile using DLL
« Reply #4 on: January 27, 2020, 11:58:54 am »
2 things:
1) IIRC, if omitted the standard calling convention in FPC is cdecl, not stdcall.

Register.

Zvoni

  • Sr. Member
  • ****
  • Posts: 410
Re: Cannot Write to Textfile using DLL
« Reply #5 on: January 27, 2020, 11:59:40 am »
2 things:
1) IIRC, if omitted the standard calling convention in FPC is cdecl, not stdcall.

Register.
Well, i did write "IIRC".......  :P :P :P
One System to rule them all, One IDE to find them,
One Code to bring them all, and to the Framework bind them,
in the Land of Redmond, where the Windows lie
---------------------------------------------------------------------
People call me crazy, because i'm jumping out of perfectly fine aircraft

glorfin

  • Full Member
  • ***
  • Posts: 109
Re: Cannot Write to Textfile using DLL
« Reply #6 on: January 27, 2020, 11:59:57 am »
In addition to Thaddy and Zvoni, who are perfectly right, you define a procedure inthe library but import it as a function. Why? And what you expect to have as a function result?

Declarations in the library and in the importing program must be equal.

Thaddy

  • Hero Member
  • *****
  • Posts: 10516
Re: Cannot Write to Textfile using DLL
« Reply #7 on: January 27, 2020, 12:06:44 pm »
2 things:
1) IIRC, if omitted the standard calling convention in FPC is cdecl, not stdcall.

Register.
Although it is good practice to declare library routines with the platform ABI (e.g. stdcall or cdecl) to keep your library inter-operable with other languages.

 

TinyPortal © 2005-2018