Click here to Skip to main content
15,911,531 members
Home / Discussions / C#
   

C#

 
AnswerRe: How can I retrieve a list of available ports on a specified ip? Pin
led mike12-Jan-07 5:16
led mike12-Jan-07 5:16 
GeneralRe: How can I retrieve a list of available ports on a specified ip? Pin
wxShayan12-Jan-07 5:23
wxShayan12-Jan-07 5:23 
AnswerRe: How can I retrieve a list of available ports on a specified ip? Pin
Eric Dahlvang12-Jan-07 6:39
Eric Dahlvang12-Jan-07 6:39 
GeneralRe: How can I retrieve a list of available ports on a specified ip? Pin
wxShayan12-Jan-07 17:00
wxShayan12-Jan-07 17:00 
QuestionLCG issue (DynamicMethod / ILGenerator) Pin
rcollina12-Jan-07 5:03
rcollina12-Jan-07 5:03 
AnswerRe: LCG issue (DynamicMethod / ILGenerator) Pin
S. Senthil Kumar12-Jan-07 6:42
S. Senthil Kumar12-Jan-07 6:42 
GeneralRe: LCG issue (DynamicMethod / ILGenerator) [modified] Pin
rcollina12-Jan-07 8:49
rcollina12-Jan-07 8:49 
GeneralRe: LCG issue (DynamicMethod / ILGenerator) Pin
S. Senthil Kumar12-Jan-07 20:31
S. Senthil Kumar12-Jan-07 20:31 
Roberto Collina wrote:
By removing ldnull I get a unhandled AccessViolationException, exactly where DynamicMethod.Invoke is called.
"Attempted to read or write protected memory. This is often an indication that other memory is corrupt."

That's a strange error for a managed application. Do any of the invoked methods end up calling unmanaged code?


Roberto Collina wrote:

Edit 2: I guess Ldnull is needed since the return value is of type "void".

No, it's not. Here's the IL dump generated by ildasm for a simple method that prints "Hello" to the console.
.method public hidebysig instance void  TestMethod() cil managed
{
  // Code size       13 (0xd)
  .maxstack  8
  IL_0000:  nop
  IL_0001:  ldstr      "Hello"
  IL_0006:  call       void [mscorlib]System.Console::WriteLine(string)
  IL_000b:  nop
  IL_000c:  ret
} // end of method Program::TestMethod

You can see that there is no ldnull instruction before the ret statement.

Could you try running the following program? It is the test program I tried for reproducing your problem.
public class Program
    {
        delegate void MyDelegate();
        public void TestMethod(){Console.WriteLine("Hello");}
        public static void Main()
        {
            Program p = new Program();
            MethodInfo methodInfo = p.GetType().GetMethod("TestMethod");
            DynamicMethod dynMethod = new DynamicMethod("DynTestMethod", null, null, typeof(Program));
            ILGenerator gen = dynMethod.GetILGenerator();
            gen.Emit(OpCodes.Ldarg_0);
            gen.Emit(OpCodes.Call, methodInfo);
            //gen.Emit(OpCodes.Ldnull); // If you uncomment, you'll get InvalidProgramException
            gen.Emit(OpCodes.Ret);
            MyDelegate d = (MyDelegate)dynMethod.CreateDelegate(typeof(MyDelegate));
            d.Invoke();
        }
    }

As you can see, it generates DynTestMethod, which simply forwards the call to TestMethod. If this works for you, then you can be sure that the IL generation part is correct.

Also, are any of your MethodInfo's static? If they are, then OpCodes.Ldarg_0 should not be emitted for those methods. That statement is for passing the this parameter and it should not be passed to static methods.

Regards
Senthil [MVP - Visual C#]
_____________________________
My Blog | My Articles | My Flickr | WinMacro

GeneralRe: LCG issue (DynamicMethod / ILGenerator) [modified] Pin
rcollina13-Jan-07 13:45
rcollina13-Jan-07 13:45 
GeneralRe: LCG issue (DynamicMethod / ILGenerator) Pin
S. Senthil Kumar13-Jan-07 21:07
S. Senthil Kumar13-Jan-07 21:07 
GeneralRe: LCG issue (DynamicMethod / ILGenerator) [modified] Pin
rcollina14-Jan-07 2:27
rcollina14-Jan-07 2:27 
GeneralRe: LCG issue (DynamicMethod / ILGenerator) Pin
rcollina15-Jan-07 14:02
rcollina15-Jan-07 14:02 
GeneralRe: LCG issue (DynamicMethod / ILGenerator) Pin
rcollina17-Jan-07 13:28
rcollina17-Jan-07 13:28 
GeneralRe: LCG issue (DynamicMethod / ILGenerator) Pin
S. Senthil Kumar17-Jan-07 17:51
S. Senthil Kumar17-Jan-07 17:51 
GeneralRe: LCG issue (DynamicMethod / ILGenerator) Pin
rcollina20-Jan-07 10:02
rcollina20-Jan-07 10:02 
QuestionFinding a Sorted DataGridView row in a Dataset Pin
efriese112-Jan-07 4:21
efriese112-Jan-07 4:21 
AnswerRe: Finding a Sorted DataGridView row in a Dataset Pin
Mircea Puiu12-Jan-07 4:34
Mircea Puiu12-Jan-07 4:34 
GeneralRe: Finding a Sorted DataGridView row in a Dataset Pin
efriese112-Jan-07 4:53
efriese112-Jan-07 4:53 
GeneralRe: Finding a Sorted DataGridView row in a Dataset Pin
Mircea Puiu12-Jan-07 5:00
Mircea Puiu12-Jan-07 5:00 
GeneralRe: Finding a Sorted DataGridView row in a Dataset Pin
efriese112-Jan-07 5:09
efriese112-Jan-07 5:09 
GeneralRe: Finding a Sorted DataGridView row in a Dataset Pin
Mircea Puiu12-Jan-07 5:12
Mircea Puiu12-Jan-07 5:12 
GeneralRe: Finding a Sorted DataGridView row in a Dataset Pin
efriese112-Jan-07 5:19
efriese112-Jan-07 5:19 
GeneralRe: Finding a Sorted DataGridView row in a Dataset Pin
Mircea Puiu12-Jan-07 5:20
Mircea Puiu12-Jan-07 5:20 
GeneralRe: Finding a Sorted DataGridView row in a Dataset Pin
efriese112-Jan-07 5:28
efriese112-Jan-07 5:28 
AnswerRe: Finding a Sorted DataGridView row in a Dataset Pin
Drew McGhie12-Jan-07 9:55
Drew McGhie12-Jan-07 9:55 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.