Einzelnen Beitrag anzeigen

dust258

Registriert seit: 18. Aug 2008
62 Beiträge
 
#1

Paradoxes verhalten beim Aufruf einer C++ DLL aus Delphi

  Alt 4. Nov 2010, 11:46
Hallo Leute,
ich beschäftige mich im Moment mit der dll-Programmierung unter C++. Mir ist allerdings aufgefallen das von alle Integer-Werten, die ich von Delphi aus in meine DLL übergebe, immer 1 abgezogen wird...
Ich habe also die Funktion in einem Testprojekt nachgestellt, und auch hier tritt der "Fehler" auf. Kann mir das Jemand erklären?

Hier mein Delphi Testprojekt:
Delphi-Quellcode:
unit u_FormAufruf;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls, ComCtrls;

  type
    TSecSigner_AssignDocuments =
    function( inArrDocs : Array of PChar;
            inIDocCount : Integer)
          : Integer; cdecl;
type
  TFormMain = class(TForm)
    Button1: TButton;
    reDisplay: TRichEdit;
    edDllName: TEdit;
    edDllFunktion: TEdit;
    lbDllName: TLabel;
    lbDllFunktion: TLabel;
    procedure Button1Click(Sender: TObject);
  private
    fLibHandle: THandle;
    SecSigner_AssignDocuments : TSecSigner_AssignDocuments;
    function loadSecSignerLibrary: Boolean;
  public
    { Public-Deklarationen }
  end;

var
  FormMain: TFormMain;

implementation

{$R *.dfm}

procedure TFormMain.Button1Click(Sender: TObject);
var
  lArrDocs : Array of PChar;
  lICount,
  lIResult,
  I: Integer;
begin

  SetLength(lArrDocs, 4);
  lArrDocs[0] := 'test1';
  lArrDocs[1] := 'test2';
  lArrDocs[2] := 'test3';
  lArrDocs[3] := 'test4';

  if loadSecSignerLibrary then
  try
    if Assigned(SecSigner_AssignDocuments) then
    begin
      lICount := Length(lArrDocs);
      for I := 0 to Length(lArrDocs) - 1 do
      begin
        reDisplay.Lines.Add('-'+lArrDocs[i]);
      end;
      reDisplay.Lines.Add('Übergabe Count: '+IntToStr(lICount));
      lIResult := SecSigner_AssignDocuments(lArrDocs, lICount); //Hier Hat lICount den Wert 4
      reDisplay.Lines.Add('Result: '+IntToStr(lIResult));
    end else
      reDisplay.Lines.Add('Funktion nicht gefunden');
  finally
    freeLibrary(fLibHandle);
  end;
end;

function TFormMain.loadSecSignerLibrary: Boolean;
begin
  Result := true;
  try
    fLibHandle := loadLibrary(PChar(edDllName.text));
    if fLibHandle <> 0 then
    begin
      @SecSigner_AssignDocuments := getProcAddress(fLibHandle, PChar(edDllFunktion.Text));
      reDisplay.Lines.Add('getProcAddress erfolgreich');
    end else
    begin
      Result := false;
      reDisplay.Lines.Add('DLL nicht gefunden');
    end;
  except
    Result := false;
  end;
end;

end.
und die C++ DLL:
Code:
#include "stdafx.h"
#include <windows.h>
#include "DllTest.h"
#include <TCHAR.H>
#include <stdio.h>

#pragma comment (lib, "user32.lib")



extern "C" __declspec(dllexport)int AssignDocuments(char* inArrFilenames[], int inIDocCount);
int AssignDocuments(char* inArrFilenames[], int inIDocCount)
{
   int i = inIDocCount;
   TCHAR str1[100];
   _stprintf(str1, _T("inIDocCount = %d"), inIDocCount);

   TCHAR str2[100];
   _stprintf(str2, _T("i in C++ = %d"), i); //Hier gibt die MessageBox 3 Aus

   MessageBox ( NULL, str1, str2, MB_OK);
   return i;
}
Das Projekt befindet sich außerdem im Anhang.
Angehängte Dateien
Dateityp: zip DllTest.zip (1,60 MB, 2x aufgerufen)
  Mit Zitat antworten Zitat