1
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?

各種言語でCOMオブジェクトを使ってみた

Last updated at Posted at 2024-03-20

Windows Script Host

JScript

speak.js
// cscript speak.js
var sapi = WScript.CreateObject("SAPI.SpVoice");
sapi.Speak("hello, world");

VBScript

speak.vbs
' cscript speak.vbs
Set sapi = CreateObject("SAPI.SpVoice")
sapi.Speak "hello, world"

PowerShell

speak.ps1
# powershell -ExecutionPolicy RemoteSigned ./speak.ps1
$sapi = New-Object -ComObject "SAPI.Spvoice"
[void]$sapi.Speak("hello, world")

.NET Framework

dev.bat
@echo off
path %path%;C:\Windows\Microsoft.NET\Framework64\v4.0.30319
prompt $e[33m$p$g$e[m
cmd

C#

speak.cs
// csc speak.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);
	}
}

Visual Basic

speak.vb
' vbc speak.vb

Module Program
	Sub Main
		Dim sapi As Object
		sapi = CreateObject("SAPI.SpVoice")
		sapi.Speak("hello, world")
	End Sub
End Module

Visual Studio

C++

speak.cpp
// cl /MD /W4 speak.cpp

#include <atlbase.h>

int main(void)
{
	HRESULT hr;
	hr = CoInitialize(nullptr);

	CComPtr<IDispatch> pDisp;
	hr = pDisp.CoCreateInstance(L"SAPI.SpVoice");

	CComVariant varg[2];
	varg[0] = 0;
	varg[1] = L"hello, world";

	CComVariant vResult;
	pDisp.InvokeN(L"Speak", varg, 2, &vResult);

	pDisp = nullptr;	// 無いとプロセスが残る
	CoUninitialize();
}

LibreOffice Basic

Sub Main
	sapi = CreateObject("SAPI.SpVoice")
	sapi.Speak "hello, world"
End Sub
1
1
1

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
1
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?