• 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

    • That'll be it for my 2018 Mac mini then. There is, of course, OpenCore Legacy Patcher . . .
    • Support cost cutting and nudging people to upgrade for profit. Pure and simple. Nothing as noble as hardware actually becoming "obsolete" about most of the hardware requirements from Apple, Microsoft, or Android companies either. iMac 2017 with AMD running legacy Core still have native GPU support in macOS 15 and run perfectly fine. Doesn't even need any fancy patching outside of the minimum patching needed to get macOS 15 on the system to bypass Apple system requirements aking to running windows 11 on a PC that doesn't match the arbitrary CPU generation requirements that make you bypass them too. You can usually tell if hardware is truly in the realm of "obsolete" if you are having a hard time finding a major Linux distro that'll install on it.
    • >Mozilla's Firefox has been left behind over time in terms of market share, as it has not been able to keep up with Chromium-based browsers in the performance department. I have no problems with Firefox's performance whatsoever. I suspect the reason Firefox is lagging in market share is that average consumers haven't heard of it and are fine with what was installed on their systems.
    • The Expanse: Osiris Reborn is a new narrative-driven sci-fi RPG inspired by Mass Effect by Pulasthi Ariyasinghe Out of nowhere, a narrative-driven sci-fi action RPG has been announced that will take players into the universe of The Expanse TV show and novels. The reveal trailer landed today during the Future Games Showcase, offering both a cinematic look at the setting as well as snippets of gameplay. Check out the The Expanse: Osiris Reborn debut trailer above. The studio behind the project is Owlcat Games. Some may remember that name from being involved in the role-playing titles Pathfinder: Kingmaker, Pathfinder: Wrath of the Righteous, as well as the most recent hugely well-received RPG Warhammer 40,000: Rogue Trader. Now, the studio's sights are set on The Expanse universe, and it's going for the over-the-shoulder third-person gameplay route for the first time. “We’ve been dreaming about building a sci-fi action RPG of this scale for a long time, and The Expanse is the perfect universe to bring that vision to life,” says Owlcat’s Creative Director, Alexander Mishulin. “It’s a world grounded in realism and complexity, perfect for telling a story the way we like it— mature and character-driven, where your choices truly matter." The story will have players taking the role of a custom captain that can be from the Earth, Mars, or the Belt to take control of the most advanced ship currently available. There's a crew to meet and lead, tactical third-person combat, and, as expected from this franchise, a divided solar system to navigate and make decisions on. The studio was also very direct about this experience being inspired by BioWare's Mass Effect trilogy. Aside from the action-heavy gameplay seen in the trailer, Owlcat is promising plenty of political intrigue, as well as romance options for players to dive into. Game Design Producer Yuliya Chernenko added "many of us first played it in our teenage years, and it left a lasting impression," and that "we are building on that legacy and expanding what players anticipate from this experience." The Expanse: Osiris Reborn does not have a release window just yet, but it will be coming out on PC (Steam, Epic Games Store, GOG), Xbox Series X|S, and PlayStation 5.
    • I don't know of anyway other than winareo tweaker to set the font type in Win11. They took that option away!   No wonder people are staying with Win10, or 7.   It's not our computer with Win11.... it's theirs!
  • Recent Achievements

    • Conversation Starter
      Naomi723 earned a badge
      Conversation Starter
    • Week One Done
      abortretryfail earned a badge
      Week One Done
    • First Post
      Mr bot earned a badge
      First Post
    • First Post
      Bkl211 earned a badge
      First Post
    • One Year In
      Mido gaber earned a badge
      One Year In
  • Popular Contributors

    1. 1
      +primortal
      486
    2. 2
      +FloatingFatMan
      256
    3. 3
      snowy owl
      243
    4. 4
      ATLien_0
      222
    5. 5
      +Edouard
      191
  • Tell a friend

    Love Neowin? Tell a friend!