I hope someone can help me out I am getting desperate.

The line
VB Code:
  1. result = Win32.CreatePalette(memoryBlock)
in the module is returning an error 203.

Please help me to fix it.

I have a module (written by someone on another forum)
VB Code:
  1. Imports System.Runtime.InteropServices
  2. Imports System.Drawing
  3.  
  4. Module Module1
  5.  
  6.     Structure PALETTEENTRY
  7.         Public peRed As Byte
  8.         Public peGreen As Byte
  9.         Public peBlue As Byte
  10.         Public peFlags As Byte
  11.     End Structure
  12.  
  13.     Structure LOGPALETTE
  14.         Public palVersion As Short
  15.         Public palNumEntries As Integer
  16.     End Structure
  17.  
  18.     Public Class Win32
  19.  
  20.         <DllImport("Gdi32.dll", SetLastError:=True)> Shared Function _
  21.             CreatePalette(ByVal ptr As IntPtr) As IntPtr
  22.  
  23.         End Function
  24.  
  25.         <DllImport("Gdi32.dll", SetLastError:=True)> Shared Function _
  26.             GetNearestPaletteIndex(ByVal hpal As IntPtr, ByVal crColor As Int32) As Integer
  27.  
  28.         End Function
  29.  
  30.         <DllImport("Gdi32.dll")> Shared Function _
  31.             DeleteObject(ByVal hObject As IntPtr) As Integer
  32.  
  33.         End Function
  34.  
  35.     End Class
  36.  
  37.     Sub Main()
  38.  
  39.         Dim hPalette As IntPtr
  40.         Dim colors(256) As Color
  41.         Dim i As Integer
  42.  
  43.         ' create grayscale palette as example
  44.         For i = 0 To 255
  45.             colors(i) = Color.FromArgb(0, i, i, i)
  46.         Next
  47.  
  48.         hPalette = CreateWin32Palette(colors, 256)
  49.  
  50.         If Not hPalette.Equals(IntPtr.Zero) Then
  51.             Console.WriteLine(GetPaletteIndex(hPalette, Color.FromArgb(0, 127, 127, 127)))
  52.             Console.WriteLine(GetPaletteIndex(hPalette, Color.FromArgb(0, 200, 200, 200)))
  53.             Console.WriteLine(GetPaletteIndex(hPalette, Color.FromArgb(0, 15, 100, 200)))
  54.  
  55.             ReleaseWin32Palette(hPalette)
  56.         End If
  57.  
  58.     End Sub
  59.  
  60.     Public Function CreateWin32Palette(ByVal colors() As Color, ByVal nColors As Integer) As IntPtr
  61.         Dim memoryBlock As IntPtr
  62.         Dim ptr As IntPtr
  63.         Dim address As Int32
  64.         Dim nEntrySize As Integer
  65.         Dim logPalette As LOGPALETTE
  66.         Dim i As Integer
  67.         Dim entries(256) As PALETTEENTRY
  68.  
  69.         ''''
  70.         Dim result As IntPtr
  71.         Dim nError As Integer
  72.  
  73.         If nColors < 1 Or nColors > 256 Then
  74.             Return IntPtr.Zero
  75.         End If
  76.  
  77.         nEntrySize = Marshal.SizeOf(entries(0))
  78.  
  79.         logPalette = New LOGPALETTE
  80.         logPalette.palVersion = 768
  81.         logPalette.palNumEntries = nColors
  82.  
  83.         memoryBlock = Marshal.AllocHGlobal( _
  84.             nColors * nEntrySize + Marshal.SizeOf(logPalette))
  85.  
  86.         For i = 0 To nColors - 1
  87.             entries(i).peRed = colors(i).R
  88.             entries(i).peGreen = colors(i).G
  89.             entries(i).peBlue = colors(i).B
  90.             entries(i).peFlags = 0
  91.         Next
  92.  
  93.         Marshal.StructureToPtr(logPalette, memoryBlock, False)
  94.         address = memoryBlock.ToInt32() + Marshal.SizeOf(logPalette)
  95.  
  96.         For i = 0 To nColors - 1
  97.             ptr = New IntPtr(address + i * nEntrySize)
  98.             Marshal.StructureToPtr(entries(i), ptr, False)
  99.         Next
  100.  
  101.         'Return Win32.CreatePalette(memoryBlock)
  102.         result = Win32.CreatePalette(memoryBlock)
  103.         nError = Marshal.GetLastWin32Error()
  104.  
  105.         Return result
  106.  
  107.     End Function
  108.  
  109.     Sub ReleaseWin32Palette(ByVal hPalette As IntPtr)
  110.         Win32.DeleteObject(hPalette)
  111.     End Sub
  112.  
  113.     Function GetPaletteIndex(ByVal hPalette As IntPtr, ByVal color As Color) As Integer
  114.         Return Win32.GetNearestPaletteIndex(hPalette, color.ToArgb())
  115.     End Function
  116.  
  117. End Module

This is my calling code
VB Code:
  1. Dim intCount As Integer = 0
  2.         Dim ct As Control
  3.         For Each ct In gpbColours.Controls
  4.             If TypeOf ct Is Label Then
  5.                 If ct.Name.Substring(0, ct.Name.IndexOf("_")).ToLower = "lblcolour" And ct.Text = "X" Then
  6.                     intCount += 1
  7.                 End If
  8.             End If
  9.         Next
  10.  
  11.         Dim intElement As Integer = -1
  12.         Dim colours() As Color
  13.         ReDim colours(intCount - 1)
  14.         Dim c As Control
  15.         For Each c In gpbColours.Controls
  16.             If TypeOf c Is Label Then
  17.                 If c.Name.Substring(0, c.Name.IndexOf("_")).ToLower = "lblcolour" And c.Text = "X" Then
  18.                     intElement += 1
  19.                     colours(intElement) = c.BackColor
  20.                 End If
  21.             End If
  22.         Next
  23.  
  24.         'Dim i As Integer
  25.         'For i = 0 To intCount - 1
  26.         '    Debug.WriteLine(colours(i).R.ToString + ", " + colours(i).G.ToString + ", " + colours(i).B.ToString)
  27.         'Next
  28.  
  29.         Dim hPalette As IntPtr
  30.  
  31.         hPalette = CreateWin32Palette(colours, intCount)
  32.  
  33.         If Not hPalette.Equals(IntPtr.Zero) Then
  34.  
  35.             Console.WriteLine(GetPaletteIndex(hPalette, Color.FromArgb(0, 127, 127, 127)))
  36.             Console.WriteLine(GetPaletteIndex(hPalette, Color.FromArgb(0, 200, 200, 200)))
  37.             Console.WriteLine(GetPaletteIndex(hPalette, Color.FromArgb(0, 15, 100, 200)))
  38.  
  39.             ReleaseWin32Palette(hPalette)
  40.         End If