• 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

    • Will be replacing my last 2 Windows 10 PC's in household ASAP, if they could upgrade now i've done it already. Main Desktop runs Windows 11 already, that ones all set. Now just to get the other 2 all set asap
    • I think you answered why it gets so much attention in your comment. It's designed to make Windows accessible to everyone! It's a core component of the Windows UI, and 30 years of its existence is why it remains such a key component and receives so much attention to this day!
    • I agree with your points, particularly around workflows. But I think this is a symptom of the way Microsoft plans, builds, and releases its OS's, not about its UI ideals itself. It has historically been large jumps in UI changes between releases, XP > Vista > 7 > 8 were ALL significant changes in how we performed tasks. Then Win10 was released and updated twice a year with minor changes. This was somewhat nice as the changes were often minimal, however as it looked like the same OS for the most part, when something did change (like the start menu), it was a bit confusing for people who didn't understand that they had actually been upgraded, and why just this one key part of their workflow had changed. But I believe that was too slow, and didn't allow their future designs to be played out in that release structure. So the "last Windows version ever" was superseded with a new UI and overhauled once more. It too has changed quite a bit in its 4 years, but I think there needs to be an understanding of that change, which I guess we have in the "24H2" version numbering, but not in the marketing that most users know. The most consistent and clear communication of changes I think is done with MacOS. While I'm not a fan of Apples software, their clear and consistent release schedule, and progressively planned changes to the OS is easy to understand, leading towards a well thought out goal. Their users understand the branding of the big updates, and that changes will occur when they update, yet these are more subtle than changes we see in Windows 7 > 8 > 10 > 11 releases. On that, Windows 10/11 version updates just get lost in the vast collection of update types in Windows Update, and as such most users wouldn't know if the were on 22H2, 23H2 or 24H2, or even know what those meant. My thoughts are that Microsoft will drop the Win11/Win12 branding once Windows 10 has finally died. And hopefully, make it a bit more consistent with feature/UI updates sticking to an improved branded version/year release rather than "moments" or just randomly enabled elements in a random monthly update. Until then, even us tech users rarely know what features are in what release and if we will see it on our own computers. Get with it MS!
    • Hello, Horizon Data Systems, the developer, has been around for 27 years.  I do not recall hearing anything bad about them, but I also haven't used their software, either. Regards, Aryeh Goretsky  
  • Recent Achievements

    • Reacting Well
      pelaird earned a badge
      Reacting Well
    • Mentor
      The Werewolf went up a rank
      Mentor
    • First Post
      Myriachan earned a badge
      First Post
    • Week One Done
      DrRonSr earned a badge
      Week One Done
    • Week One Done
      Sharon dixon earned a badge
      Week One Done
  • Popular Contributors

    1. 1
      +primortal
      601
    2. 2
      ATLien_0
      214
    3. 3
      +FloatingFatMan
      169
    4. 4
      Michael Scrip
      151
    5. 5
      Som
      151
  • Tell a friend

    Love Neowin? Tell a friend!