[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".
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 ?
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.
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.
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.
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 ?
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 (:blush::blush:).
Using the correct .DLL my application is just crashing. When changing the CallingConvention to StdCall I get an PInvokeStackImbalance.
Re: Callback a Sub from a DLL.
Quote:
Originally Posted by
opus
...but I was using the .DLL out of the /Debug folder while I should have used the one from the /Release folder (:blush::blush:).
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:
#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.
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
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?
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!
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.