27.01.2025, 09:18 PM 
(This post was last modified: 27.01.2025, 09:57 PM by Pierre Bellisle.)
		
	
	
		Calling opcode string discussion was started at https://forum.powerbasic.com/forum/user-...discussion
It is an answer To Anne on how to get and embed a function in a string and call it via "call dword"
This one show particularly how to find the end of the function code by inserting data near the end.
	
	
	
	
	
It is an answer To Anne on how to get and embed a function in a string and call it via "call dword"
This one show particularly how to find the end of the function code by inserting data near the end.
Code:
'follow up of https://forum.powerbasic.com/forum/user-to-user-discussions/programming/838669-calling-opcode-string-discussion
#compile exe '#Win 10.04 (D:\Dev\Pow\Bas\Jose Roca\Forum\Jose\Windows API Headers\3.1.07\uz)#
#dim all
'#register none
'%Unicode = 1
#include "Win32Api.inc"
#RESOURCE MANIFEST, 1, "XPTheme.xml"
global hDlg as dword
$AppName  = "call dword"
%Static01 = 101
%Button01 = 201
declare function myfunction(byval var1 as long ,byval var2 as long) as long 'for call dword to use
'_____________________________________________________________________________
function HexView$(sString as string) as string 'HexString
 local  pByte   as byte pointer
 local  sBuffer as string
 local  sChar16 as string
 local  Looper  as long
 pByte = strptr(sString)
 do
   if (Looper and 15) = 00 then                 'Like MOD 16
     sBuffer = sBuffer & hex$(Looper, 4) & ": " 'Line number:
   elseif (Looper and 07) = 00 then             'Like MOD 8
     sBuffer = sBuffer & "- "                   'Middle dash
   end if
   if Looper < len(sString) then                'Add data
     sBuffer = sBuffer & hex$(@pByte[Looper], 2) & $spc
   else
     #if %def(%pb_win32) 'A to F have bigger width
     sBuffer = sBuffer & "     "                'Windows: No more data, fill with five spaces
     #else 'Use STDOUT in console
     sBuffer = sBuffer & "   "                  'Console: No more data, fill with three spaces
     #endif
   end if
   if (Looper and 15) = 15 then                 'End of 16 bytes line
     sChar16 = mid$(sString, Looper -14, 16) 'Next line replace non visible characters with dot
     replace any chr$(0,1,7,9,10,13,27 to 31,127,129,140,141,143,144,152,157) with "..................." in sChar16
     sBuffer = sBuffer & "; " & sChar16 & $crlf 'Add ascii string and CRLF
     if Looper >= len(sString) - 1 then exit do 'Job done
   end if
   incr Looper
 loop
 function = "Binary data lenght is" & str$(len(sString)) & " bytes." & $crlf & sBuffer
end function
'____________________________________________________________________________
function Add2Numbers(byval var1 as long ,byval var2 as long) as long
 function = var1 + var2
 'function = var2
 exit function
 !DB &h12, &h34, &h56, &h78, &h9A, &hBC, &hDE, &hF0 
end function
'_____________________________________________________________________________
callback function DlgProc
 local sAsm        as string
 local sTerminator as string
 local pcode       as dword
 local pString     as dword
 local RetVal      as long
 local byteVal     as byte
 select case cbmsg
   case %wm_command
     select case cbctl
       case %Button01
         if cbctlmsg = %bn_clicked or cbctlmsg = 1 then
           'call original function
           sTerminator = chr$(&h12, &h34, &h56, &h78, &h9A, &hBC, &hDE, &hF0)
           pcode       = codeptr(Add2Numbers)
           RetVal      = 0
           call dword pcode using myfunction(2, 2) to RetVal
           MessageBox(hDlg, "CALL DWORD pcode result =" & str$(RetVal), $AppName, 266240)
           '-----------------------------------------------------------------------------
           'call a copy of original function using myfunction()
           sAsm = ""
           do
             sAsm &= peek$(pcode, 1) 'build sAsm byte by byte to be sure to not access out of bound memory
             incr pcode
             if instr(sAsm, sTerminator) then
                sAsm &= peek$(pcode, 16) 'get the end of function
                exit do
             end if
           loop
           RetVal = 0
           pString = strptr(sAsm)
           call dword pString using myfunction(2, 3) to RetVal
           MessageBox(hDlg, "CALL DWORD pString result =" & str$(RetVal), $AppName, 266240)
           '-------------------------------------------------------------------------------------------------
           'call a copy of original function without using myfunction()
           pString = strptr(sAsm)
           ! push 4
           ! push 2
           call dword pString 'Or you may use !call pcode
           ! mov RetVal, eax
           MessageBox(hDlg, "CALL DWORD ASM result =" & str$(RetVal), $AppName, 266240)
           '-------------------------------------------------------------------------------------------------
           'show the copy and original function code side to side
           MessageBox(hDlg, "pcode:" & $crlf & HexView$(peek$(codeptr(Add2Numbers), len(sAsm))) & $crlf & $crlf & $crlf & _
                      "pString:" & $crlf & HexView$(peek$(codeptr(Add2Numbers), len(sAsm))), $AppName, 266240)
         end if
     end select
  end select
end function
'_____________________________________________________________________________
function pbmain()
 dialog font "Segoe UI", 9
 dialog new %hwnd_desktop, $AppName, , , 150, 50, _
 %ws_caption or %ws_minimizebox or %ws_maximizebox or %ws_sizebox or %ws_sysmenu, %ws_ex_left to hDlg
 control add label, hDlg, %Static01, "codeptr() and strptr() test", 5, 10, 140, 11, %ss_center
 control add button, hDlg, %Button01, "test codeptr() and strptr()", 15, 25, 120, 15
 dialog show modal hDlg call DlgProc
end function
'_____________________________________________________________________________
'
 

