• 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?


 

Link to comment
Share on other sites

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;
        }
Link to comment
Share on other sites

  • 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
Link to comment
Share on other sites

  • 0

Thanks for coming back and letting us know how it worked and for the fixed version. :)

 

Glad I could help.

Link to comment
Share on other sites

This topic is now closed to further replies.