指针。将C ++函数转换为Delphi

时间:2015-11-24 06:36:57

标签: c++ delphi dll

亲爱的同事们,美好的一天! 我正试图通过DLL与某些设备通信。 供应商给了我用C ++编写的示例。 现在我正在尝试将其转换为Delphi。

C ++代码

void CTesaBPXLibraryExampleDlg::OnBnClickedButton1()
{
// Initialization library. A callback must be provided to receive the notification
// from the libray (e.g. new base found, new probe found, etc..)
if(!TUL_Init(manageComm_Communication, strConfigFile))
{
    Log(_T("---> ERROR OPENING USER LIBRARY <---"));
    return;
}
Log(_T("---> USER LIBRARY CONNECTED <---"));
Log(_T("..."));

// Optional: define the mode (Wireless Probe or Wireless Cap) to be used with TWIN bases
// Note: the mode of a TWIN base can be changed at any time.
//       It is possible to have a setup made of both types at the same time
VERIFY(TUL_BPWXSetDefaultTwinMode(TUL_BASE_TWIN_CAP)); // TUL_BASE_UNKNOWN or TUL_BASE_TWIN_PROBE or TUL_BASE_TWIN_CAP

// Start seeking new bases connected by the USB ports of this computer
VERIFY(TUL_SearchBaseResume());
}

DLL C ++函数

TUL_API BOOL WINAPI TUL_Init(CommunicationEvent* pEventCallback, LPCWSTR szConfigFile);
TUL_API BOOL WINAPI TUL_BPWXSetDefaultTwinMode(INT32 nBaseType);
TUL_API BOOL WINAPI TUL_SearchBaseResume();

С++函数manageComm_Communication和manageComm_Protocol

void WINAPI manageComm_Communication(int Event, int Count, LPCTSTR szValues[])
{
// Transfer values to a container
CStringArray* pStrValues = new CStringArray();
pStrValues->SetSize(Count);
for(int i = 0; i < Count; ++i)
{
    pStrValues->SetAt(i, szValues[i]);
}

// Forward event to the main GUI thread
// Note: the caller thread should not be blocked to long, therefore we use a asynchronous call
pThis->PostMessage(WM_LIBRARY_EVENT, (WPARAM)Event, (LPARAM)pStrValues);
}

void WINAPI manageComm_Protocol(const SYSTEMTIME* pSystemTime, LPCWSTR szSerial, LPCWSTR szValue)
{
// Transfer values to a container
SYSTEMTIME* pNewSystemTime = new SYSTEMTIME();
memcpy(pNewSystemTime, pSystemTime, sizeof(SYSTEMTIME));

// Transfer string values to a container
CStringArray* pStrValues = new CStringArray();
pStrValues->Add(szSerial);
pStrValues->Add(szValue);

// Forward event to the main GUI thread
// Note: the caller thread should not be blocked to long, therefore we use a asynchronous call
pThis->PostMessage(WM_LIBRARY_PROTOCOL_EVENT, (WPARAM)pNewSystemTime, (LPARAM)pStrValues);
}

转换为Delphi

type
  TTUL_Init = function (CommunicationEventDelegate:Pointer; szConfigFile:string): BOOL;
  TTUL_BPWXSetDefaultTwinMode = function (nBaseType:Int32): BOOL;
  TTUL_SearchBaseResume = function ():BOOL;

procedure manageComm_Communication(Event, Count: int32;
szValues: LPCTSTR);
var
  e,c:Integer;
  sz:LPCTSTR;
begin
  e:=Event;
  c:=Count;
  sz:=szValues;
end;

procedure manageComm_Protocol(st:TSystemTime; szSerial:LPCWSTR; szValue:LPCWSTR);
var
  tt:TSystemTime;
  szs,szv:LPCWSTR;
begin
  tt:=st;
  szs:=szSerial;
  szv:=szValue;
end;

procedure TForm1.btn1Click(Sender: TObject);
var
  r, br:BOOL;
  hndDLLHandle: THandle;
  tulinit:TTUL_Init;
  tulsbr:TTUL_SearchBaseResume;
  tullp:TTUL_LogProtocol;
  TULBPM:TTUL_BPWXSetDefaultTwinMode;
begin
try
   hndDLLHandle := loadLibrary ( 'TESABPXLibrary.dll' );
   if hndDLLHandle <> 0 then begin
      @tulinit := getProcAddress ( hndDLLHandle, '_TUL_Init@8' );
      if addr (tulinit) <> nil then begin
         r:=tulinit(@manageComm_Communication1,'probes.cfg');
      end 
      else
         showMessage ( 'Function tulinit not exists...' );

      @tullp := getProcAddress ( hndDLLHandle, '_TUL_LogProtocol@4' );
      if addr (tullp) <> nil then
        begin
           r:=tullp(@manageComm_Protocol);
        end
      else
         showMessage ( 'Function tullp not exists...' );

      @TULBPM := getProcAddress ( hndDLLHandle, '_TUL_BPWXSetDefaultTwinMode@4' );
      if addr (TULBPM) <> nil then
        begin
           r:=TULBPM(3);
        end
      else
         showMessage ( 'Function TULBPM not exists...' );

      @tulsbr := getProcAddress ( hndDLLHandle, '_TUL_SearchBaseResume@0' );
      if addr (tulsbr) <> nil then
        begin
           r:=tulsbr();
        end
      else
         showMessage ( 'Function tulsbr not exists...' );

   end 
   else
      showMessage ( 'DLL not found...' );
finally
   freeLibrary ( hndDLLHandle );
end;
end;

在按钮上单击DLL已成功加载,函数正在执行,但没有调用manageComm_Communication和manageComm_Protocol过程。 由于我糟糕的C ++经验,我不明白出了什么问题。

谢谢。

0 个答案:

没有答案