String^ からchar*に変換するには?


超初心者  2009-04-16 19:54:56  No: 70002

環境:VC++ 2008

今、スレッドを用いて、テキストボックスに入れた*.exeファイルを
_beginthreadexの引数として渡して、呼び出し先で*.exeファイルを実行するというプログラムを作っているのですが、そのままでは引数として扱えないので、String^型の変数をchar*型に変換したいのですが、やり方がよくわかりません。


Blue  2009-04-16 20:01:41  No: 70003

http://msdn.microsoft.com/ja-jp/library/d1ae6tz5(VS.80).aspx

というかC++/CLIなのに_beginthreadexをつかうの?
スレッドクラスとか普通にあると思うけど。
http://msdn.microsoft.com/ja-jp/library/system.threading.threadstart(VS.80).aspx


επιστημη  URL  2009-04-16 20:29:39  No: 70004

> String^型の変数をchar*型に変換したいのですが
2008なら marshal_as ↓が使えるかな。
http://blogs.wankuma.com/episteme/archive/2007/05/20/77402.aspx


超初心者  2009-04-16 22:48:22  No: 70005

include ファイルを開けません。'marshal.h': No such file or directory
てエラーメッセージが出るのですけど、
marshal.hはどこに追加すればよいのですか?


επιστημη  URL  2009-04-16 23:00:18  No: 70006

↑再現できません。

以下のコードをコマンドラインでコンパイル/実行できました。

// foo.cpp
// compile: cl -clr foo.cpp 
#include <msclr/marshal.h> // char*, wchar_t*
#include <msclr/marshal_windows.h> // BSTR
#include <msclr/marshal_cppstd.h> // std::string, std::wstring
#include <iostream>

using namespace System;
using namespace std;

int main() {
  String^ s = L"alphabetやカタカナ漢字を含む文字列";
  msclr::interop::marshal_context ctx;
  const char* s2 = ctx.marshal_as<const char*>(s);
  cout << s2 << endl;
}


※返信する前に利用規約をご確認ください。

※Google reCAPTCHA認証からCloudflare Turnstile認証へ変更しました。






  このエントリーをはてなブックマークに追加