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
Solved

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

Posted on 1998-09-14
4
170 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
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

Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

Question has a verified solution.

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

Suggested Solutions

Introduction In a recent article (http://www.experts-exchange.com/A_7811-A-Better-Concatenate-Function.html) for the Excel community, I showed an improved version of the Excel Concatenate() function.  While writing that article I realized that no o…
When trying to find the cause of a problem in VBA or VB6 it's often valuable to know what procedures were executed prior to the error. You can use the Call Stack for that but it is often inadequate because it may show procedures you aren't intereste…
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
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…

792 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