「Microsoft Visual Cppコード集」の版間の差分
		
		
		
		
		
		ナビゲーションに移動
		検索に移動
		
				
		
		
	
| (同じ利用者による、間の5版が非表示) | |||
| 3行目: | 3行目: | ||
| Microsoft Visual C++ 2008をベースとしています。 | Microsoft Visual C++ 2008をベースとしています。 | ||
| == フォームアプリのデバック中、「出力」ウィンドウにメッセージを出す == | == TIPS == | ||
| === マネージ・アンマネージ間文字列変換 === | |||
| <source lang="cpp"> | |||
| System::String^ NativeWrapper::CopyWrapper(System::String^ src) | |||
| { | |||
| 	msclr::interop::marshal_context^ context = gcnew msclr::interop::marshal_context(); | |||
| 	// String -> char* 変換 | |||
| 	const char *psrc = context->marshal_as<const char*>(src); | |||
| 	char *pdest = new char[ src->Length + 1 ]; | |||
| 	// psrcの値をpdestにコピー | |||
| 	_nativeClass->Copy( pdest, psrc ); | |||
| 	delete context; | |||
| 	// char* -> String 変換 | |||
| 	System::String^ dest =  msclr::interop::marshal_as<System::String^>( pdest ); | |||
| 	delete pdest; | |||
| 	return dest; | |||
| } | |||
| </source> | |||
| * 動作確認 | |||
| ** Visual Studio 2010 Professional | |||
| * 参考書籍 | |||
| ** ひと目でわかるMicrosoft Visual C++ 2008 アプリケーション開発入門 | |||
| -- 2010年7月19日 (月) 17:16 (UTC) | |||
| === 固定長配列を使用しない文字列の結合(ネイティブ) === | |||
| <source lang="cpp"> | |||
| const char* c1 = "123"; | |||
| const char* c2 = "abc"; | |||
| std::string s = std::string(c1) + c2; // s -> "123abc" | |||
| </source> | |||
| -- 2010年7月5日 (月) 16:10 (UTC) | |||
| === フォームアプリのデバック中、「出力」ウィンドウにメッセージを出す === | |||
| <source lang="cpp"> | <source lang="cpp"> | ||
| 13行目: | 52行目: | ||
| -- 2010年7月4日 (日) 08:35 (UTC) | -- 2010年7月4日 (日) 08:35 (UTC) | ||
| == deleteによる2重の領域開放をした場合の動作 == | === deleteによる2重の領域開放をした場合の動作 === | ||
| <source lang="cpp"> | <source lang="cpp"> | ||
| 31行目: | 70行目: | ||
| [[ファイル:Visual cpp 2008 delete error 2.png]] | [[ファイル:Visual cpp 2008 delete error 2.png]] | ||
| -- 2010年7月4日 (日) 10: | -- 2010年7月4日 (日) 10:08 (UTC) | ||
| == トラブルシューティング == | |||
| === インクルード時に警告が表示され、インクルードに含まれるクラスが使用できない === | |||
|  1>.\StringTest.cpp(2) : warning C4627: '#include <string>': プリコンパイル済みヘッダーの使用を検索中にスキップされました | |||
|  1>        ディレクティブを 'stdafx.h' に追加するか、プリコンパイル済みヘッダーをビルドし直します | |||
| 対策は、出力の内容通りに「#include "stdafx.h"」を追加することです。ただし、インクルードの先頭行である必要があります。 | |||
| -- 2010年7月5日 (月) 16:10 (UTC) | |||
| == 更新履歴 == | == 更新履歴 == | ||
| * ページ作成 -- 2010年7月4日 (日) 08:35 (UTC) | * ページ作成 -- 2010年7月4日 (日) 08:35 (UTC) | ||
2010年7月19日 (月) 18:04時点における最新版
Microsoft Visual C++ 2008をベースとしています。
TIPS
マネージ・アンマネージ間文字列変換
System::String^ NativeWrapper::CopyWrapper(System::String^ src)
{
	msclr::interop::marshal_context^ context = gcnew msclr::interop::marshal_context();
	// String -> char* 変換
	const char *psrc = context->marshal_as<const char*>(src);
	char *pdest = new char[ src->Length + 1 ];
	// psrcの値をpdestにコピー
	_nativeClass->Copy( pdest, psrc );
	delete context;
	// char* -> String 変換
	System::String^ dest =  msclr::interop::marshal_as<System::String^>( pdest );
	delete pdest;
	return dest;
}
- 動作確認
- Visual Studio 2010 Professional
 
- 参考書籍
- ひと目でわかるMicrosoft Visual C++ 2008 アプリケーション開発入門
 
-- 2010年7月19日 (月) 17:16 (UTC)
固定長配列を使用しない文字列の結合(ネイティブ)
const char* c1 = "123";
const char* c2 = "abc";
std::string s = std::string(c1) + c2; // s -> "123abc"
-- 2010年7月5日 (月) 16:10 (UTC)
フォームアプリのデバック中、「出力」ウィンドウにメッセージを出す
System::Diagnostics::Debug::WriteLine("test");
Console::WriteLineでは出力できませんでした。
-- 2010年7月4日 (日) 08:35 (UTC)
deleteによる2重の領域開放をした場合の動作
Test *p = new Test;
p->v1 = 1;
p->v2 = '1';
delete p;
delete p;
Debugビルドの場合、上記のコードを実行すると以下のダイアログが出ました。
Releaseビルドの場合、特別なダイアログも出さず異常終了しました。
-- 2010年7月4日 (日) 10:08 (UTC)
トラブルシューティング
インクルード時に警告が表示され、インクルードに含まれるクラスが使用できない
1>.\StringTest.cpp(2) : warning C4627: '#include <string>': プリコンパイル済みヘッダーの使用を検索中にスキップされました 1> ディレクティブを 'stdafx.h' に追加するか、プリコンパイル済みヘッダーをビルドし直します
対策は、出力の内容通りに「#include "stdafx.h"」を追加することです。ただし、インクルードの先頭行である必要があります。
-- 2010年7月5日 (月) 16:10 (UTC)
更新履歴
- ページ作成 -- 2010年7月4日 (日) 08:35 (UTC)

