HighTechTalks DotNet Forums  

Cant call Marshal.PtrToStructure from a DynamicMethod

Dotnet Framework (CLR) microsoft.public.dotnet.framework.clr


Discuss Cant call Marshal.PtrToStructure from a DynamicMethod in the Dotnet Framework (CLR) forum.



Reply
 
Thread Tools Search this Thread Display Modes
  #1  
Old   
john conwell
 
Posts: n/a

Default Cant call Marshal.PtrToStructure from a DynamicMethod - 11-27-2007 , 08:12 PM






I'm trying to create a DynamicMethod to handle custom marshaling from a
pointer in memory to managed types. I'm trying to use a DynamicMethod so I
can template the code for all the custom marshaling and dont have to maintain
it.

My problem is I cant call Marshal.PtrToStructure in a DynamicMethod. I get
no exception, the method just exits. Its really odd. I've put debugging
WriteLine lines in my IL to find exactly what opcode was causing the problem,
and its the 10th opcode (shown below). It gets to that point, and then
"poof" (and I dont even believe in poofs in computers, but I cant explain it)

I then dumped my IL to an assembly (via reflection.enit) and opened it in
Reflector and compared it to the IL that the C# compiler created from my C#
code that does this. and its EXACTLY the same. I mean the IL for the entire
method is EXACTLY the same.

Does anyone have an idea? Below is the method def and the first 12 or so
opcodes, taken from Reflector.

One other thing; the C# method that I based this IL code from is marked as
unsafe, but I didnt see anything in Reflection.Emit to mark something as
unsafe.

..method public hidebysig virtual instance void
SetTraceData_ProcessEventData(void* pData, int32 length) cil managed
{
..maxstack 15
..locals init (
[0] int32 num,
[1] native int ptr,
[2] int32 num2,
[3] bool flag)

ldc.i4.0
stloc.0
ldloca.s ptr
ldarg.2
call instance void [mscorlib]System.IntPtr::.ctor(void*)
ldarg.1
ldloc.1
ldtoken int32
call class [mscorlib]System.Type
[mscorlib]System.Type::GetTypeFromHandle(valuetype
[mscorlib]System.RuntimeTypeHandle)
call object
[mscorlib]System.Runtime.InteropServices.Marshal::PtrToStruc ture(native int,
class [mscorlib]System.Type)
unbox.any int32
stfld int32
[Attenex.Instrumentation]Attenex.Instrumentation.Etw.ProcessEventData::inst anceId
..
..
..
ret
}

Reply With Quote
  #2  
Old   
john conwell
 
Posts: n/a

Default RE: Cant call Marshal.PtrToStructure from a DynamicMethod - 11-28-2007 , 12:22 PM






oh buggers, I figured it out. I actually was emmitting one wrong IL opcode.

the 4th opcode was "ldarg.2", and should have been "ldarg.1". Basically I
was trying to marshal data from a pointer in memory to the wrong place, where
my process didnt own. This caused badness.

I guess it goes to show how easy it is to screw up IL emittion :-)


"john conwell" wrote:

Quote:
I'm trying to create a DynamicMethod to handle custom marshaling from a
pointer in memory to managed types. I'm trying to use a DynamicMethod so I
can template the code for all the custom marshaling and dont have to maintain
it.

My problem is I cant call Marshal.PtrToStructure in a DynamicMethod. I get
no exception, the method just exits. Its really odd. I've put debugging
WriteLine lines in my IL to find exactly what opcode was causing the problem,
and its the 10th opcode (shown below). It gets to that point, and then
"poof" (and I dont even believe in poofs in computers, but I cant explain it)

I then dumped my IL to an assembly (via reflection.enit) and opened it in
Reflector and compared it to the IL that the C# compiler created from my C#
code that does this. and its EXACTLY the same. I mean the IL for the entire
method is EXACTLY the same.

Does anyone have an idea? Below is the method def and the first 12 or so
opcodes, taken from Reflector.

One other thing; the C# method that I based this IL code from is marked as
unsafe, but I didnt see anything in Reflection.Emit to mark something as
unsafe.

.method public hidebysig virtual instance void
SetTraceData_ProcessEventData(void* pData, int32 length) cil managed
{
.maxstack 15
.locals init (
[0] int32 num,
[1] native int ptr,
[2] int32 num2,
[3] bool flag)

ldc.i4.0
stloc.0
ldloca.s ptr
ldarg.2
call instance void [mscorlib]System.IntPtr::.ctor(void*)
ldarg.1
ldloc.1
ldtoken int32
call class [mscorlib]System.Type
[mscorlib]System.Type::GetTypeFromHandle(valuetype
[mscorlib]System.RuntimeTypeHandle)
call object
[mscorlib]System.Runtime.InteropServices.Marshal::PtrToStruc ture(native int,
class [mscorlib]System.Type)
unbox.any int32
stfld int32
[Attenex.Instrumentation]Attenex.Instrumentation.Etw.ProcessEventData::inst anceId
.
.
.
ret
}

Reply With Quote
  #3  
Old   
Günter Prossliner
 
Posts: n/a

Default Re: Cant call Marshal.PtrToStructure from a DynamicMethod - 11-29-2007 , 02:19 AM



Hello John!

Quote:
I then dumped my IL to an assembly (via reflection.enit) and opened
it in Reflector and compared it to the IL that the C# compiler
created from my C# code that does this.
If you do all this just to view the IL of the DynamicMethod, you should take
a look at this:


[Haibo Luo's weblog : DebuggerVisualizer for DynamicMethod (Show me the IL)]
http://blogs.msdn.com/haibo_luo/arch...25/484861.aspx



GP




Reply With Quote
Reply




Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Powered by vBulletin Version 3.5.4
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.