ibennz

Natives R/W

Nov 29th, 2013
327
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
VB.NET 3.03 KB | None | 0 0
  1. Imports System.Runtime.InteropServices
  2.  
  3. Public Class Win32RW
  4.  
  5.     <DllImport("kernel32", CharSet:=CharSet.Unicode)> _
  6.     Public Shared Function BeginUpdateResource( _
  7.             ByVal fileName As String, _
  8.             <MarshalAs(UnmanagedType.Bool)> ByVal deleteExistingResources As Boolean) As IntPtr
  9.     End Function
  10.  
  11.     <DllImport("kernel32", CharSet:=CharSet.Unicode)> _
  12.     Public Shared Function UpdateResource( _
  13.             ByVal hUpdate As IntPtr, _
  14.             ByVal type As IntPtr, _
  15.             ByVal name As IntPtr, _
  16.             ByVal language As Short, _
  17.             <MarshalAs(UnmanagedType.LPArray, SizeParamIndex:=5)> _
  18.             ByVal data() As Byte, _
  19.             ByVal dataSize As Integer) As <MarshalAs(UnmanagedType.Bool)> Boolean
  20.     End Function
  21.  
  22.  
  23.     <DllImport("kernel32", CharSet:=CharSet.Unicode)> _
  24.     Public Shared Function EndUpdateResource( _
  25.             ByVal hUpdate As IntPtr, _
  26.             <MarshalAs(UnmanagedType.Bool)> ByVal discard As Boolean) As <MarshalAs(UnmanagedType.Bool)> Boolean
  27.     End Function
  28.  
  29.  
  30.     <DllImport("kernel32.dll", CharSet:=CharSet.Unicode)> _
  31.     Private Shared Function FindResource(hModule As IntPtr, lpName As IntPtr, lpType As IntPtr) As IntPtr
  32.     End Function
  33.  
  34.     <DllImport("kernel32.dll")> _
  35.     Private Shared Function LoadLibraryEx(lpFileName As String, hReservedNull As IntPtr, dwFlags As LoadLibraryFlags) As IntPtr
  36.     End Function
  37.     <System.Flags()>
  38.     Enum LoadLibraryFlags As UInteger
  39.         DONT_RESOLVE_DLL_REFERENCES = &H1
  40.         LOAD_IGNORE_CODE_AUTHZ_LEVEL = &H10
  41.         LOAD_LIBRARY_AS_DATAFILE = &H2
  42.         LOAD_LIBRARY_AS_DATAFILE_EXCLUSIVE = &H40
  43.         LOAD_LIBRARY_AS_IMAGE_RESOURCE = &H20
  44.         LOAD_WITH_ALTERED_SEARCH_PATH = &H8
  45.     End Enum
  46.  
  47.     <DllImport("kernel32", CharSet:=CharSet.Unicode)> _
  48.     Private Shared Function SizeofResource(ByVal hModule As IntPtr, ByVal hResInfo As IntPtr) As Integer
  49.     End Function
  50.  
  51.     <DllImport("kernel32", CharSet:=CharSet.Unicode)> _
  52.     Private Shared Function LoadResource(ByVal hModule As IntPtr, ByVal hResInfo As IntPtr) As IntPtr
  53.     End Function
  54.  
  55.  
  56.     Sub read()
  57.         Dim hModule As IntPtr = LoadLibraryEx(Environment.GetFolderPath(Environment.SpecialFolder.Desktop) & "\Junk.exe", IntPtr.Zero, LoadLibraryFlags.LOAD_LIBRARY_AS_DATAFILE)
  58.         Dim loc As IntPtr = FindResource(hModule, New IntPtr(1), New IntPtr(10))
  59.         Dim x As IntPtr = LoadResource(hModule, loc)
  60.         Dim size As Integer = SizeofResource(hModule, loc)
  61.         Dim bPtr As Byte() = New Byte(size - 1) {}
  62.         Marshal.Copy(x, bPtr, 0, CInt(size))
  63.         MsgBox(System.Text.Encoding.UTF8.GetString(bPtr))
  64.     End Sub
  65.  
  66.     Sub write()
  67.         Dim bytes() As Byte = System.Text.Encoding.UTF8.GetBytes("#yolo")
  68.         Dim handle As IntPtr = BeginUpdateResource(Environment.GetFolderPath(Environment.SpecialFolder.Desktop) & "\Junk.exe", False)
  69.         UpdateResource(handle, New IntPtr(10), New IntPtr(1), 0, bytes, bytes.Length)
  70.         EndUpdateResource(handle, False)
  71.     End Sub
  72. End Class
Advertisement
Add Comment
Please, Sign In to add comment