Hack Forums
[.NET] Marshall.SizeOf() Bypass unmanaged restriction - Printable Version

+- Hack Forums (https://hackforums.net)
+-- Forum: Tech (https://hackforums.net/forumdisplay.php?fid=444)
+--- Forum: Coding (https://hackforums.net/forumdisplay.php?fid=151)
+---- Forum: Software Development (https://hackforums.net/forumdisplay.php?fid=118)
+---- Thread: [.NET] Marshall.SizeOf() Bypass unmanaged restriction (/showthread.php?tid=4449756)



Marshall.SizeOf() Bypass unmanaged restriction - TheBarret - Sep 16, 2014

I suppose only a few of here really ever used this method from the marshall namespace,
for those that are not familaire to the marshalling methods and its meaning,
please refer to the wiki for more explaination.

http://en.wikipedia.org/wiki/Marshalling_(computer_science)
Quote:Marshalling is the process of transforming the memory representation of an object to a data format suitable for storage or transmission, and it is typically used when data must be moved between different parts of a computer program or from one program to another. Marshalling is similar to serialization


Anyway, to continue i used it for a series of tests in some of my projects,
and i was bothered by the fact that the SizeOf() is restricted to unmannged code only or basic types.

So i begun my hunting for a way to bypass the compiler restriction wich i found out was quit easy actually.
There was a (stackoverflow.com) thread about this matter and i got my solution.
The guy in qeustion who broke down the easiest way was called dasblinkenlight (Thanks dude ^^).

Now if you play arround with the SizeOf you would most likely into the following exception:

Quote:Type cannot be marshaled as an unmanaged structure; no meaningful size or offset can be computed.


In this situation you could do a little trick to obtain the size of that structure by measuring the memory usage with the garbage collector using the GetTotalMemory() method.
Code
System.GC.GetTotalMemory

But its been said this is not thread safe, and i do work with threads alote,
so i don't wanna put crap infront of my plans ahead, we need to come up with a more cooked-up trick up our sleeve here.

Here are my test results and my findings

[Image: h6nxhxw.png]

Reference table from msdn:
  • sizeof(sbyte) 1 bytes

  • sizeof(byte) 1 bytes

  • sizeof(short) 2 bytes

  • sizeof(ushort) 2 bytes

  • sizeof(int) 4 bytes

  • sizeof(uint) 4 bytes

  • sizeof(long) 8 bytes

  • sizeof(ulong) 8 bytes

  • sizeof(char) 2 bytes

  • (Uni) sizeof(float) 4 bytes

  • sizeof(double) 8 bytes

  • sizeof(decimal) 16 bytes

  • sizeof(bool) 1 bytes



The solution is simple, we just create our own SizeOf() method and we emit the IL opcode for it and create a delegate followed by invoking it.
Works just as fine, no sweat has been broken here 👍

Source solution:
Code
Public Shared Function SizeOf(Instance As Object) As Integer
      Dim Method As New DynamicMethod("SizeOf", GetType(UInteger), Nothing, True)
      Dim ILGen As ILGenerator = Method.GetILGenerator()
      ILGen.Emit(OpCodes.Sizeof, Instance.GetType)
      ILGen.Emit(OpCodes.Ret)
      Return CInt(Method.CreateDelegate(GetType(Func(Of UInteger))).DynamicInvoke)
End Function

I hope it helps anybody out there, as it did for me.

- Barret 🎉


* - SIM-1456245 - Sep 16, 2014

That's very neat as always. But the real question is, why would you want the size of a type, they only usage I've seen is in obfuscators to make your life harder, but in the real world, wtf ??


* - TheBarret - Sep 16, 2014

(Sep 16, 2014 09:48 AM)Electroalek Wrote:  That's very neat as always. But the real question is, why would you want the size of a type, they only usage I've seen is in obfuscators to make your life harder, but in the real world, wtf ??

Debug info in my script engine, my structures (custom ones like the 2d and 3dvector) can be reduced or any other custom structure, its for memory and speed optmizing a usefull tool.


* - SaketSaket - Sep 16, 2014

I never used marshall namespace... so don't know anything about it... I hope i will learn something from it & use it in future ;)


* - Cornflower Blue இڿڰۣ-ڰۣ— - Sep 16, 2014

This is something I can find a use for, unlike your other releases :P
Goodjob!


* - Only3 - Sep 16, 2014

😊

[Image: pipXnDU.png]


* - TheBarret - Sep 16, 2014

(Sep 16, 2014 10:51 AM)Cornflower Blue Wrote:  This is something I can find a use for, unlike your other releases :P
Goodjob!

The joke is on you mate ^^ coz im already happy with what ive accomplished for myself, and whatever they tell me is wrong or stupid, as long i am happy, u can for all i care get a testicular disease , no offense intended ofc 😅


* - Cornflower Blue இڿڰۣ-ڰۣ— - Sep 16, 2014

(Sep 16, 2014 10:59 AM)TheBarret Wrote:  The joke is on you mate ^^ coz im already happy with what ive accomplished for myself, and whatever they tell me is wrong or stupid, as long i am happy, u can for all i care get a testicular disease , no offense intended ofc 😅

Okay? I was being nice and saying it was a good release and you come back saying that... Bit mean.


* - TheBarret - Sep 16, 2014

(Sep 16, 2014 11:03 AM)Cornflower Blue Wrote:  Okay? I was being nice and saying it was a good release and you come back saying that... Bit mean.

Ofc not was a joke, just like you made one 😊 see look at the last part: "no offense intended" 🤭


* - Cornflower Blue இڿڰۣ-ڰۣ— - Sep 16, 2014

(Sep 16, 2014 11:04 AM)TheBarret Wrote:  Ofc not was a joke, just like you made one 😊 see look at the last part: "no offense intended" 🤭

You other release "float to hex" was kinda useless tho, atleast it was for me


* - TheBarret - Sep 16, 2014

(Sep 16, 2014 11:12 AM)Cornflower Blue Wrote:  You other release "float to hex" was kinda useless tho, atleast it was for me

I enforce nobody to read, use or anything of anykind on my work, source code or applications. You are free to use them and free to ask me help on the matter.
In return if one does use my work or needs help, one should realize that i put this together for other ppl in my free time, if one decides to complain about it that it was useless or not usefull enough, then ask yourself, why should one read something that he or she has no intrest for and secondly, why even bother responding to something that doesn't help you in the first place?

So now ive made that clear. This is what my statement is and all you get.