C++/CLIのFormでの開発なのですが、function(char *str)に起動時の引数を渡したいと考えたのですが、array<System::String ^>をchar型に変える方法が分からず、苦戦しております。
どうやったら良いのでしょうか?
エラーは次のように出ます。
CouponPrint.cpp(22): error C2664: 'PtrToStringChars' : 1 番目の引数を 'cli::array<Type> ^' から '__const_String_handle' に変換できません。(新しい機能 ; ヘルプを参照)
with
[
Type=System::String ^
]
この変換を実行可能なユーザー定義変換演算子がないか、または演算子を呼び出せません。
以下、そのときのソースコード
int main(array<System::String ^> ^args)
{
// コントロールが作成される前に、Windows XP ビジュアル効果を有効にします
Application::EnableVisualStyles();
Application::SetCompatibleTextRenderingDefault(false);
if(!args)
{
size_t convertedChars = 0;
size_t sizeInBytes = ((args->Length + 1) * 2);
const wchar_t *wch = PtrToStringChars(args);
char *ch = (char *)malloc(sizeInBytes);
errno_t err = 0;
err = wcstombs_s(&convertedChars,
ch, sizeInBytes,
wch, sizeInBytes);
if (err != 0)
printf_s("wcstombs_s failed! \n");
function(ch);
free(ch);
}
// メイン ウィンドウを作成して、実行します
Application::Run(gcnew Form1());
return 0;
}
No.5ベストアンサー
- 回答日時:
> marshal_context ^ context = gcnew marshal_context();
marshal_contextをgcnewする必要はなさそう。
#include <msclr/marshal.h>
#include <cstdio>
#include <cstring>
using namespace System;
using namespace msclr::interop;
void a(const char* s) {
std::printf( "%s(%d)%p\n", s, std::strlen(s), s );
}
int main(array<String^>^ args) {
marshal_context ctx;
for each (String^ s in args ) {
a(ctx.marshal_as<const char*>(s));
}
}
No.4
- 回答日時:
C++/CLIのコードって書いたことないけど、Visual Studio 2008とMSDN、googleの力を借りて書いてみたり:
/* http://support.microsoft.com/kb/311259/ja */
#include <msclr/marshal.h>
#include <stdio.h>
#include <string.h>
using namespace System;
using namespace msclr::interop;
void a( const char* s ){ printf( "%s(%d)%p\n", s, strlen(s), s ); }
int main(array<System::String ^> ^args){
marshal_context ^ context = gcnew marshal_context();
for each ( System::String ^s in args ){ a( context->marshal_as<const char*>( s ) ); }
delete context;
}
以上、1時間かかったり。
No.3
- 回答日時:
お探しのQ&Aが見つからない時は、教えて!gooで質問しましょう!
似たような質問が見つかりました
- C言語・C++・C# const char** p;のとき、free(p)でC4090エラーとなるのはなぜですか 3 2023/03/31 16:28
- C言語・C++・C# プログラミングの授業の課題です 1 2023/01/17 22:15
- Java java 入力 3 4 3 出力 ABC DEFG HIJ このようなプログラムの書き方を教えてくだ 2 2022/07/15 14:18
- C言語・C++・C# 宣言する関数の形が決まっている状態で、 str1とstr2の文字列をこの順に引っ付けてstrに保存し 2 2022/05/30 18:21
- C言語・C++・C# C++のcase文の書き方 4 2023/02/24 20:50
- C言語・C++・C# c言語の問題です 課題1 (二分探索木とセット) 大きさ size の配列 array を考える。す 2 2023/01/10 21:08
- C言語・C++・C# C++のcinの動作 5 2023/02/26 00:13
- C言語・C++・C# c言語の問題の説明、各所ごとに 5 2023/07/26 11:03
- C言語・C++・C# C++プログラミングコードにポリモーフィズムを取り入れ方を教えてください。 2 2023/06/09 11:17
- Visual Basic(VBA) 別シートから年齢別の件数をカウントしたいの続き 5 2023/01/24 00:16
関連するカテゴリからQ&Aを探す
おすすめ情報
デイリーランキングこのカテゴリの人気デイリーQ&Aランキング
-
fgetsなどのときのstdinのバッ...
-
【C言語】テキストファイル内の...
-
CStringをwchar_tに変換したい
-
c++ 文字列を入力して、一文字...
-
文字列から空白を取り除きたい...
-
配列をnビットシフトする
-
str系関数を使わずに二つの文字...
-
charからLPTSTRへの変換方法
-
charでの計算?
-
strtok関数 自作
-
C言語のポインターで詰まっている
-
【C言語】文字型と整数型の違い
-
C言語のfor文です。 繰り返しの...
-
ファイル名である文字列からbas...
-
c言語の文字列の逆順のプログラ...
-
ネットワークにつながっている...
-
enumの記述について。
-
char型からのバイト数取得
-
C言語プログラミングについて(...
-
ヘキサ表現が上手く行かない
マンスリーランキングこのカテゴリの人気マンスリーQ&Aランキング
-
fgetsなどのときのstdinのバッ...
-
charでの計算?
-
C言語のfor文です。 繰り返しの...
-
charからLPTSTRへの変換方法
-
文字列から空白を取り除きたい...
-
C言語の入力した文字を反転させ...
-
'const char *' 型は 'char *' ...
-
配列をnビットシフトする
-
str系関数を使わずに二つの文字...
-
int main()の・・・
-
atoi( ) の反対をやりたい
-
c++ 文字列を入力して、一文字...
-
CStringをwchar_tに変換したい
-
switch文で文字を比較すること...
-
干支のプログラム
-
絶対パスからのファイル名の切...
-
3桁区切(コンマ)記号をつけ...
-
間接操作のレベルとは
-
間接参照のレベルが異なっています
-
【C言語】文字型と整数型の違い
おすすめ情報