Solved

How to call a F(x, y, ... ) function

Posted on 1998-09-14
4
167 Views
Last Modified: 2010-04-30
I'd like to make calls to a function that takes a variable length-list of arguments.

If the function F(x, y, ...) is contained in Mylib.dll, how could I declare it in a VB app ? And how to call it ?

Does anybody know how to do it ?
0
Comment
Question by:edsont
  • 2
4 Comments
 
LVL 6

Accepted Solution

by:
clifABB earned 50 total points
Comment Utility
VB doesn't recognize variable parameter counts.

You would declare it with it's full compliment of parameters and then pass 0 (or null) to those parameters that don't require values:
Private Declare Function Foo Lib "MyLib.dll"(P1 As Long, P2 As Long) As Long
0
 
LVL 12

Expert Comment

by:mark2150
Comment Utility
If you think about how function calls work it's clear that they can't have different numbers of params. Functions pass params on the stack. (that's why you CALL a function) If you had a different number of params passed how you would know when the params ran out?

One "end run" on this is to pass an *array* to the function. The first element in the array can define the number of additional elements in the array and you can therefore have a single param function that effectively has a varaible number of params. But the call itself, has a constant number.

0
 

Author Comment

by:edsont
Comment Utility
clifABB,

Let me know if I understood your answer:

Suppose I have the following C function:

long WINAPI Foo( p1, p2, ... );

If the maximum number of parameters Foo can take is 5, I'd declare Foo in following way:

Declare Function Foo Lib "Mylib.dll" (p1,p2,p3,p4,p5) As Long

And if an app will use Foo with only 3 parameters, it would call:

  ret = Foo( p1, p2, p3, 0, 0 )

Is it correct ?

0
 
LVL 6

Expert Comment

by:clifABB
Comment Utility
Yep.
I'm wondering how the C function could export a variable number of parameters and be understood by VB.
There is a possibility that VB couldn't deal with the function at all.  But, if it does the way I explained (and you understand) is it.

There is also a possibility that, if the functions are overloaded, that is to say there are two or more with the same name, you could declare each one using the alias and the functions ordinal number:
Private Declare Function Foo1 Lib "MyLib.dll" Alias "#1"(P1 As String) As Long
Private Declare Function Foo2 Lib "MyLib.dll" Alias "#2"(P1 As Long, P2 As Long) As Long
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

There are many ways to remove duplicate entries in an SQL or Access database. Most make you temporarily insert an ID field, make a temp table and copy data back and forth, and/or are slow. Here is an easy way in VB6 using ADO to remove duplicate row…
Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
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…
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…

762 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

14 Experts available now in Live!

Get 1:1 Help Now