LoginSignup
2
0

各種言語でWindows APIを使ってみた

Last updated at Posted at 2024-03-21

PowerShell

msgbox.ps1
# powershell -ExecutionPolicy RemoteSigned ./msgbox.ps1

$Signature = @"
[DllImport("user32")]
public static extern int MessageBox(
	IntPtr hWnd, string lpText, string lpCaption, uint uType);
"@

$addTypeSplat = @{
	MemberDefinition = $Signature
	Name = "hoge"
	PassThru = $true
}
$user32 = Add-Type @addTypeSplat

[void]$user32::MessageBox(0, "hello, world", "PowerShell", 0)
msgbox2.ps1
# powershell -ExecutionPolicy RemoteSigned .\msgbox2.ps1

$Signature = @'
[DllImport("user32")]
public static extern int MessageBox(
	IntPtr hWnd, string lpText, string lpCaption, uint uType);
'@
Add-Type -MemberDefinition $Signature -Name bar -Namespace foo

[void][foo.bar]::MessageBox(0, "hello, world", "PowerShell", 0)

.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#

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

class Program {
	[DllImport("user32")]
	static extern int MessageBox(
		IntPtr hWnd,
		string lpText,
		string lpCaption,
		uint uType);
  
	static void Main() {
		MessageBox(IntPtr.Zero, "hello, world", "C#", 0);
	}
}

Visual Basic

msgbox.vb
' vbc msgbox.vb

Module Program
	Declare Auto Function MessageBox Lib "user32" (
		ByVal hWnd As Integer,
		ByVal lpText As String,
		ByVal lpCaption As String,
		ByVal uType As Integer) As Integer

	Sub Main
		MessageBox(0, "hello, world", "VB.NET", 0)
	End Sub
End Module

Visual Studio

C++

msgbox.c
// cl /MD /W4 msgbox.c
#pragma comment(lib, "user32")

#include <windows.h>

int main(void)
{
	MessageBoxW(NULL, L"hello, world", L"VC++", MB_OK);
}

MASM

dev64a.bat
@echo off
set vcdir=C:\Program Files\Microsoft Visual Studio\2022\Community\VC\Tools\MSVC\14.39.33519
path %path%;%vcdir%\bin\Hostx64\x64
set lib=%lib%;C:\Program Files (x86)\Windows Kits\10\Lib\10.0.22000.0\um\x64
prompt $e[33m$p$g$e[m
cmd
msbbox.asm
comment *
ml64 /c /Fl msgbox.asm
link /subsystem:console /entry:start msgbox
*
includelib kernel32
includelib user32

extrn	ExitProcess:proc
extrn	MessageBoxA:proc

.const
$capt	db	'MASM (x64)', 0
$text	db	'hello, world', 0

.code
start proc
	sub	rsp, 40
	xor	r9d, r9d
	lea	r8, offset $capt
	lea	rdx, offset $text
	xor	ecx, ecx
	call	MessageBoxA
	mov	ecx, 42
	call	ExitProcess
;	add	rsp, 40
start endp
end

LibreOffice Basic

LibreOffice 7.6 (x86_64)ではハンドルやアドレスは使えない模様。

Declare PtrSafe Function Beep Lib "kernel32" ( _
	ByVal dwFreq As Integer, _
	ByVal dwDuration As Integer) As Integer

Sub Main
	Beep 440, 800
End Sub

Python

LibreOfficeをインストールすると付属するPythonを使用。

dev.bat
@echo off
path C:\Program Files\LibreOffice\program;%path%
prompt $e[33m$p$g$e[m
cmd
msgbox.py
# python msgbox.py
import ctypes

user32 = ctypes.windll.user32
user32.MessageBoxW(None, "hello, world", "Python", 0)
2
0
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
2
0