Remove Ads

Share on Facebook Share on Twitter

Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[VB.NET]Read/Write Resource(Bypass Avira)
#1
Write resources to .exe
Create a new module and paste this code.
[code]Imports System.Runtime.InteropServices
Module ResourceWriter
Private Function ToPtr(ByVal data As Object) As IntPtr
Dim h As GCHandle = GCHandle.Alloc(data, GCHandleType.Pinned)
Dim ptr As IntPtr
Try
ptr = h.AddrOfPinnedObject()
Finally
h.Free()
End Try
Return ptr

End Function

<DllImport("kernel32.dll", SetLastError:=True)> _
Private Function UpdateResource(ByVal hUpdate As IntPtr, ByVal lpType As String, ByVal lpName As String, ByVal wLanguage As UShort, ByVal lpData As IntPtr, ByVal cbData As UInteger) As Boolean
End Function
<DllImport("kernel32.dll", SetLastError:=True)> _
Private Function BeginUpdateResource(ByVal pFileName As String, <MarshalAs(UnmanagedType.Bool)> ByVal bDeleteExistingResources As Boolean) As IntPtr
End Function
<DllImport("kernel32.dll", SetLastError:=True)> _
Private Function EndUpdateResource(ByVal hUpdate As IntPtr, ByVal fDiscard As Boolean) As Boolean
End Function

Public Function WriteResource(ByVal filename As String, ByVal bytes As Byte()) As Boolean

Try
Dim handle As IntPtr = BeginUpdateResource(filename, False)
Dim file1 As Byte() = bytes
Dim fileptr As IntPtr = ToPtr(file1)
Dim res As Boolean = UpdateResource(handle, "RT_RCDATA", "0", 0, fileptr, Convert.ToUInt32(file1.Length))
EndUpdateResource(handle, False)
Catch ex As Exception
Return False
End Try
Return True

End Function
End Module[/code]

When building file use WriteResource("Whatever you want") to write the string/byte/etc.


Read resource from .exe

Create a new module and paste this code
[code]
Imports System.Runtime.InteropServices
Module ResourceReader

<DllImport("kernel32.dll", SetLastError:=True)> _
Private Function FindResource(ByVal hModule As IntPtr, ByVal lpName As String, ByVal lpType As String) As IntPtr
End Function

Private Declare Function GetModuleHandle Lib "kernel32" Alias "GetModuleHandleA" (ByVal moduleName As String) As IntPtr
Private Declare Function SizeofResource Lib "kernel32" (ByVal hModule As IntPtr, ByVal hResInfo As IntPtr) As Integer
Private Declare Function LoadResource Lib "kernel32" (ByVal hModule As IntPtr, ByVal hResInfo As IntPtr) As IntPtr

Public Function ReadResource(ByVal filename As String) As Byte()
Dim hModule As IntPtr = GetModuleHandle(filename)
Dim loc As IntPtr = FindResource(hModule, "0", "RT_RCDATA")
Dim x As IntPtr = LoadResource(hModule, loc)
Dim size = SizeofResource(hModule, loc)
Dim bPtr As Byte() = New Byte(size - 1) {}
Marshal.Copy(x, bPtr, 0, CInt(size))
Return bPtr
End Function
End Module[/code]

Use ReadResource("Name of the File(Location Name)") to get the string/bytes/etc..

Convert to string function:
System.Text.Encoding.Default.GetString("BYTES!")
Convert to bytes function:
System.Text.Encoding.Default.GetBytes("STRING!")3

Note: This writes bytes, not string. You need to convert string to bytes in order to run.
Reply
#2
Nice share, thank you. Time to give you your first reputation.
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)