Home Upgrade Search Memberlist Extras Hacker Tools Award Goals Help Wiki Contact

.NET 

Marshall.SizeOf() Bypass unmanaged restriction

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...r_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 🎉
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 ??
(Sep 16, 2014 09:48 AM)SIM-1456245 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.
I never used marshall namespace... so don't know anything about it... I hope i will learn something from it & use it in future ;)
BTC - bc1q3efew3yzfwhdz290dafykaw0a2w56g2j69g2c6
ETH - 0xf1c873217032468990D3313da6509D1b813c4671
USDT - 0xf1c873217032468990D3313da6509D1b813c4671
PP - [email protected]
This is something I can find a use for, unlike your other releases :P
Goodjob!
😊

[Image: pipXnDU.png]
(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 😅
(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.
(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" 🤭
(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
(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.


Possibly Related Threads...
Thread / Author Replies Views Last Post
[C#] by DistrictAug 23, 2020 11:38 PM 1 181 Aug 23, 2020 11:38 PM
Last Post: Accord
[C] by SirhofiioSep 24, 2018 02:56 PM 2 125 Sep 24, 2018 02:56 PM
Last Post: pmtobefriend
[C] by aeiroAug 19, 2018 06:03 PM 2 148 Aug 19, 2018 06:03 PM
Last Post: aeiro