Solved

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

Posted on 2002-04-25
3
357 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

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Create a Restore Point In Windows 10 3 102
Delphi cmd execution 6 40
find a node in VST 2 47
Delphi inherited method 6 40
Creating an auto free TStringList The TStringList is a basic and frequently used object in Delphi. On many occasions, you may want to create a temporary list, process some items in the list and be done with the list. In such cases, you have to…
Introduction I have seen many questions in this Delphi topic area where queries in threads are needed or suggested. I know bumped into a similar need. This article will address some of the concepts when dealing with a multithreaded delphi database…
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

705 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

12 Experts available now in Live!

Get 1:1 Help Now