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?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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
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
Cloud Class® Course: MCSA MCSE Windows Server 2012

This course teaches how to install and configure Windows Server 2012 R2.  It is the first step on your path to becoming a Microsoft Certified Solutions Expert (MCSE).

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
LischkeCommented:
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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
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
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Delphi

From novice to tech pro — start learning today.