Hello all,
I'm trying to upgrade a VB6 project into VB.net, and the problem I had is in converting 'address of' to 'delegate'.
I had the error "Value of type 'DelegateIDccManSink_OnLogIpAddr' cannot be converted to 'integer'.",
when calling this line, ReplaceVtableEntry(.., .., callbackOnLogIpAddr).
I guess the problem is due to the VB6 expected a memory address, but the 'delegate' doesn't return an address.
Any ideas are appreciated.
- Public Delegate Function DelegateIDccManSink_OnLogIpAddr(ByVal this As KwyDccMan.IDccManSink, ByVal dwIpAddr As Integer) As Integer
-
-
-
Public Sub DccManSinkInitialize(ByVal this As KwyDccMan.IDccManSink)
-
-
Dim lObjAddress As Integer
-
Dim callbackOnLogIpAddr As DelegateIDccManSink_OnLogIpAddr
-
-
' Object Address
-
lObjAddress = ObjPtr(this)
-
-
callbackOnLogIpAddr = New DelegateIDccManSink_OnLogIpAddr(AddressOf IDccManSink_OnLogIpAddr)
-
-
' Replace VTable Function Entries
-
-
m_lFnOnLogIpAddr = ReplaceVtableEntry(lObjAddress, &H4S, callbackOnLogIpAddr)
-
-
End Sub
-
-
Private Function IDccManSink_OnLogIpAddr(ByVal this As KwyDccMan.IDccManSink, ByVal dwIpAddr As Integer) As Integer
-
IDccManSink_OnLogIpAddr = SCDCallParams(ObjPtr(this), EDccManCallID.eCallIDSink_OnLogIpAddr, dwIpAddr)
-
End Function
-
-
' Put the function address (callback) directly into the object v-table
-
Public Function ReplaceVtableEntry(ByVal lpObj As Integer, ByVal nEntry As Short, ByVal lpFunc As Integer) As Integer
-
' lpObj - Pointer to object whose v-table will be modified
-
' nEntry - Index of v-table entry to be modified
-
' lpFunc - Function pointer of new v-table method
-
-
Dim lpFuncOld As Integer, lpVTableHead As Integer, lpFuncTmp As Integer, nOldProtect As Integer
-
-
' Object pointer contains a pointer to v-table--copy it to temporary
-
CopyMemory(lpVTableHead, lpObj, 4)
-
' Calculate pointer to specified entry
-
lpFuncTmp = lpVTableHead + (nEntry - 1) * 4
-
-
' Save address of previous method for return
-
CopyMemory(lpFuncOld, lpFuncTmp, 4)
-
-
' Ignore if they're already the same
-
If lpFuncOld <> lpFunc Then
-
' Need to change page protection to write to code
-
VirtualProtect(lpFuncTmp, 4&, PAGE_EXECUTE_READWRITE, nOldProtect)
-
-
' Write the new function address into the v-table
-
CopyMemory(lpFuncTmp, lpFunc, 4) ' *lpFuncTmp = lpFunc;
-
-
' Restore the previous page protection
-
VirtualProtect(lpFuncTmp, 4, nOldProtect, nOldProtect) 'Optional
-
End If
-
-
ReplaceVtableEntry = lpFuncOld
-
-
End Function