TOP Languages C++ Visual C++ .NET MSIL

enum class

enum class Colors{Red,Green,Blue,};
Colors col = Color::Red;
int i = static_cast<int>(color);
Colors color = static_cast<Colors>(2);
array<Colors>^arr = static_cast<array<Colors>^>(Enum::GetValues(Colors::typeid));
Array^ a = Enum::GetNames( Colors::typeid );
for ( i = 0; i < a->Length; i++ ) Console::Write( " {0}", a->GetValue( i ) );
Console::WriteLine("{0}",Colors::Blue.ToString());

static const

enum だとnamespace が深くなるので代替手段
ref class A{
public:
    static const VALUE1=1;
};
int i=A::VALUE1;

Enumの名前取り出し、名前から値の取り出し

void ColoredConsole::Sample(){
    array<String^>^colorNames = Enum::GetNames( ConsoleColor::typeid );
    for each(String^ col in colorNames){
        ConsoleColor c = *dynamic_cast<ConsoleColor^>(Enum::Parse( ConsoleColor:
:typeid, col ));

        Write(c,ConsoleColor::Black,col);
        WriteLine(c,ConsoleColor::White,col);
    }
}

Destructors vs Finalizers vs Close() method

Destructors in a reference type perform deterministic clean up of your resources.
Finalizers clean up unmanaged resources and can be called deterministically by the destructor or non-deterministically by the garbage collector.
When a Close() method is called, any managed resource can be temporarily closed and can be opened once again.

generic vs template

http://vene.wankuma.com/prog/CppCli_Generics.aspx
genericcan be used from an other assembly
templatecan be used in the same assembly

pin_ptr

Runtime/Environments

Resource

  1. about.txt favicon.img を用意.
(テキストはUTF8,bom付き)
; about.txt
; vim:encoding=CP932
; vim:fileencoding=utf-8
; vim:bomb
AboutCaptionFormat_ja={0}のバージョン情報
AboutCaptionFormat_en={0}'s version information
  1. 下記makefile で.resource生成
(resxgen,resx_merge は System::Resources::ResXResourceWriter()/Reader を使用した自作ツール。)
ABOUT_TXT=about.txt
ABOUT_IMG=about.ico
ABOUT_RESOURCES=$(ABOUT_TXT:%.txt=%.resources)
ABOUT_RESX=$(ABOUT_TXT:%.txt=%.resx)
ABOUT_RESX_STR=$(ABOUT_TXT:%.txt=%_str.resx)
ABOUT_RESX_BIN=$(ABOUT_TXT:%.txt=%_bin.resx)
$(ABOUT_RESOURCES): $(ABOUT_TXT) $(ABOUT_IMG)
    resgen $(ABOUT_TXT) $(ABOUT_RESX_STR)
    resxgen -p -o $(ABOUT_RESX_BIN) -e $(ABOUT_IMG)
    resx_merge -i $(ABOUT_RESX_STR) -i $(ABOUT_RESX_BIN) -o $(ABOUT_RESX)
    #cat data\ name $(ABOUT_RESX)
    resgen $(ABOUT_RESX) $(ABOUT_RESOURCES)
  1. プログラムの$(OBJS)とリンク
CLRIMAGETYPE=pure
LOPT=-ENTRY:main -SUBSYSTEM:WINDOWS -clrimagetype:$(CLRIMAGETYPE)
ASSEMBLY_RESOURCES+= /assemblyResource:$(ABOUT_RESOURCES)
$(TARGET).exe: $(OBJS)  $(ABOUT_RESOURCES)
    link $(OBJS) /out:$@ $(LOPT) $(ASSEMBLY_RESOURCES)
  1. プログラム中のアクセス
    System::Resources::ResourceManager^ rm ;
    System::Globalization::CultureInfo^ ci;
    rm = gcnew System::Resources::ResourceManager( "about", System::Reflection::Assembly::GetExecutingAssembly() );
    ci = gcnew System::Globalization::CultureInfo( System::Globalization::CultureInfo::CurrentUICulture->Name );
    String^ ci_name=String::Format("_{0:##}",ci->Name->Substring(0,2));
    String^ fmt=rm->GetString(String::Format("AboutCaptionFormat{0}",ci_name),ci);
    if(fmt == nullptr||fmt->Length==0) fmt="{0}'s version information";
    Text = String::Format(fmt,System::Windows::Forms::Application::ProductName);
    System::Drawing::Icon^ icon = safe_cast<System::Drawing::Icon^>(rm->GetObject("about.ico"));
  1. 多国語対応
(未確認)
    1. frディレクトリ作成
    2. fr/about.fr.txt 作成
    3. fr/about.fr.resources 生成
    4. fr/test_main.resources.dll生成
CULTUREDIRS=fr
DLLS=$(shell for x in $(CULTUREDIRS); do echo $$x/$(TARGET).resources.dll; done)
CULTURE_TEXTFILES:=$(shell for x in $(CULTUREDIRS); do echo $$x/strings.$$x.txt; done)
CULTURE_RESOURCES=$(CULTURE_TEXTFILES:%.txt=%.resources)
$(CULTURE_RESOURCES): $(CULTURE_TEXTFILES)
$(DLLS): $(CULTURE_RESOURCES)
    @(for x in $(CULTUREDIRS); do  \
        if [ ! -d $$x ] ; then  mkdir $$x; fi ;\
        al /t:lib /culture:$$x /embed:$$x\\about.$$x.resources /out:$$x\\$(TARGET).resources.dll  ;\
    done;)

管理人/副管理人のみ編集できます