-
Apr 24th, 2019, 05:02 PM
#1
Thread Starter
Member
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...
Last edited by sancarn; Apr 24th, 2019 at 05:44 PM.
-
Apr 24th, 2019, 05:51 PM
#2
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
i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next
dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part
come back and mark your original post as resolved if your problem is fixed
pete
-
Apr 25th, 2019, 01:45 AM
#3
Thread Starter
Member
Re: Call VB6 module function dynamically
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.
-
Apr 25th, 2019, 02:02 AM
#4
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...
Last edited by Zvoni; Tomorrow at 31:69 PM.
----------------------------------------------------------------------------------------
One System to rule them all, One Code to find them,
One IDE to bring them all, and to the Framework bind them,
in the Land of Redmond, where the Windows lie
---------------------------------------------------------------------------------
People call me crazy because i'm jumping out of perfectly fine airplanes.
---------------------------------------------------------------------------------
Code is like a joke: If you have to explain it, it's bad
-
Apr 25th, 2019, 02:43 AM
#5
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
i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next
dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part
come back and mark your original post as resolved if your problem is fixed
pete
-
Apr 25th, 2019, 03:10 AM
#6
Thread Starter
Member
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
Last edited by sancarn; Apr 25th, 2019 at 03:55 PM.
-
Jun 20th, 2019, 08:15 PM
#7
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
-
Jun 21st, 2019, 05:51 AM
#8
Thread Starter
Member
Re: Call VB6 module function dynamically
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...
-
Jun 24th, 2019, 09:08 AM
#9
Re: Call VB6 module function dynamically
-
Jun 25th, 2019, 10:17 PM
#10
Fanatic Member
Re: Call VB6 module function dynamically
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.
-
Jul 20th, 2019, 11:52 AM
#11
Thread Starter
Member
Re: Call VB6 module function dynamically
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.
Tags for this Thread
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|