?
Solved

DLL written in Delphi gives inconsistent results in VBA

Posted on 2007-08-09
11
Medium Priority
?
330 Views
Last Modified: 2013-11-05
I have a DLL written in Delphi providing functions with boolean return type. When I run code in VBA to call the functions I always get result of TRUE even if it should be false. However when I debug the VBA code in single step mode I get the correct result. How can this happen, where is the mistake?

Thanks for any help.

The vba code:

Public Declare Function IsA4PRunning Lib "c:\programme\geops\altlast\bin\calla4p.dll" () As Boolean

Public Function TestDLLFunctions()

  MsgBox IsA4PRunning

End Function


The delphi function in the DLL:

function IsA4PRunning:Boolean;stdcall;
{checks if Application XXX is running}
begin
  try
    Result:= (FindWindow('TfrmCMain', nil)<>0);
  except
    Result:=FALSE;
  end;
end;

0
Comment
Question by:theinterface
11 Comments
 
LVL 21

Expert Comment

by:ziolko
ID: 19660765
not sure if that's issue in your case but true on VB is mapped to -1 while in delphi to 1.



ziolko.
0
 

Author Comment

by:theinterface
ID: 19660830
Thank you, but that's no solution. Your answer does not explain the differences between normal execution and debug mode in VBA.

And, if I force my Delphi function to always return false, VBA will also always get false:

function IsA4PRunning:Boolean;stdcall;
{checks if Application XXX is running}
begin
 Result:=false;
end;
0
 
LVL 21

Expert Comment

by:ziolko
ID: 19660866
well in this case check your VB code maybe variable storing return value is somwhere overriden?

ziolko.
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:theinterface
ID: 19660901
Please have a look at my VB sample code above. in this 3-liner there is nothing that could be overrriden.
0
 
LVL 21

Expert Comment

by:ziolko
ID: 19660948
wow i didn't think thats full code;)

in this case i'm out of ideas.

ziolko.

0
 
LVL 15

Expert Comment

by:cquinn
ID: 19661342
Are you calling it correctly?

try

Public Function TestDLLFunctions()

  MsgBox IsA4PRunning()

End Function
0
 

Author Comment

by:theinterface
ID: 19661370
With or without ()  makes no difference.
0
 
LVL 21

Assisted Solution

by:developmentguru
developmentguru earned 360 total points
ID: 19661452
I have run into the situation in VB in the past (thats one of many reasons I no longer do VB if I have a choice).  I have found that statements that should be equivalent are not always. For example:

Public Declare Function IsA4PRunning Lib "c:\programme\geops\altlast\bin\calla4p.dll" () As Boolean
Public Function TestDLLFunctions()
  MsgBox (IsA4PRunning <> false)
End Function

may work for you even though it is equivalent to what you wrote.

The other thing to try is to declare your Delphi function as follows:

function IsA4PRunning:LongBool;stdcall;
{checks if Application XXX is running}
begin
  try
    Result:= (FindWindow('TfrmCMain', nil)<>0);
  except
    Result:=FALSE;
  end;
end;

  I checked on MSDN and the size of the boolean value in memory is platform dependant.  sometimes one byte sometimes sometimes 2.  While I would expect your original code, if not causing stack misalignment, is correct on the Delphi side... this is just something you can experiment with.  Be aware that the experiment could cause a stack misalignment.  If this happens you need to be prepared to loose the VB IDE (crash, hang, etc) not a big deal if you are prepared for it.

  I would 1) try the redeclaration of the Delphi DLL function as LongBool.  If that does not fix it try the different test in VB.  Let me know the results.
0
 
LVL 26

Accepted Solution

by:
Russell Libby earned 390 total points
ID: 19661789

The Boolean type in VBA is not platform independant; its clearly defined as a TWO (2) byte data storage type. The Delphi Boolean on the other hand is one (1) byte. End result is that the return register will only have one byte (of four) set, and the other 3 bytes of the register will be filled with whatever was there last, thus the incorrect and inconsistent results.

To fix, you should be using the WordBool data type as the return from your function.

eg:
function IsA4PRunning: WordBool;stdcall;


Regards,
Russell

0
 
LVL 46

Expert Comment

by:aikimark
ID: 19680838
Trust Russell Libby's answer.  The Delphi Boolean data type is an enumeration of a {False, True} set.
0
 

Author Comment

by:theinterface
ID: 19681825
Akimark, sorry, I thought I had already accepted two solutions last week. The return type of longbool or wordbool solved the problem.

The hint by developmentguru to change the VBA-code to "IsA4PRunning <> false" however did not have any effect.

Thank you all!

Uli
0

Featured Post

Receive 1:1 tech help

Solve your biggest tech problems alongside global tech experts with 1:1 help.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

If you have ever used Microsoft Word then you know that it has a good spell checker and it may have occurred to you that the ability to check spelling might be a nice piece of functionality to add to certain applications of yours. Well the code that…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…
Suggested Courses
Course of the Month8 days, 8 hours left to enroll

621 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question