Connecting Tech Pros Worldwide Forums | Help | Site Map

[VB6 to VB.net] Error in converting 'address of' to 'delegate'

Newbie
 
Join Date: Oct 2008
Posts: 1
#1: Oct 8 '08
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.


Expand|Select|Wrap|Line Numbers
  1.     Public Delegate Function DelegateIDccManSink_OnLogIpAddr(ByVal this As KwyDccMan.IDccManSink, ByVal dwIpAddr As Integer) As Integer
  2.  
  3.  
  4.     Public Sub DccManSinkInitialize(ByVal this As KwyDccMan.IDccManSink)
  5.  
  6.         Dim lObjAddress As Integer
  7.         Dim callbackOnLogIpAddr As DelegateIDccManSink_OnLogIpAddr
  8.  
  9.         ' Object Address
  10.         lObjAddress = ObjPtr(this)
  11.  
  12.         callbackOnLogIpAddr = New DelegateIDccManSink_OnLogIpAddr(AddressOf IDccManSink_OnLogIpAddr)
  13.  
  14.         ' Replace VTable Function Entries
  15.  
  16.         m_lFnOnLogIpAddr = ReplaceVtableEntry(lObjAddress, &H4S, callbackOnLogIpAddr)
  17.  
  18.     End Sub
  19.  
  20.     Private Function IDccManSink_OnLogIpAddr(ByVal this As KwyDccMan.IDccManSink, ByVal dwIpAddr As Integer) As Integer
  21.         IDccManSink_OnLogIpAddr = SCDCallParams(ObjPtr(this), EDccManCallID.eCallIDSink_OnLogIpAddr, dwIpAddr)
  22.     End Function
  23.  
  24.     ' Put the function address (callback) directly into the object v-table
  25.     Public Function ReplaceVtableEntry(ByVal lpObj As Integer, ByVal nEntry As Short, ByVal lpFunc As Integer) As Integer
  26.         ' lpObj - Pointer to object whose v-table will be modified
  27.         ' nEntry - Index of v-table entry to be modified
  28.         ' lpFunc - Function pointer of new v-table method
  29.  
  30.         Dim lpFuncOld As Integer, lpVTableHead As Integer, lpFuncTmp As Integer, nOldProtect As Integer
  31.  
  32.         ' Object pointer contains a pointer to v-table--copy it to temporary
  33.         CopyMemory(lpVTableHead, lpObj, 4)          
  34.         ' Calculate pointer to specified entry
  35.         lpFuncTmp = lpVTableHead + (nEntry - 1) * 4
  36.  
  37.         ' Save address of previous method for return
  38.         CopyMemory(lpFuncOld, lpFuncTmp, 4)        
  39.  
  40.         ' Ignore if they're already the same
  41.         If lpFuncOld <> lpFunc Then
  42.             ' Need to change page protection to write to code
  43.             VirtualProtect(lpFuncTmp, 4&, PAGE_EXECUTE_READWRITE, nOldProtect)
  44.  
  45.             ' Write the new function address into the v-table
  46.             CopyMemory(lpFuncTmp, lpFunc, 4)      ' *lpFuncTmp = lpFunc;
  47.  
  48.             ' Restore the previous page protection
  49.             VirtualProtect(lpFuncTmp, 4, nOldProtect, nOldProtect)    'Optional
  50.         End If
  51.  
  52.         ReplaceVtableEntry = lpFuncOld
  53.  
  54.     End Function
Reply


Similar .NET Framework bytes