2
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

.NET FrameworkでCOM参照

Last updated at Posted at 2024-03-24

環境

Windows 11 Pro (64bit)

手順

dev.bat
@echo off
path %path%;C:\Windows\Microsoft.NET\Framework64\v4.0.30319
path %path%;C:\Program Files (x86)\Microsoft SDKs\Windows\v10.0A\bin\NETFX 4.8 Tools
prompt $e[33m$p$g$e[m
cmd

任意のディレクトリに上記ファイルを用意し、dev.batを実行する。

CreateInstance

speak1.cs
// csc speak1.cs
using System;
using System.Runtime.InteropServices;

class Program {
	static void Main() {
		Type t = Type.GetTypeFromProgID("SAPI.SpVoice");
		dynamic sapi = Activator.CreateInstance(t);

		sapi.Speak("hello, world");

		Marshal.ReleaseComObject(sapi);
	}
}

TlbImp

speak2.cs
/*
tlbimp C:\Windows\System32\Speech\Common\sapi.dll
csc /r:SpeechLib.dll speak2.cs
*/

class Program {
	static void Main() {
		var sapi = new SpeechLib.SpVoice();
		sapi.Speak("hello, world");
	}
}

dll / tlbのパスはレジストリ エディター(regedit)で\\HKLM\SOFTWARE\Classes\CLSID\の中からProgID(SAPI.SpVoiceなど)で検索。
warningは大量に出るが、160KBほどのSpeechLib.dllが作られる。

reg query HKLM\SOFTWARE\Classes\CLSID /f SAPI.SpVoice /s
reg query HKEY_LOCAL_MACHINE\SOFTWARE\Classes\CLSID\{96749377-3391-11D2-9EE3-00C04F797396} /s

VisualBasic

speak3.cs
// csc /r:Microsoft.VisualBasic.dll speak3.cs
using Microsoft.VisualBasic;

class Program {
	static void Main() {
		dynamic sapi = Interaction.CreateObject("SAPI.SpVoice");
		sapi.Speak("hello, world");
	}
}
speak4.vb
' vbc speak4.vb

Module Program
	Sub Main
		Dim v = CreateObject("SAPI.SpVoice")
		v.Speak("hello, world")
	End Sub
End Module
2
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?