title: Code translation from pascal to c#
body: <p>I have a c++ dll which is used in a delphy pascal project and I want to create a c# project to use it. 
  The dll returns an interface object and I am stuck right now on how to implement the C# interface to be recognized by dllImport. I have the code in pascal, that works. Also, I do not have access to the c++ source of the dll.</p>
  
  <p>here is the delphy part:</p>
  
  <pre><code>interface
  uses
    IdGlobal;
  type
  //  TIdBytes = array of Byte;
    TBUF2 = array[0..1] of Byte;
    TBUF3 = array[0..2] of Byte;
    TBUF4 = array[0..3] of Byte;
    PBUF4 = ^TBUF4;
    TBUF6 = array[0..5] of Byte;
    TBUF8 = array[0..7] of Byte;
    TBUF16 = array[0..15] of Byte;
  
    TUDPRcvRcd = record
      Head: TBUF3;
      DevType: Byte;
      DevID: Byte;
      DevIP: TBUF4;
      DevMAC: TBUF6;
      SoftVer: TBUF2;
      HardVer: TBUF2;
      DevName: TBUF16;
      CHK: Byte;
    end;
  
    ReceiveDataEvent = procedure(aData: TIdBytes) of object;
    ListRecive = procedure(aData: TIdBytes; aMac: PChar) of object;
  
    ILhm = interface
      function SearchDev: Integer; stdcall;
      function SetRcvSearchResultProc(aReceiveDataEvent: ReceiveDataEvent ): Boolean; stdcall;
      function ConnectDev(aMac, aIp, aPort, aPsd: PChar): Boolean; stdcall;
      function RemoveDev(aMac: PChar): Boolean; stdcall;
      function SendDataByMac(aData: TIdBytes; aMac: PChar): Boolean; stdcall;
      function SetRcvDevDataProc(aListRecive: ListRecive ): Boolean; stdcall;
    end;
  
  
  function ControllerInit: ILhm; stdcall; external 'controller.dll' name 'ControllerInit';
  
  implementation
  end.
  </code></pre>
  
  <p>I wrote this in my c# project until now</p>
  
  <pre><code> [DllImport("controller.dll",
   //EntryPoint = "ControllerInt", SetLastError = true, 
   CallingConvention = CallingConvention.Cdecl)]
   public static extern ILhm ControllerInit();
  
   public delegate object ReceiveDataEvent (byte[] aData);
   public delegate object ListRecive(byte[] aData, string aMac);
  
  
      public interface ILhm
      {
          int SearchDev();
  
          bool SetRcvSearchResultProc(ReceiveDataEvent aReceiveDataEvent);
  
          bool ConnectDev(string aMac, string aIp, string aPort, string aPsd);
          bool RemoveDev(string aMac);
          bool SendDataByMac(byte[] aData, string aMac);
          bool SetRcvDevDataProc(ListRecive aListRecive);
      }
  </code></pre>
  
  <p>I receive various errors regarding wrong definition of PInvoke or :</p>
  
  <pre><code>Additional information: The runtime has encountered a fatal error. The address of the error was at 0x7221dd74, on thread 0x305c. The error code is 0xc0000005. This error may be a bug in the CLR or in the unsafe or non-verifiable portions of user code. Common sources of this bug include user marshaling errors for COM-interop or PInvoke, which may corrupt the stack.
  </code></pre>
  
  <p>Thanks!</p>