Recent

Author Topic: Help with sending library functions to another program  (Read 4377 times)

karmacomposer

  • New Member
  • *
  • Posts: 25
Help with sending library functions to another program
« on: March 31, 2021, 07:42:28 pm »
Hello.  I need help.  I have created a bunch of procedures and functions that need to be in a .dll that can then be loaded and used across multiple units in the same overall program.

Here is the simple .dll I created to store some simple strings:

Code: [Select]
library serverInfo;

var
  User: pChar;
  Pass: pChar;
  Loc: pChar;

procedure getUser();
begin
  User := 'myUsername';
end;
exports
         User;

procedure getPass();
begin
  User := 'myPassword';
end;
exports
         Pass;

procedure getLoc();
begin
  Loc := 'http://www.someurl.com';
end;
exports
         Loc;

end.

and here is the program code:

Code: [Select]
unit unitGetServerInfo2;

{$mode objfpc}{$H+}

interface

uses
  Classes, SysUtils, Forms, Controls, Graphics, Dialogs, StdCtrls, dynlibs;

type

  { TForm1 }

  TForm1 = class(TForm)
    Button1: TButton;
    procedure Button1Click(Sender: TObject);

  private
    { dynamically linking }
    hLib: TlibHandle;

  public

  end;

var
  Form1: TForm1;
  hndlDll: TLibHandle = dynlibs.NilHandle;
  User: pChar;
  Pass: pChar;
  Loc: pChar;
  Username: pChar;
  Password: pChar;
  Location: pChar;

implementation

function getUser(var User: pChar): pChar; cdecl;
begin
   hndlDll:= LoadLibrary('serverInfo.dll');
   Username := User;
   showmessage(Username);
end;

function getPass(var Pass: pChar): pChar; cdecl;
begin
   hndlDll:= LoadLibrary('serverInfo.dll');
   Password := Pass;
   showmessage(Password);
end;

function getLoc(var Loc: pChar): pChar; cdecl;
begin
   hndlDll:= LoadLibrary('serverInfo.dll');
   Location := Loc;
   showmessage(Location);
end;

{$R *.lfm}

{ TForm1 }

procedure TForm1.Button1Click(Sender: TObject);
begin
  getUser(User);
  getPass(Pass);
  getLoc(Loc);
end;

end.

What am I doing wrong/omitting? Nothing is passed - nothing shows up in the showmessage prompt.

Thank you for your help.

Mike


balazsszekely

  • Guest
Re: Help with sending library functions to another program
« Reply #1 on: March 31, 2021, 08:11:33 pm »
@karmacomposer

1. You did not check the value of hndlDll
2. If hndlDll >0 then you should call GetProcedureAddress(hndlDll , 'getUser')

Please check the attached project from this link: https://forum.lazarus.freepascal.org/index.php/topic,53621.msg396954.html#msg396954 .
 

Gustavo 'Gus' Carreno

  • Hero Member
  • *****
  • Posts: 1114
  • Professional amateur ;-P
Re: Help with sending library functions to another program
« Reply #2 on: March 31, 2021, 08:41:33 pm »
Hey karmacomposer,

Don't let me detract you from what GetMem told you. He's more right than wrong :P

I just wanted to ask some, maybe dumb, questions:
Why are you doing this with *.dlls?
Is it to hide those secrets on compiled binary, like some kind of security by obscurity?
Is it to then distribute that *.dll as some sort of an unlock key when someone purchases a license?

The example code you provided as made me quite curious of your intentions. I'm feeling quite cat'ish :)

Cheers,
Gus
Lazarus 3.99(main) FPC 3.3.1(main) Ubuntu 23.10 64b Dark Theme
Lazarus 3.0.0(stable) FPC 3.2.2(stable) Ubuntu 23.10 64b Dark Theme
http://github.com/gcarreno

karmacomposer

  • New Member
  • *
  • Posts: 25
Re: Help with sending library functions to another program
« Reply #3 on: March 31, 2021, 11:53:23 pm »
@karmacomposer

1. You did not check the value of hndlDll
2. If hndlDll >0 then you should call GetProcedureAddress(hndlDll , 'getUser')

Please check the attached project from this link: https://forum.lazarus.freepascal.org/index.php/topic,53621.msg396954.html#msg396954 .

Thank you.  I will try that tomorrow when I am coding.  I assume I do the same for the other functions.

Mike

karmacomposer

  • New Member
  • *
  • Posts: 25
Re: Help with sending library functions to another program
« Reply #4 on: March 31, 2021, 11:55:36 pm »
Hey karmacomposer,

Don't let me detract you from what GetMem told you. He's more right than wrong :P

I just wanted to ask some, maybe dumb, questions:
Why are you doing this with *.dlls?
Is it to hide those secrets on compiled binary, like some kind of security by obscurity?
Is it to then distribute that *.dll as some sort of an unlock key when someone purchases a license?

The example code you provided as made me quite curious of your intentions. I'm feeling quite cat'ish :)

Cheers,
Gus

While the code above is not part of our security product (created in another, more secure language), there is quite a lot of functions and procedures that are and I need to distribute them
as some kind of protected library.  Since you can decompile a .dll fairly easily, we will be obsfucating and encrypting the compiled code and then
distributing it. So, yes, it is intended to be a locked library that clients can use without getting to the actual source code.

Mike

karmacomposer

  • New Member
  • *
  • Posts: 25
Re: Help with sending library functions to another program
« Reply #5 on: April 01, 2021, 06:41:56 pm »
I tried what you suggested:

Code: [Select]
unit unitGetServerInfo2;

{$mode objfpc}{$H+}

interface

uses
  Classes, SysUtils, Forms, Controls, Graphics, Dialogs, StdCtrls, dynlibs;

type

  { TForm1 }

  TForm1 = class(TForm)
    Button1: TButton;
    procedure Button1Click(Sender: TObject);

  private
    { dynamically linking }
    hLib: TlibHandle;

  public

  end;

var
  Form1: TForm1;
  hndlDll: TLibHandle = dynlibs.NilHandle;
  User: pChar;
  Pass: pChar;
  Loc: pChar;
  Username: pChar;
  Password: pChar;
  Location: pChar;

implementation

function getUser(var User: pChar): pChar; cdecl;
begin
   hndlDll:= LoadLibrary('serverInfo.dll');
   if hndlDll>0 then GetProcedureAddress(hndlDll , 'getUser');
   Username := User;
   showmessage(Username);
end;

function getPass(var Pass: pChar): pChar; cdecl;
begin
   hndlDll:= LoadLibrary('serverInfo.dll');
   if hndlDll>0 then GetProcedureAddress(hndlDll , 'getPass');
   Password := Pass;
   showmessage(Password);
end;

function getLoc(var Loc: pChar): pChar; cdecl;
begin
   hndlDll:= LoadLibrary('serverInfo.dll');
   if hndlDll>0 then GetProcedureAddress(hndlDll , 'getLoc');
   Location := Loc;
   showmessage(Location);
end;

{$R *.lfm}

{ TForm1 }

procedure TForm1.Button1Click(Sender: TObject);
begin
  getUser(User);
  getPass(Pass);
  getLoc(Loc);
end;

end.

and it did not work, nor did it throw an error.

How can I make this work please?

Thank you.

Mike
« Last Edit: April 01, 2021, 06:59:49 pm by karmacomposer »

Gustavo 'Gus' Carreno

  • Hero Member
  • *****
  • Posts: 1114
  • Professional amateur ;-P
Re: Help with sending library functions to another program
« Reply #6 on: April 01, 2021, 06:50:53 pm »
Hey Mike,
I tried what you suggested:
[snipped code]
and it did not work, nor did it throw an error.
How can I make this work please?

Im sorry Mike, but you didn't do everything GetMem told you to:
@karmacomposer

1. You did not check the value of hndlDll
2. If hndlDll >0 then you should call GetProcedureAddress(hndlDll , 'getUser')

Please check the attached project from this link: https://forum.lazarus.freepascal.org/index.php/topic,53621.msg396954.html#msg396954 .

You didn't call GetProcedureAddress(hndlDll , 'getUser').

Are you sure you had a good look at the project attached on the post that GetMem gave you?

Cheers,
Gus
Lazarus 3.99(main) FPC 3.3.1(main) Ubuntu 23.10 64b Dark Theme
Lazarus 3.0.0(stable) FPC 3.2.2(stable) Ubuntu 23.10 64b Dark Theme
http://github.com/gcarreno

karmacomposer

  • New Member
  • *
  • Posts: 25
Re: Help with sending library functions to another program
« Reply #7 on: April 01, 2021, 07:00:48 pm »
Hey Mike,
I tried what you suggested:
[snipped code]
and it did not work, nor did it throw an error.
How can I make this work please?

Im sorry Mike, but you didn't do everything GetMem told you to:
@karmacomposer

1. You did not check the value of hndlDll
2. If hndlDll >0 then you should call GetProcedureAddress(hndlDll , 'getUser')

Please check the attached project from this link: https://forum.lazarus.freepascal.org/index.php/topic,53621.msg396954.html#msg396954 .

You didn't call GetProcedureAddress(hndlDll , 'getUser').

Are you sure you had a good look at the project attached on the post that GetMem gave you?

Cheers,
Gus

My mistake.  I corrected it and then tried again - still not working.

Code: [Select]
unit unitGetServerInfo2;

{$mode objfpc}{$H+}

interface

uses
  Classes, SysUtils, Forms, Controls, Graphics, Dialogs, StdCtrls, dynlibs;

type

  { TForm1 }

  TForm1 = class(TForm)
    Button1: TButton;
    procedure Button1Click(Sender: TObject);

  private
    { dynamically linking }
    hLib: TlibHandle;

  public

  end;

var
  Form1: TForm1;
  hndlDll: TLibHandle = dynlibs.NilHandle;
  User: pChar;
  Pass: pChar;
  Loc: pChar;
  Username: pChar;
  Password: pChar;
  Location: pChar;

implementation

function getUser(var User: pChar): pChar; cdecl;
begin
   hndlDll:= LoadLibrary('serverInfo.dll');
   if hndlDll>0 then GetProcedureAddress(hndlDll , 'getUser');
   Username := User;
   showmessage(Username);
end;

function getPass(var Pass: pChar): pChar; cdecl;
begin
   hndlDll:= LoadLibrary('serverInfo.dll');
   if hndlDll>0 then GetProcedureAddress(hndlDll , 'getPass');
   Password := Pass;
   showmessage(Password);
end;

function getLoc(var Loc: pChar): pChar; cdecl;
begin
   hndlDll:= LoadLibrary('serverInfo.dll');
   if hndlDll>0 then GetProcedureAddress(hndlDll , 'getLoc');
   Location := Loc;
   showmessage(Location);
end;

{$R *.lfm}

{ TForm1 }

procedure TForm1.Button1Click(Sender: TObject);
begin
  getUser(User);
  getPass(Pass);
  getLoc(Loc);
end;

end.

I could not find any attached project in that link.

Help is appreciated.

Mike

Gustavo 'Gus' Carreno

  • Hero Member
  • *****
  • Posts: 1114
  • Professional amateur ;-P
Re: Help with sending library functions to another program
« Reply #8 on: April 01, 2021, 07:10:53 pm »
Hey Mike,

I wasn't gonna say anything more, but after having a look at the code that GetMem linked to, it's quite apparent that you did nothing that he suggested, well apart from that single if statement, and then came back to bitch about it.

If I read GetMem's code correctly you need to:
  • Declare the signature of the functions inside the DLL.
  • Load the library just once, which make absolute sense, why the heck load it for each call!?!?
  • Get the function address into the var you're gonna use with GetProcedureAddress() by typecasting it to the appropriate signature.
  • Call the function through the variable set in the step before.

So you did not do any of those things.

And again:
My mistake.  I corrected it and then tried again - still not working.

You did not correct anything since you completely failed to understand what to do with GetProcedureAddress().

I could not find any attached project in that link.

I'm sure that what you want to say is: I didn't even try to look.

Because I found it immediately, downloaded it, unzipped it and had a look at it.

You on the other hand want us to do the leg work for you. That's not what this forum is about.
We give you help, so you can help yourself. We don't do Homework for you!!

Cheers,
Gus

PS: I've linked the code twice. I hope that's enough for you to have a look at it and THEN come back if you still have issues.
Lazarus 3.99(main) FPC 3.3.1(main) Ubuntu 23.10 64b Dark Theme
Lazarus 3.0.0(stable) FPC 3.2.2(stable) Ubuntu 23.10 64b Dark Theme
http://github.com/gcarreno

karmacomposer

  • New Member
  • *
  • Posts: 25
Re: Help with sending library functions to another program
« Reply #9 on: April 01, 2021, 07:17:58 pm »
Hey Mike,

I wasn't gonna say anything more, but after having a look at the code that GetMem linked to, it's quite apparent that you did nothing that he suggested, well apart from that single if statement, and then came back to bitch about it.

If I read GetMem's code correctly you need to:
  • Declare the signature of the functions inside the DLL.
  • Load the library just once, which make absolute sense, why the heck load it for each call!?!?
  • Get the function address into the var you're gonna use with GetProcedureAddress() by typecasting it to the appropriate signature.
  • Call the function through the variable set in the step before.

So you did not do any of those things.

And again:
My mistake.  I corrected it and then tried again - still not working.

You did not correct anything since you completely failed to understand what to do with GetProcedureAddress().

I could not find any attached project in that link.

I'm sure that what you want to say is: I didn't even try to look.

Because I found it immediately, downloaded it, unzipped it and had a look at it.

You on the other hand want us to do the leg work for you. That's not what this forum is about.
We give you help, so you can help yourself. We don't do Homework for you!!

Cheers,
Gus

PS: I've linked the code twice. I hope that's enough for you to have a look at it and THEN come back if you still have issues.

While I appreciate the help, I do not appreciate the tone.  I wrote the initial code.  Obviously, I did it wrong.  I am asking for help.  I don't need sanctimonious remarks from someone who must enjoy belittling people who ask for help.  And NO, I could not find the attached file, so obviously I do not know how the forum attaches files.  If someone would point it out to me, I would download it and study it.

Do me a favor GUS and do not help me any more.  I'd hate to be in a class with you if you were the instructor.

Anyone else, thank you.

Mike

karmacomposer

  • New Member
  • *
  • Posts: 25
Re: Help with sending library functions to another program
« Reply #10 on: April 01, 2021, 07:23:01 pm »
If you were referring to plugin.zip, that is why I could not find it.  Again, I appreciate the help, but if someone would have just said "download plugin.zip at this link" I would have found it immediately.

I'll study that file and see if I can do this on my own.

I wrote an entire security program in another language, so I am not an idiot.  I just don't know how to grab functions and procedures from a .dll file.  I even watched videos, took classes on Pascal and all they teach is how to do rudimentary stuff. I am writing the code, but I ask for help if I get stuck since this is about the ONLY resource for Lazarus other than Stack Overflow and the Wiki.

Thanks again.

Mike

Gustavo 'Gus' Carreno

  • Hero Member
  • *****
  • Posts: 1114
  • Professional amateur ;-P
Re: Help with sending library functions to another program
« Reply #11 on: April 01, 2021, 08:24:53 pm »
Hey Mike,

I don't turn on The Tone just willy nilly.
I turn on The Tone when the person that is being helped makes a small to no effort to pay attention to what is being said to him/her.
I turn on The Tone when the person has been advised, in a normal and helpful tone, more than once, and still cries wolf.

GetMem sent you a link that would land you in his exact message, that's what the #msg396954 at the end of the URL makes the browser do.
You then had only to look at the space between his message and the next to find the link to plugin.zip.
Right after any message you find the attachment area:
  • Empty if no one attached anything.
  • Has a link to an attached file with it's name if it's not an image.
  • Has a thumbnail of the image that was attached.

It's all over the forum and I'm sure you've stumbled upon it many times before.

What I don't appreciate is someone that has been given polite instruction on how to be helped, then turns around and complains I have a tone when you have failed to do the legwork.
I'm sorry if you feel entitled to anything. You're not. We help here if everything is civil. Entitlement is not being civil.
Like you said, you're not stupid, but contrary to that is all the evidence of your previous interaction. Don't you agree?

I linked to the file TWICE and it took you TWO quoted replies to even acknowledge it. With that kind of evidence from you, you wanna tell me I can't conclude something suspicious about you?
How am I to be convinced that you're NOT stupid after that?

I sincerely hope that, NOW, that you have the code you can help yourself and finish your endeavour. No ill feelings.
Just pay attention to your environment next time, Kay?

Cheers,
Gus
Lazarus 3.99(main) FPC 3.3.1(main) Ubuntu 23.10 64b Dark Theme
Lazarus 3.0.0(stable) FPC 3.2.2(stable) Ubuntu 23.10 64b Dark Theme
http://github.com/gcarreno

balazsszekely

  • Guest
Re: Help with sending library functions to another program
« Reply #12 on: April 01, 2021, 08:49:02 pm »
@karmacomposer

See attached project.
1. Build the dll(serverinfo subfolder)
2. Run the project

Gustavo 'Gus' Carreno

  • Hero Member
  • *****
  • Posts: 1114
  • Professional amateur ;-P
Re: Help with sending library functions to another program
« Reply #13 on: April 01, 2021, 08:58:56 pm »
Hey GetMem,

See attached project.

Awwww, GetMem, you are such a softy!!! ;-P

I just ended scalding him because we don't do homework and you gone-n-dun it :D

Oh, well Mike, you got lucky GetMem is such a nice guy and did the legwork for you :P
I hope you thank him appropriately !!

Cheers,
Gus
Lazarus 3.99(main) FPC 3.3.1(main) Ubuntu 23.10 64b Dark Theme
Lazarus 3.0.0(stable) FPC 3.2.2(stable) Ubuntu 23.10 64b Dark Theme
http://github.com/gcarreno

Gustavo 'Gus' Carreno

  • Hero Member
  • *****
  • Posts: 1114
  • Professional amateur ;-P
Re: Help with sending library functions to another program
« Reply #14 on: April 01, 2021, 09:02:14 pm »
Hey Mike,

If after the effort GetMem put on for you, you turn around and say you can't find the bloody file, I'll pop up from your monitor and do a Jethro Gibbs on you by slapping the back of that head of yours !!

Cheers,
Gus
Lazarus 3.99(main) FPC 3.3.1(main) Ubuntu 23.10 64b Dark Theme
Lazarus 3.0.0(stable) FPC 3.2.2(stable) Ubuntu 23.10 64b Dark Theme
http://github.com/gcarreno

 

TinyPortal © 2005-2018