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

Asked

Viewed 195 times

1

Good morning, I created using Unmanagedexports a dll in c# (Visual Studio 2010) to be consumed in a form Delphi 5, as per:

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;
            }
        }
    }


}

Dll call 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 options "Make Assembly COM-Visible" and "Register for COM Interop". The DLL call is performed correctly, the function is activated and everything else, however both my variables are going with incorrect value, the message generated by the excerpt "Messagebox.Show(testStr);" comes incorrect characters, and the return of the function is 1. I don’t know what I’m doing wrong, someone can help me?

@EDIT - I solved people, it was missing to add the stdcall directive in the dll function reference statement in Delphi, as per:

TTesteDll = function (testeInt: Integer; testeStr: String): Integer; stdcall;
  • Put an answer to the question then, to be documented.

1 answer

1


I managed to solve, on the line (from the Delphi code):

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

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

Browser other questions tagged

You are not signed in. Login or sign up in order to post.