Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Getting an IntPtr handle to an instance of the executable file that contains a resource.

Getting an IntPtr handle to an instance of the executable file that contains a resource.

Scheduled Pinned Locked Moved C#
graphicshardwaretutorialquestionlearning
16 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • U Uwe Keim

    i guess you are confusing managed and unmanaged resources. try using reflector to see whether it is a .net managed resource.

    -- • Zeta Producer Desktop CMS Intuitive, completely easy-to-use CMS for Windows. • Zeta Helpdesk Open Source ticket software for Windows and web. • Zeta Uploader Easily send large files by e-mail. Windows and web client. • Desargues Innovative, lean and neat calculator for Windows.

    M Offline
    M Offline
    mike montagne
    wrote on last edited by
    #7

    No, my resource is managed. I've added many bitmaps to my class library DLL by the visual process, Project.Add... Existing file... Determining whether it is a managed resource is not the problem. The DLL is loaded by a test application, using the classes as they would typically be deployed. The problem is for the class, when it is in the test application (exe), to assign the bitmap resources to property, preferably with the Bitmap.FromResource( IntPtr hinstance, String bitmapName ) method.

    U 1 Reply Last reply
    0
    • M mike montagne

      In other words, you can't do this, and there doesn't seem to be a way to cast or otherwise derive the IntPtr value so that you could do the equivalent of this:

      Glyph1.GMap = Bitmap.FromResource( ( IntPtr )Assembly.GetAssembly( t ), s );
      

      So the question remains, How do you get an IntPtr to an executable containing a resource?

      L Offline
      L Offline
      led mike
      wrote on last edited by
      #8

      I don't know what to say at this point. As far as I am aware I gave you a solution that works, in my experience, and you have not tried it.

      led mike

      M 2 Replies Last reply
      0
      • L led mike

        I don't know what to say at this point. As far as I am aware I gave you a solution that works, in my experience, and you have not tried it.

        led mike

        M Offline
        M Offline
        mike montagne
        wrote on last edited by
        #9

        No, I tried it:

        Glyph1.GMap = Bitmap.FromResource( ( IntPtr )Assembly.GetExecutingAssembly( ), s );
        

        FromResource requires an IntPtr. As I indicated in my question, I need an IntPtr to the executable containing the resource. According to documentation, what you need to do to get *that* assembly is:

                       Type t = this.GetType( );
                       Assembly a = Assembly.GetAssembly( t );
        

        This gets the assembly. The problem is, getting an IntPtr hinstance pointing to the assembly from the assembly. Are you saying that you can get an IntPtr from your calls, because I don't see that in your answer?

        L 1 Reply Last reply
        0
        • L led mike

          I don't know what to say at this point. As far as I am aware I gave you a solution that works, in my experience, and you have not tried it.

          led mike

          M Offline
          M Offline
          mike montagne
          wrote on last edited by
          #10

          In other words, we can get so far as acquiring a "ModuleHandle" from GetExecutingAssembly -- but I can do this with GetAssembly as well: Assembly.GetExecutingAssembly( ).GetModule( "APC" ).ModuleHandle; ModuleHandle however is a struct with no field or conversion method which converts to an IntPtr to the instance of the assembly. That's the missing piece: How to get an IntPtr from the assembly, or something else, which identifies the executable containing the resource. GetExecutingAssembly just looks like another course to the same dead end to me, unless we can get that IntPtr somehow. But thanks for the try! m

          1 Reply Last reply
          0
          • M mike montagne

            No, my resource is managed. I've added many bitmaps to my class library DLL by the visual process, Project.Add... Existing file... Determining whether it is a managed resource is not the problem. The DLL is loaded by a test application, using the classes as they would typically be deployed. The problem is for the class, when it is in the test application (exe), to assign the bitmap resources to property, preferably with the Bitmap.FromResource( IntPtr hinstance, String bitmapName ) method.

            U Offline
            U Offline
            Uwe Keim
            wrote on last edited by
            #11

            But Bitmap.FromResource is for unmanaged: "...Creates a Bitmap from the specified Windows resource..." (http://msdn2.microsoft.com/en-us/library/system.drawing.bitmap.fromresource.aspx[^])

            -- • Zeta Producer Desktop CMS Intuitive, completely easy-to-use CMS for Windows. • Zeta Helpdesk Open Source ticket software for Windows and web. • Zeta Uploader Easily send large files by e-mail. Windows and web client. • Desargues Innovative, lean and neat calculator for Windows.

            M 1 Reply Last reply
            0
            • M mike montagne

              No, I tried it:

              Glyph1.GMap = Bitmap.FromResource( ( IntPtr )Assembly.GetExecutingAssembly( ), s );
              

              FromResource requires an IntPtr. As I indicated in my question, I need an IntPtr to the executable containing the resource. According to documentation, what you need to do to get *that* assembly is:

                             Type t = this.GetType( );
                             Assembly a = Assembly.GetAssembly( t );
              

              This gets the assembly. The problem is, getting an IntPtr hinstance pointing to the assembly from the assembly. Are you saying that you can get an IntPtr from your calls, because I don't see that in your answer?

              L Offline
              L Offline
              led mike
              wrote on last edited by
              #12

              mike montagne wrote:

              Glyph1.GMap = Bitmap.FromResource( ( IntPtr )Assembly.GetExecutingAssembly( ), s );

              umm that's not what I told you to do. Perhaps you should go back and read my post.

              led mike

              M 1 Reply Last reply
              0
              • U Uwe Keim

                But Bitmap.FromResource is for unmanaged: "...Creates a Bitmap from the specified Windows resource..." (http://msdn2.microsoft.com/en-us/library/system.drawing.bitmap.fromresource.aspx[^])

                -- • Zeta Producer Desktop CMS Intuitive, completely easy-to-use CMS for Windows. • Zeta Helpdesk Open Source ticket software for Windows and web. • Zeta Uploader Easily send large files by e-mail. Windows and web client. • Desargues Innovative, lean and neat calculator for Windows.

                M Offline
                M Offline
                mike montagne
                wrote on last edited by
                #13

                Whoa. Jumps out and grabs you when it's in Bold! :-) I guess I need to learn to *read*! I suppose I was so intent on loading and embedded resource I just assumed this was the method (what other method is there?). So this *does* require calling into LoadLibrary to get the hinstance. I'm going to have to look around to see what method to use to retrieve my managed resources, because that's the way I want to do it. I hope that isn't some odd implementation of "FromFile", even as the resources are managed -- maybe so?

                U 1 Reply Last reply
                0
                • L led mike

                  mike montagne wrote:

                  Glyph1.GMap = Bitmap.FromResource( ( IntPtr )Assembly.GetExecutingAssembly( ), s );

                  umm that's not what I told you to do. Perhaps you should go back and read my post.

                  led mike

                  M Offline
                  M Offline
                  mike montagne
                  wrote on last edited by
                  #14

                  This is just one permutation of what I tried with GetExecutingAssembly( ). If what you're intimating is that I need to try Assembly.GetManifestResourceStream() to retrieve the bitmaps so they can be assigned to my properties, I'll have to explore that later (?). I'm late to hit the road, but thanks so much for trying to set me straight on this. I'll get around to it as soon as I can.

                  L 1 Reply Last reply
                  0
                  • M mike montagne

                    This is just one permutation of what I tried with GetExecutingAssembly( ). If what you're intimating is that I need to try Assembly.GetManifestResourceStream() to retrieve the bitmaps so they can be assigned to my properties, I'll have to explore that later (?). I'm late to hit the road, but thanks so much for trying to set me straight on this. I'll get around to it as soon as I can.

                    L Offline
                    L Offline
                    led mike
                    wrote on last edited by
                    #15

                    mike montagne wrote:

                    If what you're intimating is that I need to try Assembly.GetManifestResourceStream()

                    intimating? Since this is a forum based conversation I don't know how more clearly to state it than putting it directly in the post which I did. :rolleyes:

                    led mike

                    1 Reply Last reply
                    0
                    • M mike montagne

                      Whoa. Jumps out and grabs you when it's in Bold! :-) I guess I need to learn to *read*! I suppose I was so intent on loading and embedded resource I just assumed this was the method (what other method is there?). So this *does* require calling into LoadLibrary to get the hinstance. I'm going to have to look around to see what method to use to retrieve my managed resources, because that's the way I want to do it. I hope that isn't some odd implementation of "FromFile", even as the resources are managed -- maybe so?

                      U Offline
                      U Offline
                      Uwe Keim
                      wrote on last edited by
                      #16

                      mike montagne wrote:

                      I'm going to have to look around to see what method to use to retrieve my managed resources

                      In fact it's rather easy. Just use the Windows Forms designer and assign an image (e.g. to a PictureBox). Then take a look at the MyForm.Designer.cs file and see the designer-generated code that loads from the resource. Adapt it and do it in a similar way.

                      -- • Zeta Producer Desktop CMS Intuitive, completely easy-to-use CMS for Windows. • Zeta Helpdesk Open Source ticket software for Windows and web. • Zeta Uploader Easily send large files by e-mail. Windows and web client. • Desargues Innovative, lean and neat calculator for Windows.

                      1 Reply Last reply
                      0
                      Reply
                      • Reply as topic
                      Log in to reply
                      • Oldest to Newest
                      • Newest to Oldest
                      • Most Votes


                      • Login

                      • Don't have an account? Register

                      • Login or register to search.
                      • First post
                        Last post
                      0
                      • Categories
                      • Recent
                      • Tags
                      • Popular
                      • World
                      • Users
                      • Groups