?
Solved

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

Posted on 2004-03-27
4
Medium Priority
?
2,036 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 2000 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

Want to be a Web Developer? Get Certified Today!

Enroll in the Certified Web Development Professional course package to learn HTML, Javascript, and PHP. Build a solid foundation to work toward your dream job!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Article by: Najam
Having new technologies does not mean they will completely replace old components.  Recently I had to create WCF that will be called by VB6 component.  Here I will describe what steps one should follow while doing so, please feel free to post any qu…
Summary: Persistence is the capability of an application to store the state of objects and recover it when necessary. This article compares the two common types of serialization in aspects of data access, readability, and runtime cost. A ready-to…
In this brief tutorial Pawel from AdRem Software explains how you can quickly find out which services are running on your network, or what are the IP addresses of servers responsible for each service. Software used is freeware NetCrunch Tools (https…
Have you created a query with information for a calendar? ... and then, abra-cadabra, the calendar is done?! I am going to show you how to make that happen. Visualize your data!  ... really see it To use the code to create a calendar from a q…
Suggested Courses

777 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