Archive for 3月 16th, 2013
-
Hello, COM(Objective-C) World!
Posted on 3月 16th, 2013 by cx20
COM(Objective-C)
COM(Component Object Model)はマイクロソフトの提唱するプログラム部品の仕様である。
COM を用いて開発された部品であれば言語を問わず利用することができる。
以下は MinGW 版 Objective-C による COM クライアントの例となっている。ソースコード
#import <windows.h> #import <ole2.h> #import <objc/Object.h> @interface Hello : Object - (void) sayHello; @end @implementation Hello - (void) sayHello { CLSID clsid; IDispatch* pShell; IDispatch* pFolder; DISPID dispid; OLECHAR* ptName = L"BrowseForFolder"; DISPPARAMS param = { NULL, NULL, 0, 0 }; VARIANT varg[4]; VARIANT vResult; CoInitialize( NULL ); CLSIDFromProgID(L"Shell.Application", &clsid ); CoCreateInstance( &clsid, NULL, CLSCTX_INPROC_SERVER, &IID_IDispatch, (void**)&pShell); pShell->lpVtbl->GetIDsOfNames( (void*)pShell, &IID_NULL, &ptName, 1, GetUserDefaultLCID(), &dispid ); VariantInit( &varg[0] ); varg[0].vt = VT_I4; varg[0].lVal = 36L; /* ssfWINDOWS */ VariantInit( &varg[1] ); varg[1].vt = VT_I4; varg[1].lVal = 0L; VariantInit( &varg[2] ); varg[2].vt = VT_BSTR; varg[2].bstrVal = SysAllocString(L"Hello, COM World!"); VariantInit( &varg[3] ); varg[3].vt = VT_I4; varg[3].lVal = 0L; param.cArgs = 4; param.rgvarg = varg; pShell->lpVtbl->Invoke( (void*)pShell, dispid, &IID_NULL, GetUserDefaultLCID(), DISPATCH_METHOD, ¶m, &vResult, NULL, NULL ); VariantInit( &varg[0] ); VariantInit( &varg[1] ); VariantInit( &varg[2] ); VariantInit( &varg[3] ); pFolder = V_DISPATCH( &vResult ); if ( pFolder != NULL ) { pFolder->lpVtbl->Release( (void*)pFolder ); } pShell->lpVtbl->Release( (void*)pShell ); CoUninitialize(); } @end int main(int argc, char *argv[]) { id obj = [ Hello alloc ]; [ obj sayHello ]; return 0; }
実行方法
C:¥> gcc -o hello hello.m -l objc -l ole32 -l oleaut32 -l uuid
実行結果
+----------------------------------------+ |Browse For Folder [X]| +----------------------------------------+ | Hello, COM(Objective-C) Wolrd! | | | | +------------------------------------+ | | |[Windows] | | | | +[addins] | | | | +[AppCompat] | | | | +[AppPatch] | | | | +[assembly] | | | | : | | | | : | | | | : | | | +------------------------------------+ | | [Make New Folder] [ OK ] [Cancel] | +----------------------------------------+