I faced a similar problem when calling some AVICAP API functions. They internally use codecs installed in the system, and some codecs use MMX instructions and leave the FPU in an inconsistent state in case of an internal codec error.
I created the following solution, that worked for me:
Code: Select all
// An example of calling capGetDriverDescription API, that may call flawed codecs
try
Result:=capGetDriverDescription(DriverNumber,Name,Length(Name),Version,Length(Version));
finally
FpuInit;
end;
Code: Select all
procedure FpuInit;
{$IFOPT W+}
{$DEFINE STACKFRAMESON}
{$ENDIF}
{$W-}
{$IFDEF CPUX64}
function DefaultMXCSRAddr : Pointer; {Pascal function is to allow building with packages}
begin
Result:=@DefaultMXCSR;
end;
{$ELSE}
function Default8087CWAddr : Pointer; {Pascal function is to allow building with packages}
begin
Result:=@Default8087CW;
end;
{$ENDIF}
{$IFDEF STACKFRAMESON}
{$UNDEF STACKFRAMESON}
{$W+}
{$ENDIF}
asm
{$IFDEF CPUX64}
PUSH RAX
CALL DefaultMXCSRAddr
LDMXCSR [RAX]
POP RAX
{$ELSE}
FNINIT
FWAIT
PUSH EAX
CALL Default8087CWAddr
FLDCW [EAX]
POP EAX
{$ENDIF}
end;