Solved

Pass Array of doubles(using Delphi Dll)  to VB??

Posted on 2002-04-25
3
359 Views
Last Modified: 2008-02-01
Hi All,

I built a win32 dll in delphi, then I want to use this
in my VB:
Delphi Dll code:
Function SumArray(ar : OleVariant): double;stdcall;
var r : double; i, h, l : integer;
begin
    try
        if TVarData(ar).VType=varArray then
        begin
            h := VarArrayHighBound(ar,1);
            l := VarArrayLowBound(ar,1);
            r := 0;
            for i := l to h do
            begin
                 r := r + ar[i];
            end;
            result := r;
        end
        else
            result := -1;
    except
        result := -1;
    end;
end;
Exports
    SumArray;
begin
end.

And this is my VB Code:
Private Declare Function SumArray Lib "ddll.DLL" _
    (db() as double) As Double

Sub SumArrayTest()
    Dim n(1000) As Double
    Dim i As Long
    Dim result As Double
    For i = 0 To 1000
        n(i) = 2.1235
    Next
    result = SumArray(n)
    MsgBox "Sum = " & result
End Sub

BUT I get this message!:
"....The Memory could not be read..."

Pls Help
0
Comment
Question by:eabell
3 Comments
 
LVL 12

Accepted Solution

by:
Lee_Nover earned 50 total points
ID: 6971436
you should either change Function SumArray(ar : OleVariant): double;stdcall;
to make ar a variable like :

Function SumArray(var ar : OleVariant): double;stdcall;
then you can use it with vb like:
Private Declare Function SumArray Lib "ddll.DLL" _
  (db() as double) As Double

like this you pass the variable by reference
and in delphi you accept it by reference with var

or leave delphi interface alone and change vb part

so you have :
Private Declare Function SumArray Lib "ddll.DLL" _
   (ByVal db() as double) As Double

haven't tested but it's how it should be done and it should work :)

if it doesn't work simplay pass a pointer to it like it's done in WinApi
0
 
LVL 1

Expert Comment

by:Moondancer
ID: 6975514
Has this helped you?  Assuming so, please convert the comment to the Accepted Answer to grade and close this item and award the points.  If more is needed, let us know.
Thanks,
Moondancer - EE Moderator
0
 
LVL 1

Expert Comment

by:pnh73
ID: 9004161
No comment has been added lately, so it's time to clean up this TA.
I will leave a recommendation in the Cleanup topic area that this question is:

Accept answer from Lee_Nover

Please leave any comments here within the next seven days.
 
PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER!
 
Paul (pnh73)
EE Cleanup Volunteer
0

Featured Post

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

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

The uses clause is one of those things that just tends to grow and grow. Most of the time this is in the main form, as it's from this form that all others are called. If you have a big application (including many forms), the uses clause in the in…
In my programming career I have only very rarely run into situations where operator overloading would be of any use in my work.  Normally those situations involved math with either overly large numbers (hundreds of thousands of digits or accuracy re…
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…

776 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