如何在dll

时间:2017-03-27 09:39:30

标签: c++ delphi dll

我正在使用Visual C ++,我想链接在Delphi上创建的dll文件。来自那里的函数参数使用回调。我正在尝试在dll函数中使用我的回调函数。

typedef void(__stdcall *Cypress_Init)(int width, int height, void(__stdcall TCypCallbackFoo)(WORD* pData, int Reason));


void (__stdcall TCypCallbackFoo)(WORD* pData, int Reason)
{
    cout << "Enter callback function" << endl;
    cout << "Reason: " << Reason << endl;
}

int main()
{   
    DWORD err;
    HINSTANCE hDll = LoadLibrary((LPCWSTR)L"c:\\Libs\\Cypress143200.dll"); // dll.dll - название подключаемой библиотеки
    if (hDll != NULL)
    {
        cout << "Library was loaded" << endl;
    }
    else
    {
        return 1;
    }

    Cypress_Init cypInit = (Cypress_Init) GetProcAddress(hDll, "Cypress_Init");
    if (cypInit != NULL)
    {
        (*cypInit)(640, 480, TCypCallbackFoo);
        cout << "good" << endl;
    }
 }

所以这个结果

>  Library was loaded
>  good

这意味着我正确使用它,但没有回调函数调用。我期待像这样的输出

 >  Library was loaded
 >  good
 >  Enter callback function
 >  Reason: 1

但这不会发生......请告诉我使用回调函数的错误?

下面delphi上dll functuon的代码标题

unit UCypA;

interface

const
  libname = 'Cypress143200.dll';

type
  TCypCallback = procedure(pData: Pointer; Reason: Integer); stdcall;

  procedure Cypress_Init(AWidth, AHeight: Integer; Callback: TCypCallback); stdcall; external libname;

implementation

end.

Delphi中的DLL使用示例(可行):

procedure CypCallback(pData: Pointer; Reason: Integer); stdcall;
begin
 case Reason of
   CYP_NEWCADR: Form1.Newcadr(pData);
   CYP_ATTACH: Form1.Caption := 'Attach';
   CYP_DETACH:Form1.Caption := 'Detach';
 end;
end;

procedure TForm1.FormCreate(Sender: TObject);
begin
 Cypress_Init(640, 480, @CypCallback);
end;

procedure TForm1.NewCadr(Data: Pointer);
var
  pw: PWordArray;
begin
  pw := PWordArray(Data);
  //work with data
end;

0 个答案:

没有答案