THRIFT-4262 Invalid binding to InterlockedCompareExchange64() with 64-bit targets Client: Delphi Patch: Jens Geyer
diff --git a/lib/delphi/src/Thrift.Utils.pas b/lib/delphi/src/Thrift.Utils.pas
index a0bf144..89d0211 100644
--- a/lib/delphi/src/Thrift.Utils.pas
+++ b/lib/delphi/src/Thrift.Utils.pas
@@ -69,8 +69,9 @@
   end;
 
 
-function InterlockedCompareExchange64( var Target : Int64; Exchange, Comparand : Int64) : Int64; stdcall;
-function InterlockedExchangeAdd64( var Addend : Int64; Value : Int64) : Int64; stdcall;
+{$IFDEF Win64}
+function InterlockedExchangeAdd64( var Addend : Int64; Value : Int64) : Int64;  
+{$ENDIF}
 
 
 implementation
@@ -223,14 +224,19 @@
 end;
 
 
-// natively available since stone age
-function InterlockedCompareExchange64;
-external KERNEL32 name 'InterlockedCompareExchange64';
+{$IFDEF Win64}
+
+function InterlockedCompareExchange64( var Target : Int64; Exchange, Comparand : Int64) : Int64;  inline;
+begin
+  {$IFDEF OLD_UNIT_NAMES}
+  result := Windows.InterlockedCompareExchange64( Target, Exchange, Comparand);
+  {$ELSE}
+  result := WinApi.Windows.InterlockedCompareExchange64( Target, Exchange, Comparand);
+  {$ENDIF}
+end;
 
 
-// natively available >= Vista
-// implemented this way since there are still some people running Windows XP :-(
-function InterlockedExchangeAdd64( var Addend : Int64; Value : Int64) : Int64; stdcall;
+function InterlockedExchangeAdd64( var Addend : Int64; Value : Int64) : Int64;
 var old : Int64;
 begin
   repeat
@@ -239,6 +245,7 @@
   result := Old;
 end;
 
+{$ENDIF}
 
 
 end.
diff --git a/lib/delphi/test/TestClient.pas b/lib/delphi/test/TestClient.pas
index a6567fa..043ff33 100644
--- a/lib/delphi/test/TestClient.pas
+++ b/lib/delphi/test/TestClient.pas
@@ -39,6 +39,7 @@
   Thrift.Transport,
   Thrift.Stream,
   Thrift.Test,
+  Thrift.Utils,
   Thrift.Collections,
   Thrift.Console;
 
@@ -88,6 +89,9 @@
     {$IFDEF StressTest}
     procedure StressTest(const client : TThriftTest.Iface);
     {$ENDIF}
+    {$IFDEF Win64}
+	procedure UseInterlockedExchangeAdd64;
+    {$ENDIF}
   protected
     procedure Execute; override;
   public
@@ -1014,6 +1018,18 @@
 end;
 
 
+{$IFDEF Win64}
+procedure TClientThread.UseInterlockedExchangeAdd64;
+var a,b : Int64;
+begin
+  a := 1;
+  b := 2;
+  Thrift.Utils.InterlockedExchangeAdd64( a,b);
+  Expect( a = 3, 'InterlockedExchangeAdd64');
+end;
+{$ENDIF}
+
+
 procedure TClientThread.JSONProtocolReadWriteTest;
 // Tests only then read/write procedures of the JSON protocol
 // All tests succeed, if we can read what we wrote before
@@ -1249,7 +1265,11 @@
 begin
   // perform all tests
   try
+    {$IFDEF Win64}  
+    UseInterlockedExchangeAdd64;
+	{$ENDIF}
     JSONProtocolReadWriteTest;
+	
     for i := 0 to FNumIteration - 1 do
     begin
       ClientTest;