?
Solved

Dynamically declaring functions

Posted on 2003-02-20
17
Medium Priority
?
301 Views
Last Modified: 2010-04-07
Hi,
I have a C Dll that contains a number of functions that I need to access from a VB component. I first declare the function like so:

Private Declare Function MyFunc Lib "MyDll" Alias "MyMethod" () As Long

which then allows me to call the function.

Each time a new function is created in the C Dll, I don't want to have to update the VB component to add in a corresponding "Private Declare Function.." statement. Is there a way I can dynamically do this?

Ideally, I'd like to be able to pass a DLL name and method name into the VB component, dynamically build up and run the "Private Declare.." statement and then call that function.

Thanks
0
Comment
Question by:adathelad
[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
  • 7
  • 2
  • 2
  • +6
17 Comments
 

Expert Comment

by:berylwilson
ID: 7986783
ur question sounds strange. If we can do everythin dynamically then whatz the use of vb programming. Though if u r able to declare those functions dynamically, only u have to manually define that function with required parameters....:)
0
 
LVL 8

Expert Comment

by:bukko
ID: 7986832

What about building a generic entry function into your dll?
You can pass a function name and param array from VB, and the component's entry function can call the specific routine within it as requested.

This way at least you only need the one declaration and it won't need modifying.

Regards

bukko
0
 
LVL 18

Expert Comment

by:deighton
ID: 7986835
surely you need to change your vb program to use the new functions, so why don't you just add the function declaration at that time.  Is there a real reason for you needing to do this?

The only thing I can think of, is some sort of general interface, where the parameters and function name are passed to a generic function in the c dll - the c dll then figures out the name of ther function to call and what parameters to pass.
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
LVL 5

Expert Comment

by:bob_online
ID: 7986838
Even if you could come up with a way to dynamically declare them, how would your component know when it was appropriate to execute them??
0
 
LVL 5

Expert Comment

by:bob_online
ID: 7986840
Even if you could come up with a way to dynamically declare them, how would your component know when it was appropriate to execute them??
0
 
LVL 26

Expert Comment

by:EDDYKT
ID: 7986913
Good question from bob_online

?->
0
 
LVL 23

Author Comment

by:adathelad
ID: 7987383
OK....let me explain more.

bob_online:
My component will be told what function to call in the C dll. This VB component is acting as an "inbetween" and will be told by something else what to call - it doesn't matter what. When the component receives a message to call a function in the C Dll, it needs to dynamically link to the specified DLL and call the relevant function.

I need a way to not have to manually define which functions from which DLLs the VB component may be needed to call. The whole point is to be as generic as possible.

I have half figured it out - i've found some articles on dynamic linking which have got me 80% towards a solution.

Cheers
0
 
LVL 28

Accepted Solution

by:
AzraSound earned 750 total points
ID: 7987448
"Call API functions by Name, without Declare, v 2.0"
http://www.freevbcode.com/ShowCode.Asp?ID=1863
0
 
LVL 23

Author Comment

by:adathelad
ID: 7987451
Thanks Azra, though I had already found that :)
0
 
LVL 23

Author Comment

by:adathelad
ID: 7987519
I can load a Dll using LoadLibrary function of Kernel32.
I can then get a handle on the function within the Dll I want to call using GetProcAddress function of Kernel32.

Here is where I am stuck. I want to now execute that function with parameters if necessary...I've tried CallWindowProc which is in the user32 library, but I dont think this is what I need (it didnt work).
0
 
LVL 1

Expert Comment

by:HarmnKardn
ID: 7987678
Why don't you just wrap that C DLL to a COM library?  The interface is much more elegant.
0
 
LVL 23

Author Comment

by:adathelad
ID: 7987832
The C Dll is used by other separate processes and has to stay exactly as it is.
0
 
LVL 23

Author Comment

by:adathelad
ID: 7987861
Ok, i've managed to create a program with a form with 2 text controls. I can enter a dll name and function name and get it to dynamically call that function of that dll. (The CallWindowProc function did actually work). The function I'm calling in C returns a value that I want to capture, does anyone know how I do this?
0
 
LVL 28

Expert Comment

by:AzraSound
ID: 7988327
If the C function is returning a value, then you should be able to catch that.  If you are talking about returning a value into an [out] parameter, and then grabbing that value after the function call, I don't think I've seen that done yet.
0
 
LVL 7

Expert Comment

by:webJose
ID: 7988461
If the function returns a value as return value (return somevalue;) then the return value of CallWindowProc is that return value.

You can also get return values from parameters but that one is a lot more tricky.
0
 
LVL 23

Author Comment

by:adathelad
ID: 7989263
Yes, the C function is returning a value, not as an output parameter.
I'll give it a try tomorrow.
Thanks for the help so far :)
0
 
LVL 23

Author Comment

by:adathelad
ID: 8008243
I managed to get it all to work myself in the end. However, I found AzraSound's input was the most helpful/relevant.
0

Featured Post

Independent Software Vendors: 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

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.
This article describes how to use a set of graphical playing cards to create a Draw Poker game in Excel or VB6.
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…
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…
Suggested Courses

764 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