ページ 11

msclr/marshal.h が無い。

Posted: 2017年9月23日(土) 12:10
by shikayama
[1] 質問文
 [1.1] msclr/marshal.h が無い。
 [1.2] 以下記述
 [1.3] 1>.\list-11-3-2.cpp(5) : fatal error C1083: include ファイルを開けません。'msclr/marshal.h': No such file or directory
 [1.4] msclr/marshal.h の入手方法を知りたい。

[2] 環境  
 [2.1] OS : Windows 10
 [2.2] コンパイラ名 : VC++ 2008EE
[3] その他
 ・C言語初歩

コード:

// list-11-3-2.cpp : メイン プロジェクト ファイルです。

#include "stdafx.h"
#include <iostream>
#include <msclr/marshal.h>			//   マーシャリング用

using namespace System;
using namespace std;
using namespace msclr::interop;		//  マーシャリング用

int main(array<System::String ^> ^args)
{
	char * cstr = "Hello/char";
	cout << "char * --> " << cstr << endl;
	String ^ Str = marshal_as<String ^>(cstr);
	Console::WriteLine("String --> " + Str);

	String ^ Str1 = %String("Hello/String");
	Console::WriteLine("String --> " + Str1);
	marshal_context ^ context = gcnew marshal_context();
	const char * cstr1 = context->marshal_as<const char *>(Str1);
	cout << "char * --> " << cstr1 << endl;

	Console::WriteLine();
  Console::WriteLine("Enterキーを押して終了。");
	Console::ReadLine();
  return 0;
}

Re: msclr/marshal.h が無い。

Posted: 2017年9月23日(土) 19:16
by Math
>[2.2] コンパイラ名 : VC++ 2008EE
VC++ 2008EEのmsclrディレクトリにはmarshal.hがないようですね。VC++ 2008 pro にはあるようです。

VS2007Communityには存在して下記のように実行できます。

C:\Program Files (x86)\Microsoft Visual Studio\2017\Community\VC\Tools\MSVC\14.10.25017\include\msclr\marshal.h
に存在する

c1.cpp

コード:

// list-11-3-2.cpp : メイン プロジェクト ファイルです。
 
//#include "stdafx.h"    <===    プリコンパイル・ヘッダ使用しない
#include <iostream>
#include <msclr/marshal.h>          //   マーシャリング用
 
using namespace System;
//using namespace std;  <=== c1.cpp(11): error C2976: 'std::array': テンプレート 引数の数が少なすぎますエラー をさける
using namespace msclr::interop;     //  マーシャリング用
 
int main(array<System::String ^> ^args)  // c1.cpp(11): error C2976: 'std::array': テンプレート 引数の数が少なすぎますエラー
{
    char * cstr = "Hello/char";
    std::cout << "char * --> " << cstr << std::endl;
    String ^ Str = marshal_as<String ^>(cstr);
    Console::WriteLine("String --> " + Str);
 
    String ^ Str1 = %String("Hello/String");
    Console::WriteLine("String --> " + Str1);
    marshal_context ^ context = gcnew marshal_context();
    const char * cstr1 = context->marshal_as<const char *>(Str1);
    std::cout << "char * --> " << cstr1 << std::endl;
 
    Console::WriteLine();
  Console::WriteLine("Enterキーを押して終了。");
    Console::ReadLine();
  return 0;
}
c.bat

コード:

rem コンパイル後リンク
cl /clr c1.cpp
rem 実行結果
c1.exe
実行結果

コード:

D:\z17c\clr\0923>c

D:\z17c\clr\0923>rem コンパイル後リンク

D:\z17c\clr\0923>cl /clr c1.cpp
Microsoft(R) C/C++ Optimizing Compiler Version 19.10.25019
for Microsoft(R) .NET Framework Version 4.07.2110.0
Copyright (C) Microsoft Corporation.  All rights reserved.

c1.cpp
Microsoft (R) Incremental Linker Version 14.10.25019.0
Copyright (C) Microsoft Corporation.  All rights reserved.

/out:c1.exe
c1.obj

D:\z17c\clr\0923>rem 実行結果

D:\z17c\clr\0923>c1.exe
char * --> Hello/char
String --> Hello/char
String --> Hello/String
char * --> Hello/String

Enterキーを押して終了。