Calling C # dll function (VS2010) using COM in Delphi 5 does not pass the parameters correctly

1

Good morning, I created using UnmanagedExports a dll in C # (Visual Studio 2010) to be consumed in a form delphi 5, as follows:

C #:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Security.Cryptography.X509Certificates;
using RGiesecke.DllExport;
using System.Runtime.InteropServices;
using System.Windows.Forms;

namespace TesteDllCom
{
    [ComVisible(true)]
    [Guid("CE805AD4-511E-4E63-A37F-9FF6C97D664B")]
    public class TesteDll 
    {
        [DllExport]
        public static int TestarDll(int testeInt, string testeStr)
        {
            MessageBox.Show(testeStr);
            if (testeInt == 0)
            {
                 return 0;
             }
             else
             {
                 return 1;
            }
        }
    }


}

Calling the dll in Delphi:

unit frmTesteDll;

interface

uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  StdCtrls, WinInet, ComObj;

type
  TfrmTesteDll = class(TForm)
    btn1: TButton;
    procedure btn1Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  frmTesteDll: TfrmTesteDll;

implementation

{$R *.DFM}

procedure TfrmTesteDll.btn1Click(Sender: TObject);
type
  TTesteDll = function (testeInt: Integer; testeStr: String): Integer;
var
  dllHandle: THandle;
  funcTestarDll: TTesteDll;
  retorno: Integer;
begin
   dllHandle :=  LoadLibrary('TesteDllCom.dll');
   @funcTestarDll := GetProcAddress(dllHandle, 'TestarDll');
   retorno := funcTestarDll(0, 'teste');
   if retorno <> 0 then
     ShowMessage('Erro');
end;

end.

In the c # project I checked the "Make Assembly COM-Visible" and "Register for COM interop" options. The DLL call is performed correctly, the function is activated and everything else, but both of my variables are going with incorrect value, the message generated by the "MessageBox.Show (testStr);" there are incorrect characters, and the function return is 1. I do not know what I'm doing wrong, can anyone help me?

@EDIT - I have decided to add the stdcall directive in the dll function reference declaration in delphi, as follows:

TTesteDll = function (testeInt: Integer; testeStr: String): Integer; stdcall;
    
asked by anonymous 31.10.2018 / 14:12

1 answer

1

I was able to solve, in the line (of Delphi code):

TTesteDll = function (testeInt: Integer; testeStr: String): Integer;

The stdcall directive was missing.
I added it and it worked perfectly.

    
05.11.2018 / 17:17