[fpc-pascal] Microsoft SAPI on Freepascal

James Richters james.richters at productionautomation.net
Mon Jun 26 19:21:41 CEST 2023


I was trying to figure this out on my own.. I went to:
https://wiki.freepascal.org/SAPI

that is where I got my advice.
I took the top example code from the link and made it into the following program:

{$Mode OBJFPC}
uses
  comobj;
var
  SavedCW: Word;
  SpVoice: Variant;
begin
  SpVoice := CreateOleObject('SAPI.SpVoice');
  // Change FPU interrupt mask to avoid SIGFPE exceptions
  SavedCW := Get8087CW;
  try
    Set8087CW(SavedCW or $4);
    SpVoice.Speak('hi', 0);
  finally
    // Restore FPU mask
    Set8087CW(SavedCW);
  end;
end.

It fails with 

Running "i:\programming\gcode\mill\sapi.exe "
An unhandled exception occurred at $004143F0:
EOleError: Variant does not reference an automation object
  $004143F0
  $0041950D
  $0040B9A1
  $00401931  main,  line 13 of I:/Programming/Gcode/Mill/SAPI.pas

I don't have Lazarus,  just FPC.   I have been going nuts trying to track down this unhandled exception.

I tried to make your example into a FPC program without Lazarus:
{$mode objfpc}{$H+}
uses
  comobj;
var
  SavedCW: Word;
  v: OleVariant;
begin
   v:=CreateOleObject('SAPI.SpVoice');
   v.Speak('Hello');
End.
I get:
Running "i:\programming\gcode\mill\sapi.exe "
An unhandled exception occurred at $00414370:
EOleError: Variant does not reference an automation object
  $00414370
  $0041948D
  $0040B941
  $004018DF  main,  line 9 of i:/programming/gcode/mill/sapi.pas

I have tried to trace through with F7  I get a message that "Program generated a signal 291... " and then the IDE terminates

I still do not have an example I can compile and run from the FPC IDE (without Lazarus)

>I don't know what people advised you, but what you want to do needs 3 lines of code only:



More information about the fpc-pascal mailing list