What's new
  • Visit Rebornbuddy
  • Visit Panda Profiles
  • Visit LLamamMagic
  • Visit Resources
  • Visit Downloads
  • Visit Portal

[Development Question] How to reference a COM object from plugin?

bugage5

New Member
Joined
Feb 8, 2011
Messages
8
Reaction score
1
Hi,

I am trying to use SAPI (Voice API by Microsoft) from plug-in. I want my plug-in to read whispers and guild chat.

How should i reference SAP's COM object to ensure it will be referenced while compiled by HB?

To reference this COM object from regular application, IDE adds to the cproj:
PHP:
  <ItemGroup>
    <COMReference Include="SpeechLib">
      <Guid>{C866CA3A-32F7-11D2-9602-00C04F8EE628}</Guid>
      <VersionMajor>5</VersionMajor>
      <VersionMinor>4</VersionMinor>
      <Lcid>0</Lcid>
      <WrapperTool>tlbimp</WrapperTool>
      <Isolated>False</Isolated>
      <EmbedInteropTypes>True</EmbedInteropTypes>
    </COMReference>
  </ItemGroup>

And from the code I use:
Code:
using SpeechLib;

Now I get:
Code:
Plugin from C:\HB\Honorbuddy 2.0.0.4053\Plugins\MyPlugin could not be compiled! Compiler errors:
File: MyPlugin .cs Line: 6 Error: The type or namespace name 'SpeechLib' could not be found (are you missing a using directive or an assembly reference?)

Guess assembly reference is missing, anybody can suggest how to reference it ? :)
 
I actually found how to do it :)

The technique called Late Binding, following code will do it:

Code:
                Type _SpVoice;
                object[] parameter = new object[2];
                object _SpVoiceObj;
                try
                {
                    _SpVoice = Type.GetTypeFromProgID("SAPI.SpVoice");
                    _SpVoiceObj = Activator.CreateInstance(_SpVoice);
                    parameter[0] = "Testing voice, 1, 2, 3, loud and clear!";
                    parameter[1] = 0;
                    _SpVoice.InvokeMember("Speak", System.Reflection.BindingFlags.InvokeMethod, null, _SpVoiceObj, parameter);
                }
                catch (Exception e)
                {
                    Console.WriteLine("Error Stack {0} ", e.Message);
                }

You need SAPI SDK or it's redistributable installed to have it working.
 
Back
Top