• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 2074
  • Last Modified:

Loading an image from a library...error 127 (The specified procedure could not be found.)

Aright, I'm trying to load a bitmap from a file (C:\WINDOWS\Resources\Themes\Luna.theme). Unfortunately, I'm receiving an error of 127...any ideas?

Oh yeah, and once I have a handle to the bitmap, can I use Bitmap.fromresource (something like that) to grab it?

Many thanks,

using System;
using System.Runtime.InteropServices;
using System.Drawing;
namespace _4D_Environment
      /// <summary>
      /// Summary description for BitmapsGrab.
      /// </summary>
      public class Resource
            public Resource(string Filename)
                  System.Diagnostics.Debug.WriteLine("Loading Resource...");
                  hModule = Kernel32.LoadLibraryEx(Filename, 0, 0); //Kernel32.LOAD_LIBRARY_AS_DATAFILE);
                  if(hModule == IntPtr.Zero)
                        System.Diagnostics.Debug.WriteLine("Error loading library");

                  System.IntPtr StartButton = Kernel32.LoadImage(hModule, "BLUE_STARTBUTTON_BMP", Kernel32.IMAGE_BITMAP, 0, 0, Kernel32.LR_DEFAULTCOLOR);

                  //Int32 n = Kernel32.EnumResourceTypes(hModule, new Kernel32.EnumResTypeProc(EnumResTypeProc), 0);

                  int nError = Marshal.GetLastWin32Error();

                  hModule = IntPtr.Zero;
                  StartButton = IntPtr.Zero;

            public Bitmap newbit;
            private IntPtr hModule;

            public static Int32 EnumResTypeProc(IntPtr hModule,
                  IntPtr lpszType, long lParam)
                  Kernel32.EnumResourceNames(hModule, lpszType, new Kernel32.EnumResNameProc(EnumResNameProc), lParam);
                  return 1;

            public static Int32 EnumResNameProc(IntPtr hModule, IntPtr lpszType, [MarshalAs(UnmanagedType.LPStr)] string lpszName, long lParam)
                  System.Diagnostics.Debug.WriteLine("Name: " + lpszName);
                  return 1;

      public class Kernel32

            public const long DONT_RESOLVE_DLL_REFERENCES     = 0x00000001;
            public const long LOAD_LIBRARY_AS_DATAFILE          = 0x00000002;
            public const long LOAD_WITH_ALTERED_SEARCH_PATH     = 0x00000008;
            public const long LOAD_IGNORE_CODE_AUTHZ_LEVEL     = 0x00000010;

            //Enum types
            public const long RT_CURSOR  = 1;          
            public const long RT_BITMAP = 2;              
            public const long RT_ICON = 3;              
            public const long RT_MENU = 4;
            public const long RT_DIALOG = 5;
            public const long RT_STRING = 6;
            public const long RT_FONTDIR = 7;
            public const long RT_FONT = 8;
            public const long RT_ACCELERATOR = 9;
            public const long RT_RCDATA = 10;
            public const long RT_MESSAGETABLE = 11;
            public const long RT_VERSION = 16;
            public const long RT_DLGINCLUDE = 17;
            public const long RT_PLUGPLAY = 19;
            public const long RT_VXD = 20;
            public const long RT_ANICURSOR = 21;
            public const long RT_ANIICON = 22;
            public const long RT_HTML = 23;
            public const long RT_MANIFEST = 24;
            public const uint IMAGE_BITMAP = 0;
            public const uint IMAGE_ICON = 1;
            public const uint IMAGE_CURSOR = 2;
            public const uint LR_DEFAULTCOLOR     =0x0000;
            public const uint LR_MONOCHROME       =0x0001;
            public const uint LR_COLOR            =0x0002;
            public const uint LR_COPYRETURNORG    =0x0004;
            public const uint LR_COPYDELETEORG    =0x0008;
            public const uint LR_LOADFROMFILE     =0x0010;
            public const uint LR_LOADTRANSPARENT  =0x0020;
            public const uint LR_DEFAULTSIZE      =0x0040;
            public const uint LR_VGACOLOR         =0x0080;
            public const uint LR_LOADMAP3DCOLORS  =0x1000;
            public const uint LR_CREATEDIBSECTION =0x2000;
            public const uint LR_COPYFROMRESOURCE =0x4000;
            public const uint LR_SHARED           =0x8000;

            public const long LANG_NEUTRAL = 0x00;

            [DllImport("kernel32.dll", SetLastError=true)]
            public static extern IntPtr LoadLibraryEx(string lpfFileName,
                  long hFile, long dwFlags);

            [DllImport("kernel32.dll", SetLastError=true)]
            public static extern Int32 EnumResourceTypes(IntPtr hModule, EnumResTypeProc callback, long lParam);

            public delegate Int32 EnumResTypeProc(IntPtr hModule, IntPtr lpszType, long lParam);
            [DllImport("kernel32.dll", SetLastError=true)]
            public static extern bool FreeLibrary(IntPtr hModule);

            [DllImport("kernel32.dll", SetLastError=true)]
            public static extern Int32 EnumResourceNames(IntPtr hModule, IntPtr lpszType, EnumResNameProc callback, long lParam);
            public delegate Int32 EnumResNameProc(IntPtr hModule, IntPtr lpszType, [MarshalAs(UnmanagedType.LPStr)] string lpszName, long lParam);
            [DllImport("user32.dll", SetLastError=true)]
            public static extern IntPtr LoadImage(IntPtr hinst, [MarshalAs(UnmanagedType.LPStr)] string lpszName, uint uType, int cxDesired, int cyDesired, uint fuLoad);
            [DllImport("kernel32.dll", SetLastError=true)]
            public static extern IntPtr FindResourceEx(IntPtr hModule, [MarshalAs(UnmanagedType.LPStr)] string lpType, [MarshalAs(UnmanagedType.LPStr)] string lpName, long wLanguage);
            [DllImport("kernel32.dll", SetLastError=true)]
            public static extern void LockResource(IntPtr hResData);



  • 3
1 Solution
Last error 127 is "The specified procedure could not be found". Error Lookup program which comes with Visual Studio translates error code to error message.

When you use API which has string parameters (like LoadImage), always read comments in the end of API description.

Unicode: Implemented as Unicode and ANSI versions on Windows NT/2000.

This means, LoadImage API doesnt' exist in the library. Instead of this, there are two API: LoadImageA (which works with ansi strings) and LoadImageW (which works with UNICODE strings). It's always better to work with UNICODE version:

[DllImport("user32.dll", SetLastError=true)]
public static extern IntPtr LoadImageW(IntPtr hinst, [MarshalAs(UnmanagedType.LPWStr)] string lpszName, uint uType, int cxDesired, int cyDesired, uint fuLoad);
rossryanAuthor Commented:
And this would explain those alias remarks...

Now I get an double extra happy error: 1814 The specified resource name cannot be found in the image file. ERROR_RESOURCE_NAME_NOT_FOUND.

Resource Luna = new Resource("C:\\WINDOWS\\Resources\\Themes\\Luna\\luna.msstyles");

Using enumresnames, I can see BLUE_STARTBUTTON_BMP...any thoughts on this>?
I need to check this.
There is VB6 program which does exactly what you need:

Try to read it, it contains all required code.
Hi rossryan,
This sample is really cool and contains all you need. It has also a lot of unnecessary code, but you can ignore it. The best thing you can do now is to learn this sample using VB6. After this you can try to translate it to C# and ask questions about specific problems you have.

Notice that download page for this sample contains also Required References, which should be downloaded and installed as well - at least, ISHF_Ex.tlb which is required to compile this sample. If you have some specific problems building this sample, ask in VB area.
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now