Jump to content



Photo
Answered Go to the full post c# .net

  • Please log in to reply
3 replies to this topic

#1 ka-la

ka-la

    Neowinian

  • Joined: 24-October 12

Posted 19 June 2014 - 14:43

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?


 



Best Answer Squirrelington , 19 June 2014 - 14:53

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;
        }
Go to the full post



#2 Squirrelington

Squirrelington

    Squirrelies!

  • Tech Issues Solved: 2
  • Joined: 26-December 02
  • Location: Oshkosh, WI, USA
  • OS: Windows 8.1 64-bit Enterprise
  • Phone: Samsung Galaxy S5 (SM-G900T) - T-Mobile - 4.4.4

Posted 19 June 2014 - 14:53   Best Answer

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;
        }


#3 OP ka-la

ka-la

    Neowinian

  • Joined: 24-October 12

Posted 19 June 2014 - 14:58

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

 



#4 Squirrelington

Squirrelington

    Squirrelies!

  • Tech Issues Solved: 2
  • Joined: 26-December 02
  • Location: Oshkosh, WI, USA
  • OS: Windows 8.1 64-bit Enterprise
  • Phone: Samsung Galaxy S5 (SM-G900T) - T-Mobile - 4.4.4

Posted 19 June 2014 - 16:10

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

 

Glad I could help.