dcsimg
Page 2 of 2 FirstFirst 12
Results 41 to 48 of 48

Thread: [VB6] Call Functions By Pointer (Universall DLL Calls)

  1. #41
    Lively Member
    Join Date
    Apr 2015
    Posts
    77

    Re: [VB6] Call Functions By Pointer (Universall DLL Calls)

    Dear Lavolpe,

    Swapping of vtables of member functions works fine provided we find their correct vtable offset.Regarding
    offsets you helped a lot to find out offset of member functions using zProbe for which I am thankful to you.

    But when I try to replace vtables of member functions with different protrotypes with different parameters and different return types the application crashes.

    Can you provide me

    1.a generic example code of how to handle this issue without crashing the application.

    2.how to handle different calling conventions _cdecl in VB without crash when I replace the vtable member function with module function.

    You can help me using my old class_vs_forms_vtable.zip which I am attaching.
    It is a standard exe project with member functions having same prototype.

    Thanking you once again.
    regards,
    Jsvenu
    Attached Files Attached Files

  2. #42
    PowerPoster Elroy's Avatar
    Join Date
    Jun 2014
    Location
    Near Nashville TN
    Posts
    5,295

    Re: [VB6] Call Functions By Pointer (Universall DLL Calls)

    EDIT1: Hey, going through DispCallFunc has become pretty moot for what I was trying to do. Other far superior alternatives have emerged. So ... you can ignore this, if you like. Also, I haven't seen you around for a few days. I hope all is well. I'll imagine you're enjoying a piña colada on some Caribbean beach until I hear otherwise.


    Say Keith,

    I suspect you've been watching what I'm messing with regarding the Cairo Graphics. I'm just wondering how this DispCallFunc wrapper (for CDECL) would handle a String return. For instance...

    Code:
    
    cairo_public const char *
    cairo_status_to_string (cairo_status_t status);
    
    

    ...how could we handle that? I scanned the posts in this thread and didn't see anything, although I could have missed it. Also, the "status" is just a Long. It seems that this library has a typedef for just about everything.

    Also, at the moment, I can't tell you whether it's ANSI (or ASCII) or Unicode. If it's Unicode, it's probably UTF-8, but that's yet another problem. First, I just need to get it into VB6, and then I'll figure out how to "translate" it to UCS-2-Unicode. I don't think these Cairo functions are anything where speed would be a concern, but there are a handful of them that return Strings.

    Thanks,
    Elroy
    Last edited by Elroy; Aug 17th, 2018 at 08:53 AM.
    Any software I post in these forums written by me is provided “AS IS” without warranty of any kind, expressed or implied, and permission is hereby granted, free of charge and without restriction, to any person obtaining a copy. Please understand that I’ve been programming since the mid-1970s and still have some of that code. My contemporary VB6 project is approaching 1,000 modules. In addition, I have a “VB6 random code folder” that is overflowing. I’ve been at this long enough to truly not know with absolute certainty from whence every single line of my code has come, with much of it coming from programmers under my employ who signed intellectual property transfers. I have not deliberately attempted to remove any licenses and/or attributions from any software. If someone finds that I have inadvertently done so, I sincerely apologize, and, upon notice and reasonable proof, will re-attach those licenses and/or attributions. To all, peace and happiness.

  3. #43
    PowerPoster
    Join Date
    Jun 2015
    Posts
    2,224

    Re: [VB6] Call Functions By Pointer (Universall DLL Calls)

    @jsvenu - You can't replace vtable functions with different function prototypes.. (different parameter counts or parameter types)
    That breaks the interface contract, which every call is expecting.
    Every COM Object is required to abide by an agreed upon interface definition.

  4. #44

    Thread Starter
    VB-aholic & Lovin' It LaVolpe's Avatar
    Join Date
    Oct 2007
    Location
    Beside Waldo
    Posts
    18,026

    Re: [VB6] Call Functions By Pointer (Universall DLL Calls)

    Just FYI: DispCallFunc can handle CDECL, it does more than just StdCall. The convention type is one of the parameters to that API. A helper/thunk may be needed for CDECL callbacks into a VB project, but I don't think you were asking about that.
    Insomnia is just a byproduct of, "It can't be done"

    Classics Enthusiast? Here's my 1969 Mustang Mach I Fastback. Her sister '67 Coupe has been adopted

    Newbie? Novice? Bored? Spend a few minutes browsing the FAQ section of the forum.
    Read the HitchHiker's Guide to Getting Help on the Forums.
    Here is the list of TAGs you can use to format your posts
    Here are VB6 Help Files online


    {Alpha Image Control} {Memory Leak FAQ} {Unicode Open/Save Dialog} {Resource Image Viewer/Extractor}
    {VB and DPI Tutorial} {Manifest Creator} {UserControl Button Template} {stdPicture Render Usage}

  5. #45
    Member
    Join Date
    Jan 2018
    Posts
    44

    Re: [VB6] Call Functions By Pointer (Universall DLL Calls)

    I was looking back into doing some CDECL calls, and tried this out. Surprisingly, it didn't work. The comments mention calling methods by ordinal, but I don't see a way to do that. One of Paul Caton's procedures had a simple solution:

    Code:
    Private Declare Function GetProcByOrdinal Lib "kernel32" Alias "GetProcAddress" (ByVal hModule As Long, ByVal nOrdinal As Long) As Long
    .
    .
        If Left$(FunctionName, 1) = "#" Then
            fPtr = GetProcByOrdinal(hMod, CLng(Mid$(FunctionName, 2))) ' get the function pointer by ordinal #
        Else
            fPtr = GetProcAddress(hMod, FunctionName)           ' get the function pointer (Case-Sensitive)
        End If

  6. #46
    New Member
    Join Date
    Apr 2019
    Posts
    2

    Exclamation Re: [VB6] Call Functions By Pointer (Universall DLL Calls) - VarType issues

    Quote Originally Posted by LaVolpe View Post
    ...
    The attachment includes very simple examples of calling DLL functions and calling a COM virtual function.
    ...
    Thanks for providing this code.

    Just a word of warning to VBA developers, and other developers that access a VarType function that has the same 'unusual' behaviour that VBA's VarType function has, you may have to use code similar to the following code to properly get each variable's type:
    Code:
    vParamType(pIndex) = IIf(TypeOf vParams(pIndex) Is Object , _
                                           VbVarType.vbObject, _
                                           VarType(vParams(pIndex))                                             
                                          )
    The reason why this may be the case, can be garnered by looking at the StackOverflow question & answers posted here.

    I've created a pull request shown here, to make VBA's VarType documentation more clear regarding these issues.

    I couldn't tell whether VB6 was affected by these issues. I think the VB6 documentation for VB6's VarType function might be shown here, but I couldn't tell for certain.

    EDIT: Have now discovered that official VB6 documentation is hosted by Microsoft here.

    Regards.
    Last edited by MarkFern; Yesterday at 06:52 AM. Reason: 1. Coding mistake. 2. Found VB6 documentation.

  7. #47
    Frenzied Member wqweto's Avatar
    Join Date
    May 2011
    Posts
    1,218

    Re: [VB6] Call Functions By Pointer (Universall DLL Calls)

    @MarkFern: TypeOf VarType(vParams(pIndex)) Is Object makes no sense. Just use IsObject function like this:

    Code:
        If IsObject(vParams(pIndex)) Then
            vParamType(pIndex) = vbObject
        Else
            vParamType(pIndex) = VarType(vParams(pIndex))
        End If
    Note that converting this to a single expression w/ IIf has the side-effect of always evaluating VarType(vParams(pIndex)) which has the side-effect of always calling the default property/method of the object which for Word.Application instances can be an expensive out-of-process call.

    VarType is not unique and evaluating default property is a standard behaviour, even built-in Array(oMyInstance1, oMyInstance2, ...) does it and this is very welcome side-effect because when you have an instance of a Recordset for instance and you try to wrap Array(rs!ID, rs!Name, rs!Address) you don't want the ADODB.Fields in this array but only their default Value property. If you pass the naked ADODB.Fields you risk rs.MoveNext actually *changing* the values in the array. Now *that* would be a big surprise!

    cheers,
    </wqw>

  8. #48
    New Member
    Join Date
    Apr 2019
    Posts
    2

    Red face Re: [VB6] Call Functions By Pointer (Universall DLL Calls)

    @wqweto: Thanks for your useful reply.

    Quote Originally Posted by wqweto View Post
    @MarkFern: TypeOf VarType(vParams(pIndex)) Is Object makes no sense...
    I made a coding mistake - should have written TypeOf vParams(pIndex) Is Object instead. TypeOf... Is Object works in VBA - it's documented here. I would have researched what code to write for VB6 but I couldn't figure out for certain where Micorosft stores the VB6 documentation. IsObject() could be more efficient though - don't know.

    EDIT: Have now discovered that official VB6 documentation is hosted by Microsoft here.

    Thank you for pointing out the side-effect of IIf. I never thought about this before but of course it makes perfect sense. I just wanted to abbreviate my code more by using the function - didn't think about the side-effect.

    Quote Originally Posted by wqweto View Post
    ...
    VarType is not unique and evaluating default property is a standard behaviour, even built-in Array(oMyInstance1, oMyInstance2, ...) does it and this is very welcome side-effect because when you have an instance of a Recordset for instance and you try to wrap Array(rs!ID, rs!Name, rs!Address) you don't want the ADODB.Fields in this array but only their default Value property. ...
    I can understand the benefit in regard to code like rs!ID but to me, such behaviour in VarType() is counter-intuitive. It tripped-up myself and probably also LaVolpe. However, I completely admit that perhaps I don't understand VB & COM well enough to understand the benefits of such functionality. Thank you so much for explaining more about this.

    Thanks.
    Last edited by MarkFern; Yesterday at 06:55 AM. Reason: Discovered where VB6 documentation is officially stored.

Page 2 of 2 FirstFirst 12

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Featured


Click Here to Expand Forum to Full Width