ProfessorProton

Proton's Resource Manager Class - Native Resources

Nov 20th, 2014
274
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
VB.NET 3.49 KB | None | 0 0
  1. Imports System.Runtime.InteropServices
  2.  
  3. ''' <summary>
  4. ''' Custom class used to read and write to resources using the UpdateResource API.
  5. ''' </summary>
  6. Public Class ResourceManager
  7.  
  8. #Region " Write "
  9.  
  10.     Private Shared Function GetPointer(ByVal Data As Object) As IntPtr
  11.         Dim H As GCHandle = GCHandle.Alloc(Data, GCHandleType.Pinned)
  12.         Dim Pointer As IntPtr
  13.         Try
  14.             Pointer = H.AddrOfPinnedObject()
  15.         Finally
  16.             H.Free()
  17.         End Try
  18.         Return Pointer
  19.  
  20.     End Function
  21.  
  22.  
  23.     <DllImport("kernel32.dll")> _
  24.     Private Shared Function UpdateResource(hUpdate As IntPtr, lpType As String, lpName As String, wLanguage As UShort, lpData As IntPtr, cbData As UInteger) As Boolean
  25.     End Function
  26.  
  27.     <DllImport("kernel32.dll")> _
  28.     Private Shared Function BeginUpdateResource(pFileName As String, <MarshalAs(UnmanagedType.Bool)> bDeleteExistingResource As Boolean) As IntPtr
  29.     End Function
  30.  
  31.     <DllImport("kernel32.dll")> _
  32.     Private Shared Function EndUpdateResource(hUpdate As IntPtr, fDiscard As Boolean) As Boolean
  33.     End Function
  34.  
  35.     ''' <summary>
  36.     ''' 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.
  37.     ''' </summary>
  38.     Public Shared Function WriteResource(Filename As String, ResourceName As String, Data As Byte()) As Boolean
  39.         Try
  40.             Dim Handle As IntPtr = BeginUpdateResource(Filename, False)
  41.             Dim DataPointer As IntPtr = GetPointer(Data)
  42.             Dim Res As Boolean = UpdateResource(Handle, ResourceName, "0", 0, DataPointer, Convert.ToUInt32(Data.Length))
  43.             EndUpdateResource(Handle, False)
  44.             Return True
  45.         Catch ex As Exception
  46.             Return False
  47.         End Try
  48.  
  49.     End Function
  50.  
  51. #End Region
  52.  
  53.  
  54. #Region " Read "
  55.  
  56.  
  57.     <DllImport("kernel32.dll")> _
  58.     Private Shared Function FindResource(hModule As IntPtr, lpName As String, lpType As String) As IntPtr
  59.     End Function
  60.  
  61.     <DllImport("kernel32.dll")> _
  62.     Private Shared Function SizeofResource(hModule As IntPtr, hResInfo As IntPtr) As Integer
  63.     End Function
  64.  
  65.     <DllImport("kernel32.dll")> _
  66.     Private Shared Function LoadResource(hModule As IntPtr, hResInfo As IntPtr) As IntPtr
  67.     End Function
  68.  
  69.     <DllImport("kernel32.dll")> _
  70.     Private Shared Function LoadLibrary(lpFileName As String) As IntPtr
  71.     End Function
  72.  
  73.     <DllImport("kernel32.dll")> _
  74.     Private Shared Function GetModuleHandle(lpModuleName As String) As IntPtr
  75.     End Function
  76.  
  77.     ''' <summary>
  78.     ''' Reads the resource using the specified resourcename from the specified filename. Returns the resource data if the resource was successfully read.
  79.     ''' </summary>
  80.     Public Shared Function ReadResource(FileName As String, ResourceName As String) As Byte()
  81.         Try
  82.             Dim hFile As IntPtr
  83.             If IO.File.Exists(FileName) Then
  84.                 hFile = LoadLibrary(FileName)
  85.             Else
  86.                 hFile = GetModuleHandle(Nothing)
  87.             End If
  88.             Dim Loc As IntPtr = FindResource(hFile, "0", ResourceName)
  89.             Dim X As IntPtr = LoadResource(hFile, Loc)
  90.             Dim Size = SizeofResource(hFile, Loc)
  91.             Dim bPtr As Byte() = New Byte(Size - 1) {}
  92.             Marshal.Copy(X, bPtr, 0, CInt(Size))
  93.             Return bPtr
  94.         Catch : Return Nothing
  95.         End Try
  96.     End Function
  97. #End Region
  98.  
  99. End Class
Advertisement
Add Comment
Please, Sign In to add comment