Solved

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

Posted on 2004-03-27
4
2,009 Views
Last Modified: 2012-06-27
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);

      }




      

}
0
Comment
Question by:rossryan
  • 3
4 Comments
 
LVL 48

Expert Comment

by:AlexFM
ID: 10697606
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);
0
 

Author Comment

by:rossryan
ID: 10697775
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>?
0
 
LVL 48

Expert Comment

by:AlexFM
ID: 10697856
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.
0
 
LVL 48

Accepted Solution

by:
AlexFM earned 500 total points
ID: 10697895
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.
0

Featured Post

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

Introduction Hi all and welcome to my first article on Experts Exchange. A while ago, someone asked me if i could do some tutorials on object oriented programming. I decided to do them on C#. Now you may ask me, why's that? Well, one of the re…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.

758 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now