Home Forums Software Development using dll in Delphi, control the mouse Reply To: using dll in Delphi, control the mouse

#5256
Yvonne
Participant

Hi everyone,
I actually tried translating the C headers to Delphi, but I failed miserably…
Nontheless, it is very well possible to control the EyeX in Delphi by using a facade DLL:

  1. Make a copy of the C++ sample project that fits your requirement best. In my case I used MinimalGazeDataStream.
  2. Rename the copy of that project to something like “EyexFacade”.
  3. Open your EyexFacade project in the coding IDE of your choice (I used Visual Studio Express 2015) and configure the settings:
    • Configuration / Output Type = Dynamic Link Library (DLL)
    • Call Convention = _cdecl (important! Do NOT use stdcall!)
  4. Define the functions you need as dllexport and compile EyexFacade.dll
  5. Write your Delphi application and use EyexFacade.dll to control the EyeX

Here’s some sample code to get the gaze coordinates via callback:
========== EyexFacade ==========

typedef void (TX_CALLCONVENTION *EC_OnGazeCallback)(int gazeX, int gazeY); // type definition of the callback function
static EC_OnGazeCallback OnGazeCallback; // global variable pointing to the registered callback

// the Delphi app calls this function to register a callback
__declspec(dllexport) void SetOnGazeCallback(EC_OnGazeCallback callback) 
{
	OnGazeCallback = callback;
}

// this function should already be included in the sample project. 
void OnGazeDataEvent(TX_HANDLE hGazeDataBehavior) 
{
	TX_GAZEPOINTDATAEVENTPARAMS eventParams;
	if (txGetGazePointDataEventParams(hGazeDataBehavior, &eventParams) == TX_RESULT_OK) 
	{
		OnGazeCallback((int)eventParams.X, (int)eventParams.Y); // call Delphi callback
	}
}

========== Delphi application ==========

type
  TEyexOnGazeCallback = procedure(GazeX: Integer; GazeY: Integer); cdecl;
  PEyexOnGazeCallback = ^TEyexOnGazeCallback;
  TEyexSetCallback    = procedure(callback: PEyexOnGazeCallback); cdecl;
implementation

procedure TForm1.FormCreate(Sender: TObject);
var 
  hDLL: THandle;
  hSetCallback: TEyexSetCallback;
begin
  hDLL := LoadLibrary('EyexFacade.dll');
  hSetCallback := GetProcAddress(hDLL, PAnsiChar(AnsiString('SetOnGazeCallback')));
  hSetCallback(@OnGaze);
end;

procedure OnGaze(GazeX: Integer; GazeY: Integer);
begin
  // do something usefull ;)
end;