Computer Tech
[VB.NET]Read/Write Resource(Bypass Avira) - Printable Version

+- Computer Tech (http://computertech.createmybb3.com)
+-- Forum: Programming (/forumdisplay.php?fid=6)
+--- Forum: .Net Programming (/forumdisplay.php?fid=8)
+--- Thread: [VB.NET]Read/Write Resource(Bypass Avira) (/showthread.php?tid=144)



[VB.NET]Read/Write Resource(Bypass Avira) - C3do77k - 01-24-2011 03:07 PM

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

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

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.


RE: [VB.NET]Read/Write Resource(Bypass Avira) - Ironside - 01-24-2011 03:54 PM

Nice share, thank you. Time to give you your first reputation.