title: Marshalling C type to C# for FMU dll method calling
body: <p>I'm trying to call methods from unmanaged dll and I'm stucked.
  For fmi2Type and fmi2Boolean I can use byte?
  How callback functions (and pointer to them) should looks like in C#?</p>
  
  <p>Method I'm trying to call is:</p>
  
  <pre class="lang-cpp prettyprint-override"><code>fmi2Component fmi2Instantiate(fmi2String instanceName,
                      fmi2Type fmuType, 
                      fmi2String fmuGUID, 
                      fmi2String fmuResourceLocation, 
                      const fmi2CallbackFunctions* functions, 
                      fmi2Boolean visible, 
                      fmi2Boolean loggingOn)
  </code></pre>
  
  <p>Type descriptions are:</p>
  
  <pre class="lang-cpp prettyprint-override"><code>typedef void*           fmi2Component;               /* Pointer to FMU instance       */
  typedef void*           fmi2ComponentEnvironment;    /* Pointer to FMU environment    */
  typedef void*           fmi2FMUstate;                /* Pointer to internal FMU state */
  typedef unsigned int    fmi2ValueReference;
  typedef double          fmi2Real   ;
  typedef int             fmi2Integer;
  typedef int             fmi2Boolean;
  typedef char            fmi2Char;
  typedef const fmi2Char* fmi2String;
  typedef char            fmi2Byte;
  
  typedef struct {
     const fmi2CallbackLogger         logger;
     const fmi2CallbackAllocateMemory allocateMemory;
     const fmi2CallbackFreeMemory     freeMemory;
     const fmi2StepFinished           stepFinished;
     const fmi2ComponentEnvironment   componentEnvironment;
  } fmi2CallbackFunctions;
  
  typedef enum {
      fmi2ModelExchange,
      fmi2CoSimulation
  } fmi2Type;
  
  typedef void      (*fmi2CallbackLogger)        (fmi2ComponentEnvironment,     fmi2String, fmi2Status, fmi2String, fmi2String, ...);
  typedef void*     (*fmi2CallbackAllocateMemory)(size_t, size_t);
  typedef void      (*fmi2CallbackFreeMemory)    (void*);
  typedef void      (*fmi2StepFinished)          (fmi2ComponentEnvironment,     fmi2Status);
  </code></pre>
  
  <p>For callbacks I made callbacks containing class and delegates for each callback.</p>
  
  <pre class="lang-cs prettyprint-override"><code>public class FMICallbackFunctions
  {
      public delegate void FMILibrary_FMICallbackLogger(IntPtr fmiComponent, string instanceName, int status, string category, string message, IntPtr parameters);
  
      public delegate IntPtr FMILibrary_FMICallbackAllocateMemory(int numberOfObjects, int size);
  
      public delegate void FMILibrary_FMICallbackFreeMemory(IntPtr @object);
  
      public delegate void FMILibrary_FMIStepFinished(IntPtr fmiComponent, int status);
  
      public FMICallbackFunctions(FMILibrary_FMICallbackLogger logger, FMILibrary_FMICallbackAllocateMemory allocateMemory, FMILibrary_FMICallbackFreeMemory freeMemory, FMILibrary_FMIStepFinished stepFinished)
          : base()
      {
          this.logger = logger;
          this.allocateMemory = allocateMemory;
          this.freeMemory = freeMemory;
          this.stepFinished = stepFinished;
      }
  }
  </code></pre>
  
  <p>Then initialize that class and trying to pass it to unmanaged method call</p>
  
  <pre class="lang-cs prettyprint-override"><code>FMICallbackFunctions callbackFunctions = new FMICallbackFunctions(logger, allocateMemory, freeMemory, stepFinished);
  </code></pre>
  
  <p>but getting an error: Attempted to read or write protected memory. This is often an indication that other memory is corrupt.</p>
  
  <p>I think it cause of wrong callback parameters. Have anyone any hints what might be wrong?</p>