• Visit Rebornbuddy
  • Visit Panda Profiles
  • Visit LLamamMagic
  • [Development Question] How to reference a COM object from plugin?

    Discussion in 'Archives' started by bugage5, Feb 14, 2011.

    1. bugage5

      bugage5 New Member

      Joined:
      Feb 8, 2011
      Messages:
      8
      Likes Received:
      1
      Trophy Points:
      0
      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 ? :)
       
    2. Possien

      Possien Member

      Joined:
      Feb 24, 2010
      Messages:
      173
      Likes Received:
      17
      Trophy Points:
      18
      Try System.Speech
       
    3. bugage5

      bugage5 New Member

      Joined:
      Feb 8, 2011
      Messages:
      8
      Likes Received:
      1
      Trophy Points:
      0
      Same error now on System.Speech
       
    4. bugage5

      bugage5 New Member

      Joined:
      Feb 8, 2011
      Messages:
      8
      Likes Received:
      1
      Trophy Points:
      0
      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.
       

    Share This Page