Asynchronous Callback Functions
Perspective: I am updating several VB6 ActiveX applications to C # .net, which all communicate with each other using the callback functions that they register using the standard VC ++ executable.
I cannot replicate the following VB6 functions in C #: The ability of VB6 to switch to VC ++, an instance of the class containing the method, as a parameter to the callback function, which VC ++ then registers as a callback function for asynchronous communication.
The update is very different from this problem: CallBack features
... and I am stuck on it now for two weeks. Please help me!
I figured out how to pass the callback function as a delegate, which I managed to work with C # DynamicInvoke, however I really need this to work in VC ++.
Message . I continue to receive from the VC ++ operator invoke "Invalid number of parameters . "
BELOW I have outlined the functions of VB6 and VC ++ that handle asynchronous callbacks. VB6 ActiveX components pass a class containing a single method as a callback function in VC ++ Executable, which stores callbacks in an array for later use. Since this is existing code, it works as expected.
The following is an instance of the VB6 Class Class1and is used as a callback:
Note:Attribute Notify.VB_UserMemId = 0
VERSION 1.0 CLASS
BEGIN
MultiUse = -1
Persistable = 0
DataBindingBehavior = 0
DataSourceBehavior = 0
MTSTransactionMode = 0
END
Attribute VB_Name = "Class1"
Attribute VB_GlobalNameSpace = False
Attribute VB_Creatable = True
Attribute VB_PredeclaredId = False
Attribute VB_Exposed = True
Sub Notify(ByVal message As Integer, ByVal data As Variant)
Attribute Notify.VB_UserMemId = 0
MsgBox Str$(message) + " " + data, vbInformation, Form1.Text2
End Sub
, .
VB6, V++ (VCCallbackHandler) Class1
Dim VCCallbackHandler New VCCallbackHandler.VCCallbackHandler
Dim c1 As New Class1
Private Sub Register_Click()
Dim i as int
i = VCCallbackHandler.Register(c1, "NameOfApplication")
End Sub
V++ (. ), () V++ , - (. "BroadCast" ). V++ exe . V++, callbackHandler V++, , . , .
V++. Net.
:
#define MAXREG 20
typedef struct tagRegElement {
char name[20];
_Callback *p_Callback;
} REG_ELEMENT;
public:
REG_ELEMENT Registry[MAXREG];
short CBreqDlgAutoProxy::Register(LPDISPATCH callback, LPCTSTR name)
{
for (int i = 0;i<MAXREG;i++){
if(!(theApp.Registry[i].name[0]))
{
RegIndex = i;
strcpy(theApp.Registry[i].name,name);
theApp.Registry[i].p_Callback = new _Callback(callback);
return i;
}
}
return -1;
}
:
BOOL CBreqDlgAutoProxy::Broadcast(short message, const VARIANT FAR& data)
{
for (int i = 0;i<MAXREG;i++){
if(theApp.Registry[i].name[0] && (i != RegIndex)){
if (!theApp.Registry[i].p_Callback->Notify(message,data,theApp.Registry[i].name))
DeRegister(i);
}
}
return TRUE;
}
BOOL _Callback::Notify(short message, VARIANT data, char* RegisteredName)
{
static BYTE parms[] = VTS_I2 VTS_VARIANT;
InvokeHelper(0x0, DISPATCH_METHOD, VT_EMPTY, NULL, parms, message, &data);
return TRUE;
}
. .
:
- #: # . , , , V++ , .
- V++: V++ .
# : `
Marshal.GetFunctionPointerForDelegateGCHandleKeepAlive
, - , ... SNAP... .. ... .