Solved

Passing an array from a VB6 app to a Delphi 5 dll function

Posted on 2000-04-03
2
385 Views
Last Modified: 2008-02-01
I have written a DLL in Delphi 5.0. It contains a math function which performs CRC and some other things with an array of bytes it receives. The function within the Dll returns a WORD. I need to use the dll in both Delphi and Visual Basic 6.0. I've written a separate test app in Delphi that calls the dll function and receives its result. My problem is that I can't seem to figure out how to properly call the function and pass it an array of bytes from a Visual Basic app. I've tried many different tips (not from this forum however) and the results have ranged from simple compile errors in VB to an "exception eefadeH" when I try to call the function within the dll from the VB app. The sample code below is of little value except to illustrate my problem better.  What am I doing wrong? Does anyone have a piece of sample VB code they can send that will clearly illustrate how to call a function within a dll(written in Delphi 5) and pass it an array of bytes?


Here is the code in Delphi:


// Simple test function to add all elements of received array of bytes
FUNCTION add_numbers (test : array of byte) word;
var
byt_count : integer;
count : integer;
ret_val : word;


BEGIN  // begin of test function
ret_val := 0;
byt_count := High(test);
For count:=  0 to byt_count do
  ret_val := ret_val + test[byt_count];
add_numbers := ret_val;
END;  // end of test function


I have tried it with and without the "stdcall" keyword with seemingly no difference.

This is my DLL Declare in VB:

Declare Function add_numbers Lib "testdll.dll" (bin_data As Byte) As Long


I call the DLL as follows from VB:

Dim Binary_dat(0 to 4) As Byte
Dim dll_answer As Long

dll_answer = 0

Binary_dat(0) = 1
Binary_dat(1) = 0
Binary_dat(2) = 2
Binary_dat(3) = 2
Binary_dat(4) = 1

dll_answer = add_numbers(Binary_dat(0))
Text1.Text = dll_answer



Can anyone help me with this problem? If you would like more details about the problem email me: reubenf@mediaone.net and I'll be glad to send you further information about what I've tried and the code for both the Delphi DLL and the VB App. It seems like such a simple thing. I guess it's just simple enough to confuse the he** out of me!!




Thanks in advance!!
0
Comment
Question by:reubenf
2 Comments
 
LVL 27

Expert Comment

by:Ark
ID: 2679110
Declare Function add_numbers Lib "testdll.dll" (bin_data() As Byte) As Integer
Cheers


0
 
LVL 1

Accepted Solution

by:
snoopym earned 100 total points
ID: 2719163
Your VB declaration of the function and passing the first element as an argument actually sends a pointer to the first element in your array (byte *).

In your delphi program you use the function High(array) which implies that delphi does not use arrays as a simple list of elements, but uses some kind of descriptor, something like:
struct ArrayType
{
   int LowBound;
   int HighBound;
   byte *Elements; // points to actual data
}

Your delphi function receives a pointer to this kind of structure.

I suggest that you declare your vb prototype
Declare Function add_numbers Lib "testdll.dll" (bin_data As Byte,byval data_len as int) As Long

and change your delphi function so it will receive a pointer to byte and array length parameters. This will force you to convert the array to pointer to byte in your delphi program.


0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Introduction While answering a recent question about filtering a custom class collection, I realized that this could be accomplished with very little code by using the ScriptControl (SC) library.  This article will introduce you to the SC library a…
Article by: Martin
Here are a few simple, working, games that you can use as-is or as the basis for your own games. Tic-Tac-Toe This is one of the simplest of all games.   The game allows for a choice of who goes first and keeps track of the number of wins for…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

911 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

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now