我在 c++ 中定义了并实现了如下的对象接口,即一个纯虚类typedef   void  (__stdcall *CRasEvent)(
  int RasHandle, //拨号的唯一识别码
  int RasError, //出错是的错误代码;0是正确,其他是出错
  int RasState); //拨号的状态代码;
//注意值为 RAS_CONNECTED 是指拨号连接成功,class IRasDial
{
public:  //进行拨号
 virtual int __stdcall Dial(
char *PhoneNumber, //需拨的号码
char *UserName, //拨号时的用户名
char *Password, //拨号时的用户名密码
int NotifierType, //拨号信息的返回方式,
//0是指同步调用,
//1是指异步调用,通过Notifier的函数地址,其中Notifier具体为CRasEvent,
//0xffffffff 异步调用,通过Notifier的句柄发送USER_RASDIALEVENT消息,wParam 是拨号状态,lParam是错误代码
void *Notifier, //拨号时的状态和错误信息的回调函数地址
int &RasHandle)=0; //该拨号的一个唯一识别码};#ifdef _DIALERDLL
#define EXTERN __declspec(dllexport) 
#else
#define EXTERN __declspec(dllimport) 
#endifextern "C"
{
EXTERN IRasDial * __stdcall CreateRasDial();
}我想在 delphi 中共享此接口,我该怎么做?

解决方案 »

  1.   

    因为delphi里的类vmt兼容c++的vmt,所以可以这样
    // 为方便研究,我另写了一个测试用的类ITest
    // delphi中的声明
    const VMTTest = 'G:\PROJECTS\VMTTest\Debug\VMTTest.dll';
    type
      ITest = class
      public
        procedure HelloWorld; virtual; stdcall; abstract;
      end;  TCreateTest = function: ITest; stdcall;procedure TForm1.Button1Click(Sender: TObject);
    var I: ITest;
    var H: THandle;
    var CreateTest: TCreateTest;
    begin
      H := LoadLibrary(VMTTest);
      @CreateTest := GetProcAddress(H, '_CreateTest@0');
      if @CreateTest <> nil then
      begin
        I := CreateTest;
        if I <> nil then
          I.HelloWorld
        else
          Caption := 'nil';
      end
      else
        Caption := 'fail';
      FreeLibrary(H);
    end;//vc中的声明
    class ITest{
    public:
    virtual void __stdcall HelloWorld(){
    MessageBox(0, "Hello, World", "Test", 0);
    };
    // TODO: add your methods here.
    };extern "C" _declspec(dllexport) ITest * __stdcall CreateTest()
    {
    return new ITest();
    };// 说明:我在d5及vc6下运行正常。
    // 不过vc中创建的对象直接拿到d中使用,释放可能是个问题,
    // 我继续研究看看。
    // 你的问题很有趣,谢谢。
      

  2.   

    谢谢 westfly(西翔) ,
    你帮了我的大忙。关于释放,你可以在ITest中再增加一个函数 free(){ delete this;} 来进行释放。其实我以前在bcb6 和 vc6下实现过,但不知道delphi怎么实现,再次谢谢你。
      

  3.   

    怎么回事,westfly(西翔) ,我已经把分数给你了,怎么好象还是没到你的帐上