Results 1 to 12 of 12

Thread: [RESOLVED] Callback a Sub from a DLL.

Hybrid View

  1. #1

    Thread Starter
    I don't do your homework! opus's Avatar
    Join Date
    Jun 2000
    Location
    Good Old Europe
    Posts
    3,863

    Resolved [RESOLVED] Callback a Sub from a DLL.

    Hi, I'm trying to use a .DLL which Callbacks a Function of my project, but I can't get it to work.

    Declaration from .h file of .DLL
    Code:
    void DLL_EXPORT SetEntity_CallBack(bool Active, _
    void (*Entity_Callback)(int Site, int Application, int ID, _
    char* Name, int scope, int Change));
    Declaration in my Mainform Class

    Code:
    Public Delegate Sub DelEntityCallBack(ByVal Site As Integer, _
    ByVal Application As Integer, ByVal ID As Integer, _
    ByRef Name As String, ByVal Scope as Integer, _
    ByVal Change As Integer)
    <DllImport("DIS_LIB.DLL", EntryPoint:="SetEntity_Callback", _
    SetLastError:=True, CharSet:=CharSet.Ansi, ExactSpelling:=True, _
    CallingConvention:=CallingConvention.Cdecl)> _
    Public Shared Sub SetEntity_CallBack(ByVal Active As Boolean, ByRef CallBackAddr as DelEntityCallBack)
    End Sub
    The Sub that is CalledBack is declared as follows:
    Code:
    Public Sub ExternEntity_Update (ByVal Site As Integer, _
    ByVal Application As Integer, ByVal ID As Integer, _
    ByRef Name As String, ByVal Scope as Integer, _
    ByVal Change As Integer)
    ...
    ....
    End Sub
    and the DLL-code gets called like:

    Code:
    SetEntity_CallBack(True, AddressOf ExternEntity_Update)
    When trying to run the code I get an "EntryPointNotFoundExecption" for the EntryPoint "SetEntity_Callback".
    You're welcome to rate this post!
    If your problem is solved, please use the Mark thread as resolved button


    Wait, I'm too old to hurry!

  2. #2
    Angel of Code Niya's Avatar
    Join Date
    Nov 2011
    Posts
    9,017

    Re: Callback a Sub from a DLL.

    It could be that the name of the function name is mangled in the DLL. Would you mind uploading the DLL so I can look at it if its not too much trouble ?
    Treeview with NodeAdded/NodesRemoved events | BlinkLabel control | Calculate Permutations | Object Enums | ComboBox with centered items | .Net Internals article(not mine) | Wizard Control | Understanding Multi-Threading | Simple file compression | Demon Arena

    Copy/move files using Windows Shell | I'm not wanted

    C++ programmers will dismiss you as a cretinous simpleton for your inability to keep track of pointers chained 6 levels deep and Java programmers will pillory you for buying into the evils of Microsoft. Meanwhile C# programmers will get paid just a little bit more than you for writing exactly the same code and VB6 programmers will continue to whitter on about "footprints". - FunkyDexter

    There's just no reason to use garbage like InputBox. - jmcilhinney

    The threads I start are Niya and Olaf free zones. No arguing about the benefits of VB6 over .NET here please. Happiness must reign. - yereverluvinuncleber

  3. #3

    Thread Starter
    I don't do your homework! opus's Avatar
    Join Date
    Jun 2000
    Location
    Good Old Europe
    Posts
    3,863

    Re: Callback a Sub from a DLL.

    I'm sorry, but I can't upload the .DLL nor the Header and Source File.
    Other Functions of this DLL are being correctly called and procecuted using similar "DllImport" lines.
    The new feature is the CallBack to a Sub of my Form, although the execption is pointing towards a miss-spelling.
    The use of such a CallBack is proven, I'm the first to use it with VB.Net.
    You're welcome to rate this post!
    If your problem is solved, please use the Mark thread as resolved button


    Wait, I'm too old to hurry!

  4. #4
    Angel of Code Niya's Avatar
    Join Date
    Nov 2011
    Posts
    9,017

    Re: Callback a Sub from a DLL.

    So you think its a misspelling ? I believe entry point lookups are case-sensitive. Make sure you have the casing right.
    Treeview with NodeAdded/NodesRemoved events | BlinkLabel control | Calculate Permutations | Object Enums | ComboBox with centered items | .Net Internals article(not mine) | Wizard Control | Understanding Multi-Threading | Simple file compression | Demon Arena

    Copy/move files using Windows Shell | I'm not wanted

    C++ programmers will dismiss you as a cretinous simpleton for your inability to keep track of pointers chained 6 levels deep and Java programmers will pillory you for buying into the evils of Microsoft. Meanwhile C# programmers will get paid just a little bit more than you for writing exactly the same code and VB6 programmers will continue to whitter on about "footprints". - FunkyDexter

    There's just no reason to use garbage like InputBox. - jmcilhinney

    The threads I start are Niya and Olaf free zones. No arguing about the benefits of VB6 over .NET here please. Happiness must reign. - yereverluvinuncleber

  5. #5

    Thread Starter
    I don't do your homework! opus's Avatar
    Join Date
    Jun 2000
    Location
    Good Old Europe
    Posts
    3,863

    Re: Callback a Sub from a DLL.

    Is my declaration of the Delegate and the DllImport as a Sub (instead of a Function) correct?
    I used Sub because no return value ("void") is expected.
    You're welcome to rate this post!
    If your problem is solved, please use the Mark thread as resolved button


    Wait, I'm too old to hurry!

  6. #6
    Angel of Code Niya's Avatar
    Join Date
    Nov 2011
    Posts
    9,017

    Re: Callback a Sub from a DLL.

    Yes.....void is a sub, its correct. If I can get my hands on the DLL, I'm sure I can help you better. Can I find that DLL somewhere on the net ? What does it do ? What is it for ?
    Treeview with NodeAdded/NodesRemoved events | BlinkLabel control | Calculate Permutations | Object Enums | ComboBox with centered items | .Net Internals article(not mine) | Wizard Control | Understanding Multi-Threading | Simple file compression | Demon Arena

    Copy/move files using Windows Shell | I'm not wanted

    C++ programmers will dismiss you as a cretinous simpleton for your inability to keep track of pointers chained 6 levels deep and Java programmers will pillory you for buying into the evils of Microsoft. Meanwhile C# programmers will get paid just a little bit more than you for writing exactly the same code and VB6 programmers will continue to whitter on about "footprints". - FunkyDexter

    There's just no reason to use garbage like InputBox. - jmcilhinney

    The threads I start are Niya and Olaf free zones. No arguing about the benefits of VB6 over .NET here please. Happiness must reign. - yereverluvinuncleber

  7. #7

    Thread Starter
    I don't do your homework! opus's Avatar
    Join Date
    Jun 2000
    Location
    Good Old Europe
    Posts
    3,863

    Re: Callback a Sub from a DLL.

    The DLL is build by a CoWorker.
    I found my stupid error, it wasn't a typo, but I was using the .DLL out of the /Debug folder while I should have used the one from the /Release folder ().

    Using the correct .DLL my application is just crashing. When changing the CallingConvention to StdCall I get an PInvokeStackImbalance.
    You're welcome to rate this post!
    If your problem is solved, please use the Mark thread as resolved button


    Wait, I'm too old to hurry!

  8. #8
    Angel of Code Niya's Avatar
    Join Date
    Nov 2011
    Posts
    9,017

    Re: Callback a Sub from a DLL.

    Quote Originally Posted by opus View Post
    ...but I was using the .DLL out of the /Debug folder while I should have used the one from the /Release folder ().
    Does this mean you have the source for the DLL ? If so, can you post the define for DLL_EXPORT and the code for the function ?

    I'd expect DLL_EXPORT to look something like:-
    C Code:
    1. #define DLL_EXPORT extern "C" __declspec( dllexport )

    However, its position in the function prototype seems to be wrong which means it may be defined differently. I'd really like to see it.

    Also, if you do have the source, you could try removing DLL_EXPORT and replacing it with _stdcall and export the function using a .DEF file instead.
    Last edited by Niya; Jun 20th, 2012 at 02:18 AM.
    Treeview with NodeAdded/NodesRemoved events | BlinkLabel control | Calculate Permutations | Object Enums | ComboBox with centered items | .Net Internals article(not mine) | Wizard Control | Understanding Multi-Threading | Simple file compression | Demon Arena

    Copy/move files using Windows Shell | I'm not wanted

    C++ programmers will dismiss you as a cretinous simpleton for your inability to keep track of pointers chained 6 levels deep and Java programmers will pillory you for buying into the evils of Microsoft. Meanwhile C# programmers will get paid just a little bit more than you for writing exactly the same code and VB6 programmers will continue to whitter on about "footprints". - FunkyDexter

    There's just no reason to use garbage like InputBox. - jmcilhinney

    The threads I start are Niya and Olaf free zones. No arguing about the benefits of VB6 over .NET here please. Happiness must reign. - yereverluvinuncleber

  9. #9

    Thread Starter
    I don't do your homework! opus's Avatar
    Join Date
    Jun 2000
    Location
    Good Old Europe
    Posts
    3,863

    Re: Callback a Sub from a DLL.

    No Problem:
    as posted above in the Header-File:
    Code:
    void DLL_EXPORT SetEntity_Callback(bool Active, void (*Entity_Callback)(int Site, int Application, int ID, char* Name, int Scope, int Change));
    and in the SourceCode:
    Code:
    void DLL_EXPORT SetEntity_Callback(bool Active, void (*Entity_Callback)(int Site, int Application, int ID, char* Name, int Scope, int Change))
    {
        CData_Manager::Get()->SetEntity_CB(Active, Entity_Callback);
    };
    Changing the SourceCode of the DLL seems to be no solution, since it's working for all others as supposed to(they are using C++), I'm the only one sing it in vb.Net
    You're welcome to rate this post!
    If your problem is solved, please use the Mark thread as resolved button


    Wait, I'm too old to hurry!

  10. #10

    Thread Starter
    I don't do your homework! opus's Avatar
    Join Date
    Jun 2000
    Location
    Good Old Europe
    Posts
    3,863

    Re: Callback a Sub from a DLL.

    No luck so far.
    I changed the "CallingConvention" from CDecl to StdCall ( only for the caled Sub from the DLL and for the DLL-Sub and the Delegate). It either just crashes(CDecl) or raises a PInvokeStackImbalance (StdCall).
    Could it be the type of variables I used within the Delegate and the callbacked Sub?
    You're welcome to rate this post!
    If your problem is solved, please use the Mark thread as resolved button


    Wait, I'm too old to hurry!

  11. #11

    Thread Starter
    I don't do your homework! opus's Avatar
    Join Date
    Jun 2000
    Location
    Good Old Europe
    Posts
    3,863

    Re: Callback a Sub from a DLL.

    Heureka!

    I shouldn't have use ByRef for the declaration of DLL-Sub for the Delegate
    Code:
    <DllImport("DIS_LIB.DLL", EntryPoint:="SetEntity_Callback", _
    SetLastError:=True, CharSet:=CharSet.Ansi, ExactSpelling:=True, _
    CallingConvention:=CallingConvention.Cdecl)> _
    Public Shared Sub SetEntity_CallBack(ByVal Active As Boolean, ByVal CallBackAddr as DelEntityCallBack)
    End Sub
    Beats me, but it works now!
    You're welcome to rate this post!
    If your problem is solved, please use the Mark thread as resolved button


    Wait, I'm too old to hurry!

  12. #12
    Angel of Code Niya's Avatar
    Join Date
    Nov 2011
    Posts
    9,017

    Re: [RESOLVED] Callback a Sub from a DLL.

    Sorry for being so late....I went offline a little after my last post in this thread. Glad you got it solved. Your solution makes sense though as pointer types like IntPtr and SafeHandle are normally passed by value. A delegate is a type of pointer too.
    Treeview with NodeAdded/NodesRemoved events | BlinkLabel control | Calculate Permutations | Object Enums | ComboBox with centered items | .Net Internals article(not mine) | Wizard Control | Understanding Multi-Threading | Simple file compression | Demon Arena

    Copy/move files using Windows Shell | I'm not wanted

    C++ programmers will dismiss you as a cretinous simpleton for your inability to keep track of pointers chained 6 levels deep and Java programmers will pillory you for buying into the evils of Microsoft. Meanwhile C# programmers will get paid just a little bit more than you for writing exactly the same code and VB6 programmers will continue to whitter on about "footprints". - FunkyDexter

    There's just no reason to use garbage like InputBox. - jmcilhinney

    The threads I start are Niya and Olaf free zones. No arguing about the benefits of VB6 over .NET here please. Happiness must reign. - yereverluvinuncleber

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