Post Reply 

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)
Author Message
C3do77k Offline
New Member

Trade Count: (0)
Posts: 1
Joined: Jan 2011
Reputation: 1
Post: #1
[VB.NET]Read/Write Resource(Bypass Avira)
Write resources to .exe
Create a new module and paste this 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
            ptr = h.AddrOfPinnedObject()
        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

            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

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
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

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

Convert to string function:
Convert to bytes function:

Note: This writes bytes, not string. You need to convert string to bytes in order to run.
(This post was last modified: 01-24-2011 03:09 PM by C3do77k.)
01-24-2011 03:07 PM
Find all posts by this user Quote this message in a reply
Ironside Offline
Site Owner

Trade Count: (0)
Posts: 170
Joined: Nov 2010
Reputation: 7
Post: #2
RE: [VB.NET]Read/Write Resource(Bypass Avira)
Nice share, thank you. Time to give you your first reputation.
01-24-2011 03:54 PM
Find all posts by this user Quote this message in a reply
Post Reply 

Forum Jump:

User(s) browsing this thread: