我用c写了一组函数,编译成动态连接库,然后我用delphi里把它们封装到一个类里面,
作为类方法使用,但创建类对象->调用方法这里总是出错。
    把这组函数作为全局函数声明后调用就没问题,why?
比如在vc里
EXPORTS
   hello  @1
在delphi里
type THello=class(TObject)
   public
      function hello(): integer;stdcall;external 'hello.dll';name 'hello';
end;
这样使用:
var
   a:THello;
begin
  a:=Thello.create;
  a.hello;//出错;
 ...

解决方案 »

  1.   

    能,但是必须把所有export的函数声明成virtual(不能是dynamic),同时参数类型也受到很大的局限。
      

  2.   

    D5DG中的DLL章节有专门的阐述。
      

  3.   

    我说的不是引出DLL对象,是用类来封装vc的dll输出的函数
      

  4.   

    这个也是可以的。从经验看,调用失败最大的可能性是Delphi这侧没有正确声明VC的函数。
    主要是调用方式,C中默认的调用是cdecl,而Pascal默认得是register,Win32系统的函数大多都是stdcall。
    另外,我以前写VC程序的时候,发现VC会给Export的函数加一段乱七八糟的字符串,这个在声明的时候也要包括进去。
      

  5.   

    我是照着delphi里window.pas那个单元里那些调用windows API的格式调用的,知其然不知其所以然啊。
    这句有什么不对吗?
    function hello(): integer;stdcall;external 'hello.dll' name 'hello';
      

  6.   

    delphi从入门到精通  里有这样一个例子
    c:
    extern "C" __declspec(dllexport)
    int WINAPI Double (int n)
    {
    return n * 2;
    }extern "C" __declspec(dllexport)
    int WINAPI Triple (int n)
    {
    return n * 3;
    }__declspec(dllexport)
    int WINAPI Add (int a, int b)
    {
    return (a + b);
    }
    ================delphi======
    function Add (A, B: Integer): Integer;
      stdcall; external 'CPPDLL.DLL' name '@Add$qqsii';//您说的就是这个吧?
    function Double (N: Integer): Integer;
      stdcall; external 'CPPDLL.DLL' name 'Double';
    function Triple (N: Integer): Integer;
      stdcall; external 'CPPDLL.DLL';//这里没有那些乱东西了
      

  7.   

    可能就是这个$qqsii,必须加上。
      

  8.   

    这些字符串可以通过tdump,或者其他工具得到。
      

  9.   

    下面的途径代码参考:(关键在于THello类中的Hello方法的声明和实现)program Project1;
    uses
      Forms,
      Unit1 in 'Unit1.pas' {Form1};
    {$R *.res}
    begin
      Application.Initialize;
      Application.CreateForm(TForm1, Form1);
      Application.Run;
    end.unit Unit1;{略}implementation{$R *.dfm}type
      THello = class(Tobject)
      public
        function  Hello(H: HWND; Text: PChar): Integer;
      end;function  Hello(H: HWND; Text: PChar): Integer; external 'Project2.dll' name 'Hello';function  THello.Hello(H: HWND; Text: PChar): Integer;
    begin
      Result := Unit1.Hello(H, Text);
    end;procedure TForm1.Button1Click(Sender: TObject);
    begin
      with THello.Create do try
        Hello(Handle, 'OK');
      finally
        Free;
      end;
    end;
    {这里的Project1是一个EXE,Unit1与之对应
     Project2是一个DLL,Unit2与之对应
     其中Project1和Project2与一般的EXE和DLL没有特别不同之处。
    }
    end.library Project2;
    uses
      SysUtils,
      Classes,
      Unit2 in 'Unit2.pas';
    {$R *.res}
    exports
      Hello;
    begin
    end.unit Unit2;interfaceuses
      Windows;function  Hello(H: HWND; Text: PChar): Integer; 
    implementation
    function  Hello(H: HWND; Text: PChar): Integer;
    begin
      Result := MessageBox(H, Text, 'Hello Title', MB_YESNO);
    end;
    end.