Posts Tagged ‘Win32 API’
-
Hello, Win32 API(Phalanger) World!
Posted on 6月 22nd, 2012 by cx20
Win32 API(Phalanger)
Win32 API は、Windows の機能にアクセスする為の API(Application Programming Interface)である。
Phalanger には Win32 API を直接呼び出す機能は実装されていないが、C# を経由することで、Win32 API を呼び出すことが可能となっている。ソースコード(C#)
using System; using System.Runtime.InteropServices; namespace Win32Lib { public class Win32 { [DllImport("user32.dll", CharSet=CharSet.Auto)] public extern static uint MessageBox(IntPtr hWnd, string lpText, string lpCaption, uint uType); } }
コンパイル方法(C#)
C:¥> csc /target:library Win32Lib.cs
ソースコード(Phalanger)
<? use System; use Win32Lib; $win32 = new Win32LibWin32(); $win32->MessageBox( new SystemIntPtr(0), "Hello, Win32 API(Phalanger) World!", "Hello, World!", 0); ?>
コンパイル方法(Phalanger)
C:¥> phpc /lang:pure /r:Win32Lib.dll Hello.php
実行結果
--------------------------- Hello, World! --------------------------- Hello, Win32 API(Phalanger) World! --------------------------- OK ---------------------------
-
Hello, Win32 API(ClojureCLR) World!
Posted on 6月 21st, 2012 by cx20
Win32 API(ClojureCLR)
Win32 API は、Windows の機能にアクセスする為の API(Application Programming Interface)である。
ClojureCLR には Win32 API を直接呼び出す機能は実装されていないが、C# を経由することで、Win32 API を呼び出すことが可能となっている。ソースコード(C#)
using System; using System.Runtime.InteropServices; namespace Win32Lib { public class Win32 { [DllImport("user32.dll", CharSet=CharSet.Auto)] public extern static uint MessageBox(IntPtr hWnd, string lpText, string lpCaption, uint uType); } }
コンパイル方法(C#)
C:¥> csc /target:library Win32Lib.cs
ソースコード(ClojureCLR)
(System.Reflection.Assembly/LoadFrom "Win32Lib.dll") (Win32Lib.Win32/MessageBox 0 "Hello, Win32 API(ClojureCLR) World!" "Hello, World!", 0 )
実行方法
C:¥> Clojure.Main hello.clj
実行結果
--------------------------- Hello, World! --------------------------- Hello, Win32 API(ClojureCLR) World! --------------------------- OK ---------------------------
-
Hello, Win32 API(IronScheme) World!
Posted on 6月 20th, 2012 by cx20
Win32 API(IronScheme)
Win32 API は、Windows の機能にアクセスする為の API(Application Programming Interface)である。
以下は IronScheme による Win32 API 呼出し例となっている。ソースコード
(import (rnrs) (ironscheme clr) ) (define msgbox (pinvoke-call user32 MessageBox int (intptr string string int))) (msgbox 0 "Hello, Win32 API(IronScheme) World!" "Hello, World!" 0 )
実行方法
C:¥> isc hello.ss
実行結果
--------------------------- Hello, World! --------------------------- Hello, Win32 API(IronScheme) World! --------------------------- OK ---------------------------
-
Hello, Win32 API(IronRuby) World!
Posted on 6月 19th, 2012 by cx20
Win32 API(IronRuby)
Win32 API は、Windows の機能にアクセスする為の API(Application Programming Interface)である。
IronRuby には Win32 API を直接呼び出す機能は実装されていないが、C# を経由することで、Win32 API を呼び出すことが可能となっている。ソースコード(C#)
using System; using System.Runtime.InteropServices; namespace Win32Lib { public class Win32 { [DllImport("user32.dll", CharSet=CharSet.Auto)] public extern static uint MessageBox(IntPtr hWnd, string lpText, string lpCaption, uint uType); } }
コンパイル方法(C#)
C:¥> csc /target:library Win32Lib.cs
ソースコード(IronRuby)
require 'System' require 'Win32Lib' include System include Win32Lib Win32.MessageBox( IntPtr.Zero, "Hello, Win32 API(IronRuby) World!", "Hello, World!", 0 )
実行方法
C:¥> ir hello.rb
実行結果
--------------------------- Hello, World! --------------------------- Hello, Win32 API(IronRuby) World! --------------------------- OK ---------------------------
-
Hello, Win32 API(IronPython) World!
Posted on 6月 18th, 2012 by cx20
Win32 API(IronPython)
Win32 API は、Windows の機能にアクセスする為の API(Application Programming Interface)である。
以下は IronPython にて clrtype ライブラリを使用した Win32 API の呼出し例である。ソースコード
import clr import clrtype import System from System.Reflection import BindingFlags class Win32(object): __metaclass__ = clrtype.ClrClass from System.Runtime.InteropServices import DllImportAttribute DllImport = clrtype.attribute(DllImportAttribute) @staticmethod @DllImport("user32.dll") @clrtype.accepts(System.IntPtr, System.String, System.String, System.UInt32) @clrtype.returns(System.Int32) def MessageBox(hwnd, text, caption, type): raise RuntimeError("Runtime Error") Win32.MessageBox(System.IntPtr.Zero, "Hello, Win32 API(IronPython) World!", "Hello, World!", 0)
実行方法
C:¥> ipy hello.py
実行結果
--------------------------- Hello, World! --------------------------- Hello, Win32 API(IronPython) World! --------------------------- OK ---------------------------
-
Hello, Win32 API(Go) World!
Posted on 5月 6th, 2012 by cx20
Win32 API(Go)
Win32 API は、Windows の機能にアクセスする為の API(Application Programming Interface)である。
以下は Go言語 にて syscall.Syscall 関数を用いた呼出し例である。ソースコード
package main import ( "syscall" "unsafe" ) var ( user32, _ = syscall.LoadLibrary("user32.dll") procMessageBoxW, _ = syscall.GetProcAddress(user32, "MessageBoxW") ) func MessageBox(hwnd uintptr, text string, caption string, style uintptr) (int32) { ret, _, _ := syscall.Syscall6( uintptr(procMessageBoxW), 4, hwnd, uintptr(unsafe.Pointer(syscall.StringToUTF16Ptr(text))), uintptr(unsafe.Pointer(syscall.StringToUTF16Ptr(caption))), style, 0, 0 ) return int32(ret) } func main() { defer syscall.FreeLibrary(user32) MessageBox( 0, "Hello, Win32 API(Go) World!", "Hello, World!", 0 ) }
Win32 データ型と Go言語 データ型の対応は主に以下のようになっている。
Win32 データ型 C/C++ データ型 Go データ型 HANDLE void * uintptr BYTE unsigned char uint8, byte SHORT short int16 WORD unsigned short uint16 INT int int32, int UINT unsigned int uint32 LONG long int32 BOOL int int DWORD unsigned long uint32 ULONG unsigned long uint32 CHAR char byte WCHAR wchar_t uint16 LPSTR char * *byte LPCSTR const char * *byte, syscall.StringByPtr() LPWSTR wchar_t * *uint16 LPCWSTR const wchar_t * *uint16, syscall.StringToUTF16Ptr() FLOAT float float32 DOUBLE double float64 LONGLONG __int64 int64 DWORD64 unsigned __int64 uint64 コンパイル方法
C:¥> SET GOROOT=C:go C:¥> go build -ldflags -Hwindowsgui hello.go
実行結果
--------------------------- Hello, World! --------------------------- Hello, Win32 API(Go) World! --------------------------- OK ---------------------------
-
Hello, Win32 API(Delphi) World!
Posted on 5月 5th, 2012 by cx20
Win32 API(Delphi)
Win32 API は、Windows の機能にアクセスする為の API(Application Programming Interface)である。
以下は Delphi からの呼出し例である。
Win32 API の参照方法としては、external 宣言を用いる方法の他、Winapi.Windows ライブラリを用いる方法などがある。ソースコード(external 宣言)
program hello; function MessageBox(hWnd: THandle; lpText: PAnsiChar; lpCaption: PAnsiChar; uType: Cardinal): Integer; stdcall; external 'user32.dll' name 'MessageBoxA'; begin MessageBox( 0, 'Hello, Win32 API(Delphi) World!', 'Hello, World!', 0 ); end.
ソースコード(Winapi.Windows ライブラリ)
program hello; uses Windows; begin MessageBox( 0, 'Hello, Win32 API(Delphi) World!', 'Hello, World!', MB_OK ); end.
Win32 データ型と Delphi データ型の対応は主に以下のようになっている。
Win32 データ型 C/C++ データ型 Delphi データ型 HANDLE void * THandle BYTE unsigned char Byte SHORT short Smallint WORD unsigned short Word INT int Integer UINT unsigned int Cardinal LONG long Integer BOOL int LongBool DWORD unsigned long LongWord ULONG unsigned long LongWord CHAR char AnsiChar WCHAR wchar_t WideChar, Char LPSTR char * PAnsiChar LPCSTR const char * PAnsiChar LPWSTR wchar_t * PWideChar, PChar LPCWSTR const wchar_t * PWideChar, PChar FLOAT float Single DOUBLE double Double LONGLONG __int64 Int64 DWORD64 unsigned __int64 UInt64 コンパイル方法(Delphi XE2)
C:¥> dcc32 hello.pas
実行結果
--------------------------- Hello, World! --------------------------- Hello, Win32 API(Delphi) World! --------------------------- OK ---------------------------
-
Hello, Win32 API(D言語) World!
Posted on 5月 4th, 2012 by cx20
Win32 API(D言語)
Win32 API は、Windows の機能にアクセスする為の API(Application Programming Interface)である。
以下は D言語 からの呼出し例である。
D言語では、Platform SDK のヘッダファイル相当のものがプロトタイプ宣言されている為、基本的な Win32 API であれば、C/C++ の場合と同様に、呼び出すことが可能となっている。ソースコード
import std.c.windows.windows; int main( char[][] args ) { MessageBoxA( null, "Hello, Win32 API(D) World!", "Hello, World!", MB_OK ); return 0; }
Win32 API のプロトタイプ宣言(参考)
extern (Windows) { : int MessageBoxA(HWND hWnd, LPCSTR lpText, LPCSTR lpCaption, UINT uType); int MessageBoxW(HWND hWnd, LPCWSTR lpText, LPCWSTR lpCaption, UINT uType); : }
また、Win32 データ型に関しても、同じ名前の型が、D言語用に定義されている為、特段、言語の差異を意識することなく使うことが可能となっている。
Win32 データ型 C/C++ データ型 D言語データ型 HANDLE void * void * BYTE unsigned char ubyte SHORT short short WORD unsigned short ushort INT int int UINT unsigned int uint LONG long int BOOL int int DWORD unsigned long uint ULONG unsigned long uint CHAR char char WCHAR wchar_t wchar LPSTR char * char * LPCSTR const char * const(char) * LPWSTR wchar_t * wchar * LPCWSTR const wchar_t * const(wchar) * FLOAT float float DOUBLE double double LONGLONG __int64 long DWORD64 unsigned __int64 ulong コンパイル方法(Digital Mars D コンパイラ)
C:¥> dmd hello.d
実行結果
--------------------------- Hello, World! --------------------------- Hello, Win32 API(D) World! --------------------------- OK ---------------------------
-
Hello, Win32 API(JNI) World!
Posted on 5月 3rd, 2012 by cx20
Win32 API(JNI)
Win32 API は、Windows の機能にアクセスする為の API(Application Programming Interface)である。
以下は JNI(Java Native Interface)による呼出し例である。
Java からは 直接、Win32 API を呼び出すことは出来ないが、JNI 用の DLL を作成することで呼び出すことが可能となっている。ソースコード(Java)
import java.io.*; public class Hello { static { System.loadLibrary("Hello"); } public native int MessageBox( int hwnd, String text, String title, int type ); public static void main(String args[]) { Hello hello = new Hello(); hello.MessageBox( 0, "Hello, Win32 API(JNI) World!", "Hello, World!", 0 ); } }
C ヘッダファイル作成
C:¥> javah -jni Hello
ソースコード(C ヘッダファイル)
/* DO NOT EDIT THIS FILE - it is machine generated */ #include <jni.h> /* Header for class Hello */ #ifndef _Included_Hello #define _Included_Hello #ifdef __cplusplus extern "C" { #endif /* * Class: Hello * Method: MessageBox * Signature: (ILjava/lang/String;Ljava/lang/String;I)I */ JNIEXPORT jint JNICALL Java_Hello_MessageBox (JNIEnv *, jobject, jint, jstring, jstring, jint); #ifdef __cplusplus } #endif #endif
ソースコード(C 実装ファイル)
#include "Hello.h" #include <windows.h> /* * Class: Hello * Method: MessageBox * Signature: (ILjava/lang/String;Ljava/lang/String;I)I */ JNIEXPORT jint JNICALL Java_Hello_MessageBox (JNIEnv* env, jobject me, jint hwnd, jstring text, jstring caption, jint type) { #ifdef UNICODE const jchar* _text = env->GetStringChars(text, 0); const jchar* _caption = env->GetStringChars(caption, 0); const int result = MessageBoxW(NULL, (LPCWSTR)_text, (LPCWSTR)_caption, type); env->ReleaseStringChars(text, _text); env->ReleaseStringChars(caption, _caption); #else /* このコードはマルチバイト文字(MBCS)を考慮していない為、全角文字列を渡した場合に文字化けする。正しくは SJIS に変換する処理が必要。*/ const char* _text = env->GetStringUTFChars(text, 0); const char* _caption = env->GetStringUTFChars(caption, 0); const int result = MessageBoxA(NULL, (LPCSTR)_text, (LPCSTR)_caption, type); env->ReleaseStringUTFChars(text, _text); env->ReleaseStringUTFChars(caption, _caption); #endif return result; }
Win32 データ型と JNI データ型の対応は主に以下のようになっている。
Win32 データ型 C/C++ データ型 JNI データ型 Java データ型 BOOL int jboolean boolean BYTE char jbyte byte WCHAR wchar_t jchar char SHORT short jshort short INT int jint int LONGLONG __int64 jlong long FLOAT float jfloat float DOUBLE double jdouble double VOID void void void LPCSTR const char * jstring string LPCWSTR const wchar_t * jstring string なお、jstring については、使用時に、正しい C/C++ データ型(ANSI, MBCS / UNICODE)に適宜、変換する必要がある。
DLL作成(Visual C++)
C:¥> SET INCLUDE=%JAVA_HOME%include;%JAVA_HOME%includewin32;%INCLUDE% C:¥> cl Hello.cpp /LD /link user32.lib
コンパイル&実行方法
C:¥> javac Hello.java C:¥> java Hello
実行結果
--------------------------- Hello, World! --------------------------- Hello, Win32 API(JNI) World! --------------------------- OK ---------------------------
-
Hello, Win32 API(JNA) World!
Posted on 5月 2nd, 2012 by cx20
Win32 API(JNA)
Win32 API は、Windows の機能にアクセスする為の API(Application Programming Interface)である。
以下は JNA(Java Native Access)による呼出し例である。ソースコード
import com.sun.jna.Native; import com.sun.jna.win32.StdCallLibrary; public class Hello { public interface User32 extends StdCallLibrary { User32 INSTANCE = (User32) Native.loadLibrary("user32", User32.class); int MessageBoxA(int hwnd, String text, String caption, int type); } public static void main(String[] args) { User32 user32 = User32.INSTANCE; user32.MessageBoxA(0, "Hello, Win32 API(JNA) World!", "Hello, World!", 0); } }
Win32 データ型と JNA データ型の対応は主に以下のようになっている。
Win32 データ型 C/C++ データ型 Java/JNA データ型 BYTE char byte SHORT short short WCHAR wchar_t char INT int int BOOL int boolean LONG long NativeLong(JNA データ型) LONGLONG __int64 long FLOAT float float DOUBLE double double LPCSTR const char * String LPCWSTR const wchar_t * WString(JNA データ型) LPINT int * IntByReference(JNA データ型) LPVOID void * Pointer コンパイル&実行方法
C:¥> javac -cp jna.jar;. Hello.java C:¥> java -cp jna.jar;. Hello
実行結果
--------------------------- Hello, World! --------------------------- Hello, Win32 API(JNA) World! --------------------------- OK ---------------------------