[API]
VirtualAlloc
VirtualProtect
VirtualFree
RtlMoveMemory
CallWindowProcA

[INCLUDE]
UIntAddUInt
Application

[CODE]
Public Function CreateCodeFromHexAssembly(ByRef ptrExec As Long, ByRef ptrMem As Long, ByRef ByteCode As String) As Boolean
  Dim xLong1 As Long
  Dim xLong2 As Long
  Dim xByteA1() As Byte
  
  xLong1 = Len(ByteCode)
  ReDim xByteA1(xLong1 - 1&)
  Call RtlMoveMemory(xByteA1(0), ByVal ByteCode, xLong1)
  xLong1 = xLong1 \ 2&
  ptrMem = VirtualAlloc(0&, xLong1 + 15&, &H1000&, 4&)
  If Not ptrMem = 0& Then
    ptrExec = ptrMem
    Call UIntAddUInt(ptrExec, 15&)
    ptrExec = (ptrExec And &HFFFFFFF0)
    For xLong2 = 0& To xLong1 - 1&
      xByteA1(xLong2) = CreateCodeFromHexAssembly_UnHexByte(xByteA1(xLong2 * 2&)) * 16& + CreateCodeFromHexAssembly_UnHexByte(xByteA1(xLong2 * 2& + 1&))
      Next
    Call RtlMoveMemory(ByVal ptrExec, xByteA1(0), xLong1)
    If VirtualProtect(ByVal ptrMem, xLong1 + 15&, &H20&, 0&) = 0& Then
      Call VirtualFree(ptrMem, 0&, &H8000&)
      Else
      CreateCodeFromHexAssembly = True
      End If
    End If
  End Function

Private Function CreateCodeFromHexAssembly_UnHexByte(ByVal xByte1 As Byte) As Byte
  Select Case CLng(xByte1)
    Case &H30& To &H39&
      CreateCodeFromHexAssembly_UnHexByte = xByte1 - &H30&
    Case &H41& To &H46&
      CreateCodeFromHexAssembly_UnHexByte = xByte1 - &H37&
    Case &H61 To &H66&
      CreateCodeFromHexAssembly_UnHexByte = xByte1 - &H57&
    Case Else
#If InIDE Then
#End If
      Stop
    End Select
  End Function
