C++ Code:
#include <windows.h>#include <stdio.h>#define Export __declspec(dllexport)#define DelphiExport extern "C" Export#define DX_PIPE "\\\\.\\pipe\\DxInterceptPipe"DelphiExport
int GetPluginABIVersion
();DelphiExport
int GetFunctionCount
();DelphiExport
int GetFunctionInfo
(int x
, void*& address
, char*& def
);DelphiExport bool DXI_Connect
();DelphiExport
void DXI_Disconnect
();DelphiExport
int DXI_MakeCompass
(int angle
);HINSTANCE inst
= NULL
;HANDLE hPipe
= INVALID_HANDLE_VALUE
;BOOL WINAPI DllMain
(HINSTANCE hInstance
, DWORD reason
, LPVOID reserved
){ switch(reason
) { case DLL_PROCESS_ATTACH
: { inst
= hInstance
; return true; } break; case DLL_PROCESS_DETACH
: { FreeLibrary
(inst
); } break; } return false;}DelphiExport bool DXI_Connect
(){ if(hPipe
!= INVALID_HANDLE_VALUE
) return true; hPipe
= CreateFile
(DX_PIPE
, GENERIC_READ
| GENERIC_WRITE
, 0, NULL
, OPEN_EXISTING
, 0, NULL
); return (hPipe
!= INVALID_HANDLE_VALUE
);}DelphiExport
void DXI_Disconnect
(){ if(hPipe
!= INVALID_HANDLE_VALUE
) CloseHandle
(hPipe
); hPipe
= INVALID_HANDLE_VALUE
;}DelphiExport
int DXI_MakeCompass
(int angle
){ if(hPipe
== INVALID_HANDLE_VALUE
) return -1; float request
= (float)angle
; DWORD written
= 0; BOOL success
= WriteFile
(hPipe
, &request
, 4, &written
, NULL
); if(!success
|| written
!= 4) { return -1; } float receive
; DWORD read
= 0; success
= ReadFile
(hPipe
, &receive
, 4, &read
, NULL
); if(success
&& read
== 4) { return (int)receive
; } return -1;}DelphiExport
int GetPluginABIVersion
(){ return 2;}DelphiExport
int GetFunctionCount
(){ return 3; //with 1 works well, 2 or 3 makes it crash}DelphiExport
int GetFunctionInfo
(int x
, void*& address
, char*& def
){ switch(x
) { case 0: { address
= reinterpret_cast
<void*>(GetProcAddress
(inst
, "DXI_MakeCompass")); strcpy(def
, "function DXI_MakeCompass(angle: integer): integer;"); } break; case 1: { address
= reinterpret_cast
<void*>(GetProcAddress
(inst
, "DXI_Connect")); strcpy(def
, "function DXI_Connect: boolean;"); } break; case 2: { address
= reinterpret_cast
<void*>(GetProcAddress
(inst
, "DXI_Disconnect")); strcpy(def
, "procedure DXI_Disconnect();"); } break; default: { char buff
[128]; memset(buff
, 0, 128); sprintf(buff
, "Wrong function index: %i", 128); MessageBox
(NULL
, buff
, "info", MB_OK
); x
= -1; } } return x
;}