RE: Cant call Marshal.PtrToStructure from a DynamicMethod



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:

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::PtrToStructure(native int,
class [mscorlib]System.Type)
unbox.any int32
stfld int32
[Attenex.Instrumentation]Attenex.Instrumentation.Etw.ProcessEventData::instanceId
.
.
.
ret
}
.



Relevant Pages

  • Re: Accessing Unmanaged Data (C++) from .NET
    ... of memory returned. ... Dim gch As System.Runtime.InteropServices.GCHandle ... Public Function lockdata(ByVal HIIDC As Int32, ... Once you have that pointer in managed code, you are going to have to ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: SEAforth Instruction Set
    ... I could allocate some space for each type of interrupt and leave the ... registers to memory was a bit ugly on F21 despite having two memory ... load opcode, we called it # or N at the time, known as @p+ today. ...
    (comp.lang.forth)
  • Re: Opcode size < instruction memory word size?
    ... >have word-addressable instruction memory and can't access the unused ... >bits for opcode fetches, but does anyone know of a byte-addressable ... Usenet Zone Free Binaries Usenet Server ...
    (comp.arch)
  • Re: Cancel error without goto
    ... saved you some memory, even if it saved only half a dozen bytes. ... three bytes (an opcode followed by an address or by some data) and in some ... instead just jump straight into the part of the project that is unique. ... No need for an eprom programmer or baking chips in the sun. ...
    (microsoft.public.vb.general.discussion)
  • Re: Opcode size < instruction memory word size?
    ... > architectures where a fixed-length opcode is smaller than the ... I'm completely missing why the opcode width would have any ... relationship to the instruction memory size, ... I'm not sure what you mean by instruction memory size. ...
    (comp.arch)

Loading