title: C++/CLI delegate as function pointer (System.AccessViolationException)
body: <p>I have been experimenting with C++/CLI delegates (as I am trying to make a .NET reference library), and I have been having the following problem.</p>
  <p>I define a delegate in C++/CLI, and then create an instance of the delegate in C#, and then call the instance of the delegate through unmanaged C++ via a function pointer. This all works as expected.</p>
  <p>Code to illustrate this (first my C#)</p>
  <pre><code>using System;
  
  namespace TestProgram
  {
      class Program
      {
          static void Main(string[] args)
          {
              Library.Test.MessageDelegate messageDelegate = new Library.Test.MessageDelegate(Message);
              Library.Test test = new Library.Test(messageDelegate);
              test.WriteMessage();
              Console.Read();
          }
  
          static void Message()
          {
              Console.WriteLine(1024);
          }
      }
  }
  </code></pre>
  <p>Next my managed c++ file (Managed.cpp)</p>
  <pre><code>#include &quot;Unmanaged.hpp&quot;
  #include &lt;string&gt;
  
  namespace Library
  {
      using namespace System;
      using namespace System::Runtime::InteropServices;
  
      public ref class Test
      {
      public:
          delegate void MessageDelegate();
      internal:
          MessageDelegate^ Message;
          void* delegatePointer;
  
      public:
          Test(MessageDelegate^ messageDelegate)
          {
              delegatePointer = (void*)Marshal::GetFunctionPointerForDelegate(messageDelegate).ToPointer();
          }
  
          void WriteMessage()
          {
              Unmanaged::WriteMessage(delegatePointer);
          }
      };
  }
  </code></pre>
  <p>And my unmanaged C++ file (Unmanaged.cpp)</p>
  <pre><code>#include &quot;Unmanaged.hpp&quot;
  
  namespace Unmanaged
  {
      typedef void (*WriteMessageType)();
      WriteMessageType WriteMessageFunc;
  
      void WriteMessage(void* Function)
      {
          WriteMessageType WriteMessageFunc = (WriteMessageType)(Function);
          WriteMessageFunc();
      }
  }
  </code></pre>
  <p>This code all works as expected, and the output is &quot;1024&quot;, as Method() is called by the function pointer to the delegate method.</p>
  <p>My problem arises when trying to apply the same method with a delegate with arguments, that is:</p>
  <pre><code>delegate void MessageDelegate(int number);
  </code></pre>
  <p>My code is now as follows (C#):</p>
  <pre><code>using System;
  
  namespace AddProgram
  {
      class Program
      {
          static void Main(string[] args)
          {
              Library.Test.MessageDelegate messageDelegate = new Library.Test.MessageDelegate(Message);
              Library.Test test = new Library.Test(messageDelegate);
              test.WriteMessage(1024);
              Console.Read();
          }
  
          static void Message(int number)
          {
              Console.WriteLine(number);
          }
      }
  }
  </code></pre>
  <p>My managed C++ file:</p>
  <pre><code>#include &quot;Unmanaged.hpp&quot;
  #include &lt;string&gt;
  
  namespace Library
  {
      using namespace System;
      using namespace System::Runtime::InteropServices;
  
      public ref class Test
      {
      public:
          delegate void MessageDelegate(int number);
      internal:
          MessageDelegate^ Message;
          void* delegatePointer;
  
      public:
          Test(MessageDelegate^ messageDelegate)
          {
              delegatePointer = (void*)Marshal::GetFunctionPointerForDelegate(messageDelegate).ToPointer();
          }
  
          void WriteMessage(int number)
          {
              Unmanaged::WriteMessage(delegatePointer, number);
          }
      };
  }
  </code></pre>
  <p>And my unmanaged C++ file:</p>
  <pre><code>#include &quot;Unmanaged.hpp&quot;
  
  namespace Unmanaged
  {
      typedef void (*WriteMessageType)(int number);
      WriteMessageType WriteMessageFunc;
  
      void WriteMessage(void* Function, int number)
      {
          WriteMessageType WriteMessageFunc = (WriteMessageType)(Function);
          WriteMessageFunc(number);
      }
  }
  </code></pre>
  <p>When I execute the program I get the following error:</p>
  <blockquote>
  <p>An unhandled exception of type 'System.AccessViolationException'
  occurred in Unmanaged Library Test.dll</p>
  <p>Additional information: Attempted to read or write protected memory.
  This is often an indication that other memory is corrupt.</p>
  </blockquote>
  <p>Incidentally, the console window does show 1024, but is then followed by a random int (~1000000), and then I get the error.</p>
  <p>I can begin to imagine some of the reasons I am getting this error, but I'm not sure and am having difficulty finding out. If anyone could tell me why I am getting this error, and what I could do to fix it, I would greatly appreciate it.</p>