dcsimg
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Page 1 of 2 12 LastLast
Results 1 to 15 of 19

Thread: How to call the function from the pointer

  1. #1
    Join Date
    Jan 2002
    Location
    Rio
    Posts
    53

    How to call the function from the pointer

    Hi,

    I have a VB Aplication that is an ActiveX Exe. It can receive from another program, a pointer to a function which I want to use as a callback. So now I have the pointer to the function... How do I call it?

    I've tried following the instructions in this page:
    http://www.devx.com/upload/free/feat...200/mc0200.asp
    But at the last moment, when I call the function, it says "memory cannot be written", and then VB crashes (VB, not just the application), it doesn't even go to my ErrorHandler... It's ugly.

    This page shows a (in my opinion) very complicated way to call the funcion when I have the pointer to it. I almost got it to work. So, if you can help me get past the "almost", or suggest a completely different method, I'm very grateful either way.

    Thanx!
    - Juliana

  2. #2
    Join Date
    Aug 2001
    Posts
    1,447
    this might just make things MORE complicated, but I'd pass the pointer to a function in a C-based DLL & call it from there since C is perfectly happy to call functions based on pointers.

  3. #3
    Join Date
    Jan 2002
    Location
    Rio
    Posts
    53
    This just keeps getting better... Now I've tried doing exactly the same thing, but instead of using the function I received from the other program, I used one in the same program just for testing.

    It worked.

    This is useless because if the function is in the same program I would just call it, not do this whole complicated thing...

    So the problem seems to be the fact that I am calling a function that belongs to a different program. But I should be able to do that... right?

  4. #4
    Join Date
    Aug 2002
    Location
    Germany
    Posts
    15

    should work ....

    I used Callbacks also , but I GAVE the other program a Pointer to my own Callback function in VB. So that the other Program can call my function. Thats the way I know that it will work.

    And I know this message and VB crashes everytime , if something with the declaration of the Parameters is wrong.
    Best regards
    Markus

  5. #5
    Join Date
    Sep 2001
    Location
    Montreal Canada
    Posts
    1,080
    I might be very wrong about this but this is an intuition I got.


    You have a dll and an application.

    You're passing a pointer to a function to your vb app. That's where I might be wrong here but here's how I see the problem.


    By passing a pointer, you get an address. The problem is that address might be relative to the start of your dll address space in memory. This might means that when you send the pointer to the vb app. It then tries to access that function in ITS address space because the address received is relative.


    But then I might also be wrong about that, meaning that the address might be absolute. If it's the case, then I suspect that the Memory protection is kicking in and don't let you access another address space in your vb app.

    So I would investigate on these intuitions I have. But like I said I might be very wrong. I was just trying to help

    Thanx.
    Nicolas Bohemier

  6. #6
    Join Date
    Jan 2002
    Location
    Rio
    Posts
    53
    This makes sense.
    You're only wrong in assuming it's a dll and an application, actually they are both applications, but it doesn't make a difference anyway.
    What should I do to find out whether the address is relative or absolute? And in case it's relative, then how do I get the "correct" pointer?

  7. #7
    Join Date
    Aug 2001
    Posts
    1,447
    I also might be wrong, but I believe that relative pointers went out with 16-bit processes. 32-bit process use don't use a "code segment" then way the old 16-bit processes did, so there is no "relative" to the address.

  8. #8
    Join Date
    Jun 2001
    Location
    MO, USA
    Posts
    2,868
    Compile the following and run two copies of the program at the same time... you will see that they both report the same address.

    Private Sub Command1_Click()
    MsgBox AddressOf Test
    End Sub

    'In a Module
    Public Sub Test()

    End Sub

  9. #9
    Join Date
    Sep 2001
    Location
    Montreal Canada
    Posts
    1,080
    This doesn'T prove anything DSJ. Both function might be at the same relative address in their respective address space.

    Only a static object might help us out on this.

    But as I said I might be very wrong about this.
    Nicolas Bohemier

  10. #10
    Join Date
    Jun 2001
    Location
    MO, USA
    Posts
    2,868
    I was responding to the message immediately above my statment. The address returned by AddressOf is only valid within the context of the current application (executable & DLLs)...

  11. #11
    Join Date
    Sep 2001
    Location
    Montreal Canada
    Posts
    1,080
    Then I apologize. I'm just trying to help this guy out like you do I guess
    Nicolas Bohemier

  12. #12
    Join Date
    Jan 2002
    Location
    Rio
    Posts
    53
    Hey,

    not a guy, I am a girl

    Anyway, DSJ showed that AddressOf gives a relative address... So is it even possible to use a pointer to a function that is in another program?

    I think it's time I start thinking about another solution that does not involve callbacks. Oh well.

  13. #13
    Join Date
    Jun 2001
    Location
    MO, USA
    Posts
    2,868

    Wink

    Now we HAVE to find a solution, not often we get gals to help!

    I re-read your original post. If you're ActiveX exe needs to "talk" to the calling app you could raise an event instead of using a callback. It should provide the same functionality.

  14. #14
    Join Date
    Dec 2001
    Posts
    6,332
    This may not be of any help to you, but have you looked into the CallByName function?
    Please remember to rate the posts and threads that you find useful.
    How can something be both new and improved at the same time?

  15. #15
    Join Date
    Jan 2002
    Location
    Rio
    Posts
    53
    My VB doesn't seem to know CallByName, maybe it's a VB6 thing, I'm using VB5. What is it supposed to do?

    Yes, I think raising an event will be better. How can I do that?

Page 1 of 2 12 LastLast

Posting Permissions

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


Windows Mobile Development Center


Click Here to Expand Forum to Full Width




On-Demand Webinars (sponsored)