Solved

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

Posted on 1998-09-14
4
173 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
[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
  • 2
4 Comments
 
LVL 6

Accepted Solution

by:
clifABB earned 50 total points
ID: 1434804
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
ID: 1434805
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
ID: 1434806
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
ID: 1434807
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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

I was working on a PowerPoint add-in the other day and a client asked me "can you implement a feature which processes a chart when it's pasted into a slide from another deck?". It got me wondering how to hook into built-in ribbon events in Office.
This article describes some techniques which will make your VBA or Visual Basic Classic code easier to understand and maintain, whether by you, your replacement, or another Experts-Exchange expert.
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…

707 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