[fpc-pascal] Microsoft SAPI on Freepascal

tsiegel at softcon.com tsiegel at softcon.com
Tue Jun 27 01:01:22 CEST 2023


On 6/26/2023 6:26 PM, Michael Van Canneyt via fpc-pascal wrote:
>
>
> On Mon, 26 Jun 2023, James Richters wrote:
>
>>
>> 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 suspect COM is not properly initialized, and that Lazarus does this 
> for you.
> (it needs com for some components on windows)
>
> So, try adding unit windows to the uses clause and add
>
> CoInitialize(nil);
>
> as the first statement of your program.
>
Not necessary at all.

The version I got working using just fpc (I don't use lazarus, or even a 
gui (in most cases) I've not figured out how to generate accessible guis 
from a pascal program.  When I need a GUI, I'll either use java (and 
tell it to use grid layout so I don't have to mess with placement), or 
I'll use powerbasic, and generate the GUI separate from the program 
using their tools, then just drop the code in where it's needed to make 
the GUI code work.  It uses DDT methods to generate the GUI, and that 
works just fine with screen readers.  I don't know how to do that with 
FPC, so I generally don't use it when a GUI is required.

But, back to the point.

For me, the code:


program voice;
uses
comobj;

procedure speak(s : string);

var
v : olevariant;

begin
v:=CreateOleObject('SAPI.SpVoice');
v.Speak(s);
end;

begin
speak('Hello.');
end.


It's not a whole lot different from the one posted above, but it might 
help point to the issue.

For me, the line

v.Voice:=v.GetVoices().Item(0);

(or any other number) causes a similar but different error from the one 
received from the original poster in his code, so it could be, (but 
likely isn't) related.




More information about the fpc-pascal mailing list