用VB在Dll中写了这样的代码:
Public Sub SetBoardId(BoardId As Integer)
  If (BoardId >= 0) Then
    mBoardId = BoardId
  Else
    mBoardId = 0
  End If
End Sub
在Delphi中调用该Dll的代码为:
//声明;
TGetBoardId=function:Integer;stdcall;
DGetBoardId:TGetBoardId;
//调用;
  FDllHandle:=LoadLibrary(PChar(FDllFileName));
  if(FDllHandle=0)then
  begin
    Raise Exception.Create('Load '''+FDllFileName+''' failed');
    Exit;
  end
  else
  begin
    //Dll functions////////////////////////
    //DSetBoardId;
    @DSetBoardId:=GetProcAddress(FDllHandle,'SetBoardId');
    if(@DSetBoardId=nil)then
    begin
      Raise Exception.Create('Can not load function ''SetBoardId''');//运行时这里报错;
      Exit;
    end;
请问是什么原因?怎样才能在Delphi中调用VB的Dll呢?

解决方案 »

  1.   

    VB生成的DLL是ACTIVEX DLL,调用的时候,直接在DELPHI里点project->import type library
    ,找到你那个DLL的名字,然后INSTALL就可以了,使用的时候,在ACTIVEX组里把你那个被DELPHI封装为VCL的组件拖出来使用就可以了,又或者,给你一个例子:
     在VB里建立一个名为clsBoard的类,然后在类里面添加
       Public Sub SetBoardId(BoardId As Integer)
         If (BoardId >= 0) Then
           mBoardId = BoardId
          Else
           mBoardId = 0
         End If
      End Sub
    编译成一个名为Board的DLL文件
      在Delphi里面用点击Project->Import Type Library在里面ADD那个Board.dll文件,然后你可以看到Class Name里面有个TclsBoard的类名,然后点击Create Unit按钮,生成一个Board_Tlb.pas文件,然后在Uses里面加入Board_Tlb,具体如下:unit Unit1;interfaceuses
      Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
      Dialogs, StdCtrls, Board_Tlb; //把Board_Tlb文件加到这里type
      TForm1 = class(TForm)
        Button1: TButton;
        procedure Button1Click(Sender: TObject);
      private
        { Private declarations }
      public
        { Public declarations }
      end;var
      Form1: TForm1;
      Board:TclsBoard;     //声明一个类;
    implementation{$R *.dfm}procedure TForm1.Button1Click(Sender: TObject);
    begin
      Board:=TclsBoard.Create(application.Handle); //实例化对象
      Board.SetBoardId(1);                         //调用方法
      Board.Free;                                  //释放对象
    end;end.