• 0

Is this the correct way? Playing with Integers and strings...


Question

I'm working on a small project of mine, and since I do not want to make huge mistakes and errors, trying to get things done the most efficent way possible, I have came across to a problem I think you guys might help me out with.
 

What am I trying to to? Well, I try to read resources from a DLL file.

The problem? There are Resource Types and Resource Names: Problem comes mostly with names. Some names are integers, some are strings.

 

I know I could write a two seperate functions, one that reads resource when its name is string, other when its integer. However, I try to come out with a more complex solution, sort of 2in1, one function would act accordingly, if its given type is integer or its string.

 

So what I have wrote so far:
 

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

        [DllImport("kernel32.dll")]
        static extern IntPtr FindResource(IntPtr hModule, int lpName, string lpType);

        [DllImport("kernel32.dll")]
        static extern IntPtr FindResource(IntPtr hModule, string lpName, string lpType);

        [DllImport("kernel32.dll", SetLastError = true)]
        static extern IntPtr LoadResource(IntPtr hModule, IntPtr hResInfo);

        [DllImport("kernel32.dll")]
        static extern IntPtr LockResource(IntPtr hResData);

        [DllImport("kernel32.dll", SetLastError = true)]
        static extern uint SizeofResource(IntPtr hModule, IntPtr hResInfo);


        static byte[] GetResource(string FileName, ResName ResourceName, string ResourceType)
        {
            var hModule = LoadLibraryEx(FileName, IntPtr.Zero, 0x00000002);
            IntPtr hResInfo;

            if (ResourceName.Str == string.Empty) hResInfo = FindResource(hModule, ResourceName.Int, ResourceType);
            else hResInfo = FindResource(hModule, ResourceName.Str, ResourceType);

            var hResSize = SizeofResource(hModule, hResInfo);
            var hResData = LoadResource(hModule, hResInfo);
            var hResSource = LockResource(hResData);

            var Resource = new byte[hResSize];
            Marshal.Copy(hResSource, Resource, 0, (int)hResSize);

            return Resource;
        }

        public struct ResName
        {
            public int Int;
            public string Str;

            public ResName(int Integer, string String)
            {
                this.Int = Integer;
                this.Str = String;
            }
        }

What I would do is create a resname object before calling the function, then setting the parameters to it, then passing ResName to function, and if one of the parameters is not as it should, it acts accordingly.

 

Now the question is wheter this is the right way to go about this, or can anyone come out with a better solution?


 

3 answers to this question

Recommended Posts

  • 0

could do something likew this and get rid of the class.

        static byte[] GetResource(string FileName, object ResourceName, string ResourceType)
        {
            var hModule = LoadLibraryEx(FileName, IntPtr.Zero, 0x00000002);
            IntPtr hResInfo;

            if (ResourceName.GetType() is int)
                hResInfo = FindResource(hModule, (int)ResourceName, ResourceType);
            else if (ResourceName.GetType() is string)
                hResInfo = FindResource(hModule, (string)ResourceName, ResourceType);
            else
                throw new NotImplementedException("Unhandled resource name type.");

            var hResSize = SizeofResource(hModule, hResInfo);
            var hResData = LoadResource(hModule, hResInfo);
            var hResSource = LockResource(hResData);

            var Resource = new byte[hResSize];
            Marshal.Copy(hResSource, Resource, 0, (int)hResSize);

            return Resource;
        }
  • 0

Thanks a bunch, knew there would be a simpler way to go around this, "object" didnt really come to my mind :)

 

EDIT: Although the above code gives "Warning 1 The given expression is never of the provided ('int') type "

Just have to replace "if (ResourceName.GetType() is int)" with  "if (ResourceName.GetType() == typeof(int))

"
 

and code works perfectly

 

  • Like 1
This topic is now closed to further replies.
  • Posts

    • If nobody was using it. Then why specifically is it a shame? Windows needs be slimmer. Every feature like this is just more bloat
    • Invincible VS announced, a brutal 3v3 tag fighting game by former Killer Instinct developers by Pulasthi Ariyasinghe The Xbox Games Showcase showed off a brand-new fighting game incoming from the award-winning Invincible universe. Simply dubbed Invincible VS, the title will feature three versus three tag fighting with all sorts of heroes and villains from the franchise. Check out the announcement trailer above, which shows off some gameplay near the tail end of it. Published by Skybound Games and developed by Quarter Up, Invincible VS has a big focus on the brutality, much like its source material. There will be iconic arenas straight from the show, as well as a battle damage system to show off the carnage on the fighters themselves. "In Invincible VS, every move, every line of dialogue, every broken limb and bloodied mouth carries real cost and consequence—all building toward the bone-deep satisfaction of victory," said Invincible VS executive producer Mike Willette. "No tag fighting game has ever delivered this level of intensity and violence. This game is a love letter to the fighting game community, created by a team of passionate, experienced fans of the genre." While this is a brand-new studio that's taking on the project, it has been revealed that former members from the 2013 Killer Instinct are involved in the development. "I have said this before, and I’ll say it again: this is the most excited I’ve been about anything involving Invincible," said Invincible co-creator Robert Kirkman. "Invincible VS is crazy fun, fast, ferocious and dripping with the same over-the-top brutality that you’ll see in the comic book pages and on the show. It is a natural evolution of the franchise and it is everything that fans have been demanding for over a decade! This is without a doubt, probably the best superhero fighting game in the universe!" The title will have multiple game modes as well, including a campaign with an original story by the writer of the animated series, an arcade mode for skirmishes, and a training arena. As expected, competitive and casual online multiplayer will be available too. Invincible VS is coming out sometime in 2026 across PC, Xbox Series X|S, and PlayStation 5.
    • And how would a "real" handheld Xbox differed from that? Pray tell. Surely it too would've been a PC with a built in screen and controller, shoehorned into a handheld form factor, and running a flavour of optimised windows? You know Xbox Series X is just a PC running a heavily customised version of Windows 10, right?
    • Let me guess, just big hyped cut scenes for what is really just more run around in the same old static urban zone machine gun vs. sniper game play.
    • I am not seeing a huge difference there.
  • Recent Achievements

    • First Post
      George Almeyda earned a badge
      First Post
    • Reacting Well
      BlakeBringer earned a badge
      Reacting Well
    • Reacting Well
      Lazy_Placeholder earned a badge
      Reacting Well
    • Dedicated
      Epaminombas earned a badge
      Dedicated
    • Veteran
      Yonah went up a rank
      Veteran
  • Popular Contributors

    1. 1
      +primortal
      470
    2. 2
      +FloatingFatMan
      265
    3. 3
      ATLien_0
      235
    4. 4
      snowy owl
      219
    5. 5
      Edouard
      173
  • Tell a friend

    Love Neowin? Tell a friend!