Results 1 to 11 of 11

Thread: Call VB6 module function dynamically

  1. #1

    Thread Starter
    Member
    Join Date
    Apr 2019
    Posts
    63

    Call VB6 module function dynamically

    I think this is a long shot... But if I have a module named e.g. "mHello":

    vb Code:
    1. Function a() as Variant
    2.    a=1
    3. End Function
    4. Function b() as Variant
    5.    b=2
    6. End Function
    7. Function c() as Variant
    8.    c=3
    9. 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.

  2. #2
    PowerPoster
    Join Date
    Dec 2004
    Posts
    25,618

    Re: Call VB6 module function dynamically

    does
    Code:
    myvariant = c
    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

  3. #3

    Thread Starter
    Member
    Join Date
    Apr 2019
    Posts
    63

    Re: Call VB6 module function dynamically

    Quote Originally Posted by westconn1 View Post
    does
    Code:
    myvariant = c
    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:
    1. 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.

  4. #4
    PowerPoster Zvoni's Avatar
    Join Date
    Sep 2012
    Location
    To the moon and then left
    Posts
    4,443

    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

  5. #5
    PowerPoster
    Join Date
    Dec 2004
    Posts
    25,618

    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

  6. #6

    Thread Starter
    Member
    Join Date
    Apr 2019
    Posts
    63

    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:
    1. sub Main()
    2.   Dim cb as variant
    3.   set cb = stdCallback.create("Module","myModuleName","myFunctionName")
    4.   Call someCall(cb)
    5. end sub
    6. sub someCall(cb as object)
    7.   Debug.Print cb()
    8. end sub
    9. Function myFunctionName() as boolean
    10.   myFunctionName = true
    11. 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.

  7. #7
    PowerPoster
    Join Date
    Sep 2012
    Posts
    2,083

    Re: Call VB6 module function dynamically

    Hi sancarn, Have you achieved your goal?

    vb Code:
    1. sub Main()
    2.   Dim cb as variant
    3.   set cb = stdCallback.create("Module","myModuleName","myFunctionName")
    4.   Call someCall(cb)
    5. end sub
    6. sub someCall(cb as object)
    7.   Debug.Print cb()
    8. end sub
    9. Function myFunctionName() as boolean
    10.   myFunctionName = true
    11. end

  8. #8

    Thread Starter
    Member
    Join Date
    Apr 2019
    Posts
    63

    Re: Call VB6 module function dynamically

    Quote Originally Posted by dreammanor View Post
    Hi sancarn, Have you achieved your goal?

    vb Code:
    1. sub Main()
    2.   Dim cb as variant
    3.   set cb = stdCallback.create("Module","myModuleName","myFunctionName")
    4.   Call someCall(cb)
    5. end sub
    6. sub someCall(cb as object)
    7.   Debug.Print cb()
    8. end sub
    9. Function myFunctionName() as boolean
    10.   myFunctionName = true
    11. 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...

  9. #9
    PowerPoster
    Join Date
    Sep 2012
    Posts
    2,083

    Re: Call VB6 module function dynamically

    Thank you, sancarn.

  10. #10
    Fanatic Member
    Join Date
    Nov 2013
    Posts
    658

    Re: Call VB6 module function dynamically

    Quote Originally Posted by sancarn View Post
    I think this is a long shot... But if I have a module named e.g. "mHello":

    vb Code:
    1. Function a() as Variant
    2.    a=1
    3. End Function
    4. Function b() as Variant
    5.    b=2
    6. End Function
    7. Function c() as Variant
    8.    c=3
    9. 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.

  11. #11

    Thread Starter
    Member
    Join Date
    Apr 2019
    Posts
    63

    Re: Call VB6 module function dynamically

    Quote Originally Posted by JAAFAR View Post
    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
  •  



Click Here to Expand Forum to Full Width