Call VB6 module function dynamically
I think this is a long shot... But if I have a module named e.g. "mHello":
vb Code:
Function a() as Variant
a=1
End Function
Function b() as Variant
b=2
End Function
Function c() as Variant
c=3
End Function
Can I get a handle to mHello such that GetProcAddress(addr,"b") gets the function handle of function b?
Alternatively, is there a way to call function b, and obtain the results, fully dynamically without going through Application.Run?
Edit:
The only thing I can find which may relate is this:
http://bbs.vbstreets.ru/viewtopic.php?f=28&t=42929
However it seems vba6.dll is not supplied with office VB IDE, and thus this particular trick can not be used...
Re: Call VB6 module function dynamically
does
not work, or please put more detail what you are trying to achieve
as this thread is about vba it should be in office development
Re: Call VB6 module function dynamically
Quote:
Originally Posted by
westconn1
does
not work, or please put more detail what you are trying to achieve
as this thread is about vba it should be in office development
Sorry, but this call convention you provided is far from dynamic. The real equivalent of what I'm after would be:
vb Code:
VariantCopy myVariant, runFunc("myProject.vbp","myModule","c")
Though you might be correct that this is about VBA specifically, VBA is VB6, and therefore any solution which works in VB6 will work in VBA as well. And I have not seen anything this low level in the Office development pages, where as I have in this forum. So I suspect this forum is my best shot at answering the question.
Re: Call VB6 module function dynamically
Only thing coming to mind is the CallByName-Call, but that one only works with Objects.
You could change your module to a global class-module...
Re: Call VB6 module function dynamically
this workaround would appear to do as requested, maybe it can help
Code:
Sub ff()
With Range("a24")
.Formula = "=myc()"
myvar = .Value
.Formula = ""
End With
End Sub
note it did not work with a function named c, but myc worked correctly
Re: Call VB6 module function dynamically
@Zvoni - Though you are correct, I actually forgot to mention in my main post that I'm not interested in CallByName unless I can get a handle to the project itself like The Trick.
@westconn1 - Sorry but a work around isn't what I am after... Even worse, what you suggest would mean that any function returning an object wouldn't work... Pretty ridiculous limitation if you ask me...
@Everyone - It may be confusing as to why I am being so picky so let me explain.
I am making a VBA/VB6 API which introduces 1st class functions to VBA/VB6. The idea is you can, using my class, wrap any existing procedure and call it from any newly created method which supports the `first class function` call convention.
E.G.
vb Code:
sub Main()
Dim cb as variant
set cb = stdCallback.create("Module","myModuleName","myFunctionName")
Call someCall(cb)
end sub
sub someCall(cb as object)
Debug.Print cb()
end sub
Function myFunctionName() as boolean
myFunctionName = true
end
The point is that I can't necessarily choose how the function being wrapped is initially defined. I can not, @westconn1, change the name or return type of the function. I can not, @Zvoni, define the function in an object. I can get the developer to define a separate wrapper function, but if I can avoid this I will, hence this post.
Ultimately I need to be able to call any function, in any module, in any vb project using string identifiers only.
Just so everyone is aware, my current implementation can be found here:
https://github.com/sancarn/VBA-STD-L...lback.cls#L223
Re: Call VB6 module function dynamically
Hi sancarn, Have you achieved your goal?
vb Code:
sub Main()
Dim cb as variant
set cb = stdCallback.create("Module","myModuleName","myFunctionName")
Call someCall(cb)
end sub
sub someCall(cb as object)
Debug.Print cb()
end sub
Function myFunctionName() as boolean
myFunctionName = true
end
Re: Call VB6 module function dynamically
Quote:
Originally Posted by
dreammanor
Hi sancarn, Have you achieved your goal?
vb Code:
sub Main()
Dim cb as variant
set cb = stdCallback.create("Module","myModuleName","myFunctionName")
Call someCall(cb)
end sub
sub someCall(cb as object)
Debug.Print cb()
end sub
Function myFunctionName() as boolean
myFunctionName = true
end
Hi DreamManor,
I've had several ideas but I havent put any into action as of yet, but generally these ideas require scraping the memory of the application instance. I have already in the past done dumps of the application memory and have identified the module names in memory... You can see my experimentation in the following 2 files:
Inspection of runtime variables:
https://github.com/sancarn/VBA-STD-L...ryAnalysis.txt
Inspection of modules names and function names:
https://github.com/sancarn/VBA-STD-L...yAnalysis2.txt
Ultimately I'm not totally sure if these will always be at a fixed location in memory or if the location will be inferrable from other information.
Also perhaps related - String interpolation:
https://github.com/sancarn/VBA-STD-L...tion-mechanism
But anyway this is all heavily WIP/Experimental. Not sure how stable any of these examples are...
Re: Call VB6 module function dynamically
Re: Call VB6 module function dynamically
Quote:
Originally Posted by
sancarn
I think this is a long shot... But if I have a module named e.g. "mHello":
vb Code:
Function a() as Variant
a=1
End Function
Function b() as Variant
b=2
End Function
Function c() as Variant
c=3
End Function
Can I get a handle to mHello such that GetProcAddress(addr,"b") gets the function handle of function b?
Alternatively, is there a way to call function b, and obtain the results, fully dynamically without going through Application.Run?
Have you looked at AddressOf and CallWindowProc ?
Or better Addressof and DispCallFunc.
Re: Call VB6 module function dynamically
Quote:
Originally Posted by
JAAFAR
Have you looked at AddressOf and CallWindowProc ?
Or better Addressof and DispCallFunc.
I have yeah! I've also looked at a neat example by TheTrick which creates a 'pointer execute' function with some machine code. It's not super ideal, but it is significantly better than what I have currently. I think it generally must be possible just don't know how.