{"id":119,"date":"2006-07-16T13:49:43","date_gmt":"2006-07-16T04:49:43","guid":{"rendered":"http:\/\/www.gesource.jp\/weblog\/?p=119"},"modified":"2017-07-17T00:37:20","modified_gmt":"2017-07-16T15:37:20","slug":"bds2006cbuilderruby","status":"publish","type":"post","link":"https:\/\/www.gesource.jp\/weblog\/?p=119","title":{"rendered":"BDS2006(C++Builder)\u304b\u3089Ruby\u3092\u4f7f\u3046"},"content":{"rendered":"<p>Borland Developer Studio 2006(C++Builder)\u304b\u3089Ruby\u3092\u4f7f\u3063\u3066\u307f\u307e\u3059\u3002<\/p>\n<p><a href=\"http:\/\/d.hatena.ne.jp\/koto_ne\/20060705\">\u7b8f\u97f3\u306e\u65e5\u8a18 &#8211; C++\u3068Ruby<\/a> \u3084 <a href=\"http:\/\/beyond.cocolog-nifty.com\/akutoku\/cat2844022\/index.html\">C#\u304b\u3089Ruby\u306e\u30b3\u30fc\u30c9\u3092\u5b9f\u884c\u3059\u308b\u30c6\u30b9\u30c8<\/a> \u3092\u53c2\u8003\u306b\u3057\u307e\u3057\u305f\u3002<\/p>\n<p>Ruby\u3092\u30b3\u30f3\u30d1\u30a4\u30eb\u3059\u308b\u624b\u9593\u3092\u7701\u304f\u305f\u3081\u306b\u3001<a href=\"http:\/\/www.garbagecollect.jp\/ruby\/mswin32\/ja\/download\/release.html\">Ruby-mswin32<\/a> \u304b\u3089 \u6700\u65b0\u30ea\u30ea\u30fc\u30b9\u7248\u3067\u3042\u308b ruby-1.8.4-i386-mswin32.zip \u3092\u30c0\u30a6\u30f3\u30ed\u30fc\u30c9\u3057\u307e\u3059\u3002<\/p>\n<p>\u30c0\u30a6\u30f3\u30ed\u30fc\u30c9\u3057\u305f ruby-1.8.4-i386-mswin32.zip \u3092\u5c55\u958b\u3057\u3001bin\\msvcrt-ruby18.dll \u3092C++Builder\u306e\u30d7\u30ed\u30b8\u30a7\u30af\u30c8\u3092\u4f5c\u6210\u3059\u308b\u30c7\u30a3\u30ec\u30af\u30c8\u30ea\u306b\u30b3\u30d4\u30fc\u3057\u307e\u3059\u3002<\/p>\n<p>C++Builder\u3092\u8d77\u52d5\u3057\u3001VCL\u30d5\u30a9\u30fc\u30e0\u30a2\u30d7\u30ea\u30b1\u30fc\u30b7\u30e7\u30f3\u3092\u4f5c\u6210\u3057\u307e\u3059\u3002<\/p>\n<p>\u307e\u305a\u3001\u8d77\u52d5\u6642\u306bDLL\u3092\u8aad\u307f\u8fbc\u307f\u3001\u7d42\u4e86\u6642\u306b\u89e3\u653e\u3057\u307e\u3059\u3002<\/p>\n<p>Unit1.h\u306b\u6b21\u306e\u30b3\u30fc\u30c9\u3092\u8ffd\u52a0\u3002<\/p>\n<pre><code>private:\n  HINSTANCE hDll;\n<\/code><\/pre>\n<p>Unit1.cpp\u306b\u6b21\u306e\u30b3\u30fc\u30c9\u3092\u8ffd\u52a0\u3002<\/p>\n<pre><code>__fastcall TForm1::TForm1(TComponent* Owner)\n    : TForm(Owner)\n{\n  hDll = LoadLibrary(\"msvcrt-ruby18.dll\");\n}\n\/\/---------------------------------------------------------------------------\nvoid __fastcall TForm1::FormClose(TObject *Sender, TCloseAction &amp;Action)\n{\n  if (hDll) FreeLibrary(hDll);\n}\n<\/code><\/pre>\n<p>DLL\u304c\u6b63\u3057\u304f\u8aad\u307f\u8fbc\u307e\u308c\u3066\u3044\u308b\u3053\u3068\u3092\u78ba\u8a8d\u3057\u307e\u3059\u3002<\/p>\n<p>\u6b21\u306f\u3001Ruby\u30d7\u30ed\u30b0\u30e9\u30e0\u3092\u5b9f\u884c\u3057\u3066\u307f\u307e\u3059\u3002<\/p>\n<p>Unit1.h\u306b\u6b21\u306e\u30b3\u30fc\u30c9\u3092\u8ffd\u52a0\u3002<\/p>\n<pre><code>private:\n  __declspec(dllexport) void (*ruby_init)(void);\n  __declspec(dllexport) unsigned long (*rb_eval_string)(const char*);\n  __declspec(dllexport) int (*ruby_cleanup)(int);\n<\/code><\/pre>\n<p>Unit1.cpp\u306b\u6b21\u306e\u30b3\u30fc\u30c9\u3092\u8ffd\u52a0\u3002<\/p>\n<pre><code>__fastcall TForm1::TForm1(TComponent* Owner)\n    : TForm(Owner)\n{\n  hDll = LoadLibrary(\"msvcrt-ruby18.dll\");\n  if (hDll)\n  {\n    ruby_init = (void (*)(void))GetProcAddress(hDll,\"ruby_init\");\n    rb_eval_string = (unsigned long (*)(const char*))GetProcAddress(hDll,\"rb_eval_string\");\n    ruby_cleanup = (int (*)(int))GetProcAddress(hDll,\"ruby_cleanup\");\n  }\n}\n<\/code><\/pre>\n<p>\u30d5\u30a9\u30fc\u30e0\u306b\u30dc\u30bf\u30f3\u3092\u8ffd\u52a0\u3057\u3001\u30dc\u30bf\u30f3\u3092\u62bc\u3057\u305f\u3068\u304d\u306e\u30a4\u30d9\u30f3\u30c8\u3092\u8a18\u8ff0\u3057\u307e\u3059\u3002<\/p>\n<pre><code>\/\/---------------------------------------------------------------------------\nvoid __fastcall TForm1::Button1Click(TObject *Sender)\n{\n  if (!hDll)\n  {\n    Application-&gt;MessageBox(\"msvcrt-ruby18.dll \u304c\u898b\u3064\u304b\u308a\u307e\u305b\u3093\u3067\u3057\u305f\u3002\",\n                            \"DLL \u30a8\u30e9\u30fc\",\n                            MB_ICONSTOP | MB_OK);\n    return;\n  }\n  \/\/Ruby\u30a4\u30f3\u30bf\u30d7\u30ea\u30bf\u306e\u521d\u671f\u5316\n  ruby_init();\n  \/\/\u30b9\u30af\u30ea\u30d7\u30c8\u306e\u5b9f\u884c\n  rb_eval_string(\"File.open('a.txt', 'w' ) { |f| f.puts 'success' }\");\n  \/\/Ruby\u30a4\u30f3\u30bf\u30d7\u30ea\u30bf\u306e\u30af\u30ea\u30fc\u30f3\u30a2\u30c3\u30d7\n  ruby_cleanup(0);\n}\n<\/code><\/pre>\n<p>\u5b9f\u884c\u3057\u3066\u307f\u307e\u3057\u3087\u3046\u3002<br \/>\n\u30dc\u30bf\u30f3\u3092\u62bc\u3059\u3068success\u3068\u8a18\u8ff0\u3055\u308c\u305fa.txt\u304c\u4f5c\u6210\u3055\u308c\u308b\u3068\u6210\u529f\u3002<\/p>\n<p>\u6b21\u306b\u3001\u30b9\u30af\u30ea\u30d7\u30c8\u3092\u30d5\u30a1\u30a4\u30eb\u304b\u3089\u8aad\u307f\u8fbc\u3093\u3067\u5b9f\u884c\u3057\u307e\u3059\u3002<\/p>\n<p>Unit1.h\u306b\u6b21\u306e\u30b3\u30fc\u30c9\u3092\u8ffd\u52a0\u3002<\/p>\n<pre><code>private:\n  __declspec(dllexport) void (*ruby_init_loadpath)(void);\n  __declspec(dllexport) void (*rb_load)(unsigned long, int);\n  __declspec(dllexport) unsigned long (*rb_str_new2)(const char*);\n<\/code><\/pre>\n<p>Unit1.cpp\u306b\u6b21\u306e\u30b3\u30fc\u30c9\u3092\u8ffd\u52a0\u3002<\/p>\n<pre><code>__fastcall TForm1::TForm1(TComponent* Owner)\n  : TForm(Owner)\n{\n  hDll = LoadLibrary(\"msvcrt-ruby18.dll\");\n  (\u4e2d\u7565)\n  ruby_init_loadpath = (void (*)(void))GetProcAddress(hDll,\"ruby_init_loadpath\");\n  rb_load = (void (*)(unsigned long, int))GetProcAddress(hDll,\"rb_load\");\n  rb_str_new2 = (unsigned long (*)(const char*))GetProcAddress(hDll,\"rb_str_new2\");\n<\/code><\/pre>\n<p>\u30d5\u30a9\u30fc\u30e0\u306b\u3082\u3046\u4e00\u3064\u30dc\u30bf\u30f3\u3092\u4f5c\u6210\u3057\u3001\u30dc\u30bf\u30f3\u3092\u62bc\u3057\u305f\u3068\u304d\u306e\u30a4\u30d9\u30f3\u30c8\u3092\u8a18\u8ff0\u3057\u307e\u3059\u3002<\/p>\n<pre><code>\/\/---------------------------------------------------------------------------\nvoid __fastcall TForm1::Button2Click(TObject *Sender)\n{\n  if (!hDll)\n  {\n    Application-&gt;MessageBox(\"msvcrt-ruby18.dll \u304c\u898b\u3064\u304b\u308a\u307e\u305b\u3093\u3067\u3057\u305f\u3002\",\n                            \"DLL \u30a8\u30e9\u30fc\",\n                            MB_ICONSTOP | MB_OK);\n    return;\n  }\n\n  \/\/Ruby\u30a4\u30f3\u30bf\u30d7\u30ea\u30bf\u306e\u521d\u671f\u5316\n  ruby_init();\n  ruby_init_loadpath();\n  \/\/ \u30b9\u30af\u30ea\u30d7\u30c8\u3092\u30d5\u30a1\u30a4\u30eb\u304b\u3089\u8aad\u307f\u8fbc\u3093\u3067\u5b9f\u884c\n  rb_load(rb_str_new2(\"test.rb\"), 0);\n  \/\/Ruby\u30a4\u30f3\u30bf\u30d7\u30ea\u30bf\u306e\u30af\u30ea\u30fc\u30f3\u30a2\u30c3\u30d7\n  ruby_cleanup(0);\n}\n<\/code><\/pre>\n<p>test.rb\u30d5\u30a1\u30a4\u30eb\u306f\u3053\u3046\u66f8\u3044\u3066\u307f\u307e\u3057\u305f\u3002<\/p>\n<pre><code>File.open('b.txt', 'w' ) { |f|\n  f.puts 'success'\n}\n<\/code><\/pre>\n<p>\u5b9f\u884c\u3057\u3066\u307f\u307e\u3057\u3087\u3046\u3002<br \/>\n\u30dc\u30bf\u30f3\u3092\u62bc\u3059\u3068success\u3068\u8a18\u8ff0\u3055\u308c\u305fb.txt\u304c\u4f5c\u6210\u3055\u308c\u308b\u3068\u6210\u529f\u3002<\/p>\n<p><a href=\"http:\/\/www.gesource.jp\/weblog\/archives\/2006\/08\/bds2006cbuilderruby2.html\">\u7d9a\u304f<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Borland Developer Studio 2006(C++Builder)\u304b\u3089Ruby\u3092\u4f7f\u3063\u3066\u307f\u307e\u3059\u3002 \u7b8f\u97f3\u306e\u65e5\u8a18 &#8211; C++\u3068Ruby \u3084 C#\u304b\u3089Ruby\u306e\u30b3\u30fc\u30c9\u3092\u5b9f\u884c\u3059\u308b\u30c6\u30b9\u30c8 \u3092\u53c2\u8003\u306b\u3057\u307e\u3057\u305f &#8230;<\/p>\n<p><a href=\"https:\/\/www.gesource.jp\/weblog\/?p=119\" class=\"more-link\">Continue reading &lsquo;BDS2006(C++Builder)\u304b\u3089Ruby\u3092\u4f7f\u3046&rsquo; &raquo;<\/a><\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":false,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2}},"categories":[16,5],"tags":[149,142],"class_list":["post-119","post","type-post","status-publish","format-standard","hentry","category-cbuilder","category-ruby","tag-cbuilder","tag-ruby"],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/www.gesource.jp\/weblog\/index.php?rest_route=\/wp\/v2\/posts\/119","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.gesource.jp\/weblog\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.gesource.jp\/weblog\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.gesource.jp\/weblog\/index.php?rest_route=\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.gesource.jp\/weblog\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=119"}],"version-history":[{"count":0,"href":"https:\/\/www.gesource.jp\/weblog\/index.php?rest_route=\/wp\/v2\/posts\/119\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.gesource.jp\/weblog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=119"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.gesource.jp\/weblog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=119"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.gesource.jp\/weblog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=119"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}