ibennz

Untitled

Aug 4th, 2013
229
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
VB.NET 1.74 KB | None | 0 0
  1. Imports System.Runtime.InteropServices
  2.  
  3. Public Class WriteNatives
  4.  
  5.  
  6.     <DllImport("kernel32")> _
  7.     Public Shared Function BeginUpdateResource( _
  8.             ByVal fileName As String, _
  9.             <MarshalAs(UnmanagedType.Bool)> ByVal deleteExistingResources As Boolean) As IntPtr
  10.     End Function
  11.  
  12.     <DllImport("kernel32")> _
  13.     Public Shared Function UpdateResource( _
  14.             ByVal hUpdate As IntPtr, _
  15.             ByVal type As IntPtr, _
  16.             ByVal name As String, _
  17.             ByVal language As Short, _
  18.             <MarshalAs(UnmanagedType.LPArray, SizeParamIndex:=5)> _
  19.             ByVal data() As Byte, _
  20.             ByVal dataSize As Integer) As <MarshalAs(UnmanagedType.Bool)> Boolean
  21.     End Function
  22.  
  23.     <DllImport("kernel32")> _
  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.     Public Shared Sub WriteRez(ByVal filename$, ByVal bytes As Byte(), ByVal restype As String, ByVal rezname$)
  30.  
  31.         Try
  32.  
  33.             Dim handle As IntPtr = BeginUpdateResource(filename, False)
  34.             Dim file1 As Byte() = bytes
  35.             Dim res As Boolean = UpdateResource(handle, restype, rezname, 0, file1, Convert.ToUInt32(file1.Length))
  36.             EndUpdateResource(handle, False)
  37.  
  38.         Catch ex As Exception
  39.             MsgBox(ex.ToString, MsgBoxStyle.Critical)
  40.         End Try
  41.  
  42.     End Sub
  43.  
  44.  
  45. End Class
  46.  
  47.  
  48. ' Usage
  49.  
  50.  Dim b As Byte() = IO.File.ReadAllBytes(My.Computer.FileSystem.SpecialDirectories.Desktop & "\a.bmp")
  51.  
  52.         WriteNatives.WriteRez(My.Computer.FileSystem.SpecialDirectories.Desktop & "\test.exe", b, "2", "hi")
Advertisement
Add Comment
Please, Sign In to add comment