Trying to call a function in a DLL

This works...

Main program:

implementation

uses LdapAPI;

{$R *.DFM}

procedure TForm1.Button1Click(Sender: TObject);
var
  i: integer;
begin
  i := DLLtest(HostEdit.Text, UserEdit.Text, PasswordEdit.Text);
  case i of
    0: StatusBar1.Panels[1].Text := 'Verify ok';
    2: StatusBar1.Panels[1].Text := 'LDAP Server not found!';
    -1326: StatusBar1.Panels[1].Text := 'Error -1326 : Bad username or password!';
  end;
end;


LdapAPI unit:

unit LdapAPI;

interface

function DLLtest(sHostName, sUserName, sPassWord: string): integer;
                          stdcall; external 'LDAPver.dll' name 'Login';

implementation

end.


But when I try to call DLLtest from another function it doesn't! Why?

Main program:

implementation

uses LdapAPI;

{$R *.DFM}

procedure TForm1.Button1Click(Sender: TObject);
var
  i: integer;
begin
  i := Login(HostEdit.Text, UserEdit.Text, PasswordEdit.Text);
  case i of
    0: StatusBar1.Panels[1].Text := 'Verify ok';
    2: StatusBar1.Panels[1].Text := 'LDAP Server not found!';
    -1326: StatusBar1.Panels[1].Text := 'Error -1326 : Bad username or password!';
  end;
end;


LdapAPI unit:

unit LdapAPI;

interface

function Login(sHostName, sUserName, sPassWord: string): integer;
function DLLtest(sHostName, sUserName, sPassWord: string): integer;
                          stdcall; external 'LDAPver.dll' name 'Login';
implementation

function Login(sHostName, sUserName, sPassWord: string): integer;
begin
  result := DLLtest(sHostName, sUserName, sPassWord);
end;

end.



Wolfie320Asked:
Who is Participating?
 
LischkeConnect With a Mentor Commented:
Yep, I think I understand. The disappearing return address is a very strong indication that your calling convention is wrong. Make sure and compare twice that both the implementation in the DLL as well as the prototype in the application have the same calling convention. You don't need to use stdcall if your DLL will never be used with other stuff, but it is quite common to declare ALL exported functions as being stdcall.

Ciao, Mike
0
 
TheNeilCommented:
Try changing the name parameter on your DLL function definition - the use of 'Login' as the 'name' and as the function name could be confusing it.

The Neil
0
 
rwilson032697Commented:
Listening
0
Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
LischkeCommented:
What does not work? Please be more specific. Additionally, I suppose you know about the limitations when using strings in DLLs (in particular as parameters)? Keyword: ShareMem! I suggest using PChars instead as parameters.

Ciao, Mike
0
 
Wolfie320Author Commented:
Hi all,

forget the axample above.

TheNeil, I did change the name to myLogin. Same result, doesen't work. :(

Lischke:

I'll try to explain what i'm doing.

here is my test prog...

program Test;

{$APPTYPE CONSOLE}
uses
  windows, regnet;

begin
  Writeln('Start...');
  //myTest(0);
  Read_Init(0);
  Write_Init(0);
  Writeln('Done');
  Sleep(10000);
end.

The Read_Init procedure is calling a function inside a dll. When the 'DLL' function returns the pointer in the call stack is gone! So when the Read_Init procedure is complete the program ends! The call to the function inside the DLL seems to remove the pionter to 'Test' (main program) in the call stack!?!!?

Hope you all understand my bad english!

Regards

/Tommy
0
 
Wolfie320Author Commented:
Thanks Lishke,

I was using stdcall... like this...

function RNConnect(Addr: word): integer;
                 stdcall; external 'rnapi.dll';

I did remove the stdcall; and now it works!

/Wolfie (dll newbie)
0
 
LischkeCommented:
So may I get the points? (You can use the "accept comment as answer" link beside any of my comments).

Ciao, Mike
0
 
Wolfie320Author Commented:
Thanks!
0
All Courses

From novice to tech pro — start learning today.