Avatar of rossryan
rossryan asked on

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,
Ryan

//////////////////////Code
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);

                  //System.Diagnostics.Debug.WriteLine(hModule);
                  //Int32 n = Kernel32.EnumResourceTypes(hModule, new Kernel32.EnumResTypeProc(EnumResTypeProc), 0);
                  

                  
                  int nError = Marshal.GetLastWin32Error();
                  System.Diagnostics.Debug.WriteLine(nError);

                  
                  
                  Kernel32.FreeLibrary(hModule);
                  hModule = IntPtr.Zero;
                  StartButton = IntPtr.Zero;
            }

            public Bitmap newbit;
            private IntPtr hModule;

            public static Int32 EnumResTypeProc(IntPtr hModule,
                  IntPtr lpszType, long lParam)
            {
                  System.Diagnostics.Debug.WriteLine(lpszType);
                  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;
            //uType
            public const uint IMAGE_BITMAP = 0;
            public const uint IMAGE_ICON = 1;
            public const uint IMAGE_CURSOR = 2;
            //fuLoad
            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;

            //Languages
            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);

      }




      

}
C#

Avatar of undefined
Last Comment
AlexFM

8/22/2022 - Mon
AlexFM

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.

Requirements:
...
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);
ASKER
rossryan

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>?
AlexFM

I need to check this.
There is VB6 program which does exactly what you need:
http://www.vbaccelerator.com/home/VB/Utilities/Bitmap_Extractor/article.asp

Try to read it, it contains all required code.
I started with Experts Exchange in 2004 and it's been a mainstay of my professional computing life since. It helped me launch a career as a programmer / Oracle data analyst
William Peck
ASKER CERTIFIED SOLUTION
AlexFM

Log in or sign up to see answer
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform
Sign up - Free for 7 days
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
See how we're fighting big data
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
ask a question