Solved

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

Posted on 2002-04-25
3
364 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

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

A lot of questions regard threads in Delphi.   One of the more specific questions is how to show progress of the thread.   Updating a progressbar from inside a thread is a mistake. A solution to this would be to send a synchronized message to the…
Introduction The parallel port is a very commonly known port, it was widely used to connect a printer to the PC, if you look at the back of your computer, for those who don't have newer computers, there will be a port with 25 pins and a small print…
In this video, viewers will be given step by step instructions on adjusting mouse, pointer and cursor visibility in Microsoft Windows 10. The video seeks to educate those who are struggling with the new Windows 10 Graphical User Interface. Change Cu…
If you’ve ever visited a web page and noticed a cool font that you really liked the look of, but couldn’t figure out which font it was so that you could use it for your own work, then this video is for you! In this Micro Tutorial, you'll learn yo…

724 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