Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Imports System.Runtime.InteropServices
- ''' <summary>
- ''' Custom class used to read and write to resources using the UpdateResource API.
- ''' </summary>
- Public Class ResourceManager
- #Region " Write "
- Private Shared Function GetPointer(ByVal Data As Object) As IntPtr
- Dim H As GCHandle = GCHandle.Alloc(Data, GCHandleType.Pinned)
- Dim Pointer As IntPtr
- Try
- Pointer = H.AddrOfPinnedObject()
- Finally
- H.Free()
- End Try
- Return Pointer
- End Function
- <DllImport("kernel32.dll")> _
- Private Shared Function UpdateResource(hUpdate As IntPtr, lpType As String, lpName As String, wLanguage As UShort, lpData As IntPtr, cbData As UInteger) As Boolean
- End Function
- <DllImport("kernel32.dll")> _
- Private Shared Function BeginUpdateResource(pFileName As String, <MarshalAs(UnmanagedType.Bool)> bDeleteExistingResource As Boolean) As IntPtr
- End Function
- <DllImport("kernel32.dll")> _
- Private Shared Function EndUpdateResource(hUpdate As IntPtr, fDiscard As Boolean) As Boolean
- End Function
- ''' <summary>
- ''' Writes resource using the specified resourcename to the specified filename, with the specified data as value. Returns true if it was successfully written. Returns false on error.
- ''' </summary>
- Public Shared Function WriteResource(Filename As String, ResourceName As String, Data As Byte()) As Boolean
- Try
- Dim Handle As IntPtr = BeginUpdateResource(Filename, False)
- Dim DataPointer As IntPtr = GetPointer(Data)
- Dim Res As Boolean = UpdateResource(Handle, ResourceName, "0", 0, DataPointer, Convert.ToUInt32(Data.Length))
- EndUpdateResource(Handle, False)
- Return True
- Catch ex As Exception
- Return False
- End Try
- End Function
- #End Region
- #Region " Read "
- <DllImport("kernel32.dll")> _
- Private Shared Function FindResource(hModule As IntPtr, lpName As String, lpType As String) As IntPtr
- End Function
- <DllImport("kernel32.dll")> _
- Private Shared Function SizeofResource(hModule As IntPtr, hResInfo As IntPtr) As Integer
- End Function
- <DllImport("kernel32.dll")> _
- Private Shared Function LoadResource(hModule As IntPtr, hResInfo As IntPtr) As IntPtr
- End Function
- <DllImport("kernel32.dll")> _
- Private Shared Function LoadLibrary(lpFileName As String) As IntPtr
- End Function
- <DllImport("kernel32.dll")> _
- Private Shared Function GetModuleHandle(lpModuleName As String) As IntPtr
- End Function
- ''' <summary>
- ''' Reads the resource using the specified resourcename from the specified filename. Returns the resource data if the resource was successfully read.
- ''' </summary>
- Public Shared Function ReadResource(FileName As String, ResourceName As String) As Byte()
- Try
- Dim hFile As IntPtr
- If IO.File.Exists(FileName) Then
- hFile = LoadLibrary(FileName)
- Else
- hFile = GetModuleHandle(Nothing)
- End If
- Dim Loc As IntPtr = FindResource(hFile, "0", ResourceName)
- Dim X As IntPtr = LoadResource(hFile, Loc)
- Dim Size = SizeofResource(hFile, Loc)
- Dim bPtr As Byte() = New Byte(Size - 1) {}
- Marshal.Copy(X, bPtr, 0, CInt(Size))
- Return bPtr
- Catch : Return Nothing
- End Try
- End Function
- #End Region
- End Class
Advertisement
Add Comment
Please, Sign In to add comment