Archive for the ‘C++/CLI’ Category

  1. Hello, Connector/NET(C++/CLI) World!

    Posted on 4月 3rd, 2013 by cx20

    Connector/NET(C++/CLI)

    Connector/NET は、.NET ベースの MySQL 接続用 API である。
    以下は C++/CLI による Connector/NET ライブラリを使用した MySQL への接続例となっている。

    ソースコード(C++/CLI + Connector/NET + MySQL)

    #using <System.dll>
    #using <System.Data.dll>
    #using <MySql.Data.dll>
     
    using namespace System;
    using namespace MySql::Data::MySqlClient;
     
    int main( array<System::String^>^ args )
    {
        String^ conStr = "server=localhost;user id=root;password=P@ssW0rd";
        String^ sqlStr = "SELECT 'Hello, Connector/NET World!' AS Message";
     
        MySqlConnection^ con = gcnew MySqlConnection(conStr);
        MySqlCommand^ cmd = gcnew MySqlCommand(sqlStr, con);
        con->Open();
        MySqlDataReader^ reader = cmd->ExecuteReader();
        while( reader->Read() )
        {
            Console::WriteLine( reader->GetName(0) );
            Console::WriteLine( "---------------------" );
            Console::WriteLine( reader[0] );
        }
        reader->Close();
        con->Close();
     
       return 0;
    }

    コンパイル方法

    C:¥> SET LIBPATH=C:\Program Files (x86)\MySQL\Connector NET 6.5.4\Assemblies\v2.0;%LIBPATH%
    C:¥> cl Hello.cpp /clr

    実行結果

    MESSAGE
    ---------------------
    Hello, Connector/NET World!
  2. Hello, ODP.NET(C++/CLI) World!

    Posted on 9月 5th, 2012 by cx20

    ODP.NET(C++/CLI)

    ODP.NET(Oracle Data Provider for .NET)は、.NET ベースの Oracle Database 接続用 API である。ODAC(Oracle Data Access Component)と呼ばれるパッケージに含まれる。
    .NET 環境での Oracle Database 用データプロバイダとしては、マイクロソフト社が提供する「Microsoft Oracle Client」とオラクル社が提供する「ODP.NET」があるが、現在、「Microsoft Oracle Client」はマイクロソフト社自身が非推奨としており、今後は ODP.NET の使用が推奨されている。

    データプロバイダ 説明
    System.Data.OracleClient .NET Framework Data Provider for Oracle
    Oracle.DataAccess.Client Oracle Data Provider for .NET

    ソースコード(C++/CLI + ODP.NET + Oracle)

    #using <System.dll>
    #using <System.Data.dll>
    #using <Oracle.DataAccess.dll>
     
    using namespace System;
    using namespace Oracle::DataAccess::Client;
     
    int main( array<System::String^>^ args )
    {
        String^ conStr = "Data Source=ORCL;User ID=scott;Password=tiger;";
        String^ sqlStr = "SELECT 'Hello, ODP.NET World!' AS Message FROM DUAL";
     
        OracleConnection^ con = gcnew OracleConnection(conStr);
        OracleCommand^ cmd = gcnew OracleCommand(sqlStr, con);
        con->Open();
        OracleDataReader^ reader = cmd->ExecuteReader();
        while( reader->Read() )
        {
            Console::WriteLine( reader->GetName(0) );
            Console::WriteLine( "---------------------" );
            Console::WriteLine( reader[0] );
        }
        reader->Close();
        con->Close();
     
       return 0;
    }

    コンパイル方法

    C:¥> SET LIBPATH=<ORA_HOME>odp.netbin2.x;%LIBPATH%
    C:¥> cl Hello.cpp /clr

    実行結果

    MESSAGE
    ---------------------
    Hello, ODP.NET World!
  3. Hello, ADO.NET(C++/CLI) World!

    Posted on 9月 5th, 2012 by cx20

    ADO.NET(C++/CLI)

    ADO.NET(ActiveX Data Objects .NET)は、ADO の後継で .NET ベースの DBMS 接続用 API である。
    .NET データプロバイダを介することで様々な DBMS への接続が可能となっている。
    .NET Framework 標準で使用できる .NET データプロバイダとしては、以下のようなプロバイダがある。

    データプロバイダ名 説明
    System.Data.SqlClient Microsoft SQL Server
    System.Data.OleDb OLE DB
    System.Data.Odbc ODBC
    System.Data.OracleClient Oracle

    ソースコード(C++/CLI + ADO.NET + OLE DB + Jet データベース)

    #using <System.dll>
    #using <System.Data.dll>
     
    using namespace System;
    using namespace System::Data::OleDb;
     
    int main( array<System::String^>^ args )
    {
        String^ conStr = "Provider=Microsoft.Jet.OLEDB.4.0;"
            + "Data Source=.\hello.mdb;";
        String^ sqlStr = "SELECT 'Hello, ADO.NET World!' AS Message";
     
        OleDbConnection^ con = gcnew OleDbConnection(conStr);
        OleDbCommand^ cmd = gcnew OleDbCommand(sqlStr, con);
        con->Open();
        OleDbDataReader^ reader = cmd->ExecuteReader();
        while( reader->Read() )
        {
            Console::WriteLine( reader->GetName(0) );
            Console::WriteLine( "---------------------" );
            Console::WriteLine( reader[0] );
        }
        reader->Close();
        con->Close();
     
       return 0;
    }

    ソースコード(C++/CLI + ADO.NET + OLE DB + ACE データベース)

    #using <System.dll>
    #using <System.Data.dll>
     
    using namespace System;
    using namespace System::Data::OleDb;
     
    int main( array<System::String^>^ args )
    {
        String^ conStr = "Provider=Microsoft.ACE.OLEDB.12.0;"
            + "Data Source=.\hello.accdb;";
        String^ sqlStr = "SELECT 'Hello, ADO.NET World!' AS Message";
     
        OleDbConnection^ con = gcnew OleDbConnection(conStr);
        OleDbCommand^ cmd = gcnew OleDbCommand(sqlStr, con);
        con->Open();
        OleDbDataReader^ reader = cmd->ExecuteReader();
        while( reader->Read() )
        {
            Console::WriteLine( reader->GetName(0) );
            Console::WriteLine( "---------------------" );
            Console::WriteLine( reader[0] );
        }
        reader->Close();
        con->Close();
     
       return 0;
    }

    ソースコード(C++/CLI + ADO.NET + SQL Server)

    #using <System.dll>
    #using <System.Data.dll>
     
    using namespace System;
    using namespace System::Data::SqlClient;
     
    int main( array<System::String^>^ args )
    {
        String^ conStr = "SERVER=(local);"
            + "DATABASE=master;"
            + "UID=sa;"
            + "PWD=P@ssW0rd";
        String^ sqlStr = "SELECT 'Hello, ADO.NET World!' AS Message";
     
        SqlConnection^ con = gcnew SqlConnection(conStr);
        SqlCommand^ cmd = gcnew SqlCommand(sqlStr, con);
        con->Open();
        SqlDataReader^ reader = cmd->ExecuteReader();
        while( reader->Read() )
        {
            Console::WriteLine( reader->GetName(0) );
            Console::WriteLine( "---------------------" );
            Console::WriteLine( reader[0] );
        }
        reader->Close();
        con->Close();
     
       return 0;
    }

    ソースコード(C++/CLI + ADO.NET + Oracle)

    #using <System.dll>
    #using <System.Data.dll>
    #using <System.Data.OracleClient.dll>
     
    using namespace System;
    using namespace System::Data::OracleClient;
     
    int main( array<System::String^>^ args )
    {
        String^ conStr = "Data Source=ORCL;User ID=scott;Password=tiger;";
        String^ sqlStr = "SELECT 'Hello, ADO.NET World!' AS Message FROM DUAL";
     
        OracleConnection^ con = gcnew OracleConnection(conStr);
        OracleCommand^ cmd = gcnew OracleCommand(sqlStr, con);
        con->Open();
        OracleDataReader^ reader = cmd->ExecuteReader();
        while( reader->Read() )
        {
            Console::WriteLine( reader->GetName(0) );
            Console::WriteLine( "---------------------" );
            Console::WriteLine( reader[0] );
        }
        reader->Close();
        con->Close();
     
       return 0;
    }

    ソースコード(C++/CLI + ADO.NET + ODBC + MySQL)

    #using <System.dll>
    #using <System.Data.dll>
     
    using namespace System;
    using namespace System::Data::Odbc;
     
    int main( array<System::String^>^ args )
    {
        String^ conStr = "Driver={MySQL ODBC 5.1 Driver};Server=localhost;UID=root;PWD=P@ssW0rd";
        String^ sqlStr = "SELECT 'Hello, ADO.NET World!' AS Message";
     
        OdbcConnection^ con = gcnew OdbcConnection(conStr);
        OdbcCommand^ cmd = gcnew OdbcCommand(sqlStr, con);
        con->Open();
        OdbcDataReader^ reader = cmd->ExecuteReader();
        while( reader->Read() )
        {
            Console::WriteLine( reader->GetName(0) );
            Console::WriteLine( "---------------------" );
            Console::WriteLine( reader[0] );
        }
        reader->Close();
        con->Close();
     
       return 0;
    }

    コンパイル方法

    C:¥> cl Hello.cpp /clr

    実行結果

    Message
    ---------------------
    Hello, ADO.NET World!
  4. Hello, DirectX(C++/CLI) World!

    Posted on 7月 30th, 2012 by cx20

    Win32 DirectX(C++/CLI)

    DirectX はマイクロソフトが Wnidows 用に開発したマルチメディア処理用 API である。
    .NET Framework 用の DirectX ライブラリとしては、DirectX SDK より入手可能な Managed DirectX がある。
    以下は C++/CLI における Managed DirectX アプリケーション の例となっている。

    ソースコード

    #using <System.dll>
    #using <System.Drawing.dll>
    #using <System.Windows.Forms.dll>
    #using <Microsoft.DirectX.dll>
    #using <Microsoft.DirectX.Direct3D.dll>
     
     
    using namespace System;
    using namespace System::Drawing;
    using namespace System::Windows::Forms;
    using namespace Microsoft::DirectX;
    using namespace Microsoft::DirectX::Direct3D;
     
    public ref class HelloForm : public Form
    {
    public:
        HelloForm()
        {
            this->Size = System::Drawing::Size( 640, 480 );
            this->Text = "Hello, World!";
        }
        bool InitD3D()
        {
            presentParam_ = gcnew PresentParameters();
            presentParam_->Windowed = true;
            presentParam_->SwapEffect = SwapEffect::Discard;
            device_ = gcnew Device(0, DeviceType::Hardware, this, CreateFlags::HardwareVertexProcessing, presentParam_);
     
            InitFont();
     
            return true;
        }
        void InitFont()
        {
            FontDescription fd;
     
            fd.Height = 16;
            fd.FaceName = "MS ゴシック";
     
            font_ = gcnew Microsoft::DirectX::Direct3D::Font(device_, fd);
        }
        void Render()
        {
            if (device_ == nullptr)
            {
                return;
            }
     
            device_->Clear(ClearFlags::Target, Color::Blue, 1.0f, 0);
            device_->BeginScene();
     
            font_->DrawText(nullptr, "Hello, DirectX(C++/CLI) World!", 10, 10, Color::White);
     
            device_->EndScene();
            device_->Present();
        }
    private:
        Device^ device_;
        PresentParameters^ presentParam_;
        Microsoft::DirectX::Direct3D::Font^ font_;
    };
     
    int main( array<System::String^>^ args )
    {
        HelloForm^ form = gcnew HelloForm();
     
        form->InitD3D();
        form->Show();
     
        while (form->Created)
        {
            form->Render();
            Application::DoEvents();
        }
     
        return 0;
    }

    コンパイル方法

    C:¥> cl Hello.cpp /clr ^
         /AI "C:WindowsMicrosoft.NETDirectX for Managed Code1.0.2911.0" ^
         /AI "C:WindowsMicrosoft.NETDirectX for Managed Code1.0.2902.0" ^
         /FU "Microsoft.DirectX.dll" ^
         /FU "Microsoft.DirectX.Direct3D.dll" ^
         /FU "Microsoft.DirectX.Direct3DX.dll" ^
         /link /SUBSYSTEM:WINDOWS /ENTRY:main

    実行結果

    +------------------------------------------+
    |Hello, World!                    [_][~][X]|
    +------------------------------------------+
    |Hello, DirectX(C++/CLI) World!            |
    |                                          |
    |                                          |
    |                                          |
    |                                          |
    |                                          |
    |                                          |
    |                                          |
    |                                          |
    |                                          |
    +------------------------------------------+
  5. Hello, Win32 GUI(C++/CLI) World!

    Posted on 7月 5th, 2012 by cx20

    Win32 GUI(C++/CLI)

    Win32 アプリケーションは Windows 標準 API である Win32 API を使用した Windows アプリケーションである。
    以下は C++/CLI における Win32 GUI アプリケーション の例となっている。

    ソースコード

    #include <windows.h>
    #include <tchar.h>
     
    LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
    {
        PAINTSTRUCT ps;
        HDC hdc;
        LPCTSTR lpszMessage = _T("Hello, Win32 GUI(C++/CLI) World!");
     
        switch (message)
        {
        case WM_PAINT:
            hdc = BeginPaint( hWnd, &ps );
            TextOut( hdc, 0, 0, lpszMessage, lstrlen(lpszMessage) );
            EndPaint( hWnd, &ps );
            break;
        case WM_DESTROY:
            PostQuitMessage(0);
            break;
        default:
            return DefWindowProc(hWnd, message, wParam, lParam);
            break;
        }
     
        return 0;
    }
     
    int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
    {
        LPCTSTR lpszClassName = _T("helloWindow");
        LPCTSTR lpszWindowName = _T("Hello, World!");
     
        WNDCLASSEX wcex;
        wcex.cbSize = sizeof(WNDCLASSEX);
        wcex.style          = CS_HREDRAW | CS_VREDRAW;
        wcex.lpfnWndProc    = WndProc;
        wcex.cbClsExtra     = 0;
        wcex.cbWndExtra     = 0;
        wcex.hInstance      = hInstance;
        wcex.hIcon          = LoadIcon(hInstance, MAKEINTRESOURCE(IDI_APPLICATION));
        wcex.hCursor        = LoadCursor(NULL, IDC_ARROW);
        wcex.hbrBackground  = (HBRUSH)(COLOR_WINDOW+1);
        wcex.lpszMenuName   = NULL;
        wcex.lpszClassName  = lpszClassName;
        wcex.hIconSm        = LoadIcon(wcex.hInstance, MAKEINTRESOURCE(IDI_APPLICATION));
     
        RegisterClassEx(&wcex);
        HWND hWnd = CreateWindow(
            lpszClassName,
            lpszWindowName,
            WS_OVERLAPPEDWINDOW,
            CW_USEDEFAULT, CW_USEDEFAULT, 640, 480,
            NULL, NULL, hInstance, NULL
        );
     
        ShowWindow(hWnd, SW_SHOWDEFAULT);
        UpdateWindow(hWnd);
     
        MSG msg;
        while (GetMessage(&msg, NULL, 0, 0))
        {
            TranslateMessage(&msg);
            DispatchMessage(&msg);
        }
     
        return (int)msg.wParam;
    }

    コンパイル方法

    C:¥> cl /clr hello.cpp /link user32.lib gdi32.lib /SUBSYSTEM:WINDOWS

    実行結果

    +------------------------------------------+
    |Hello, World!                    [_][~][X]|
    +------------------------------------------+
    |Hello, Win32 GUI(C++/CLI) World!          |
    |                                          |
    |                                          |
    |                                          |
    |                                          |
    |                                          |
    |                                          |
    |                                          |
    |                                          |
    |                                          |
    +------------------------------------------+
  6. Hello, Windows Forms(C++/CLI) World!

    Posted on 6月 5th, 2012 by cx20

    Windows Forms(C++/CLI)

    Windows フォーム(Windows Forms)は .NET Framework におけるユーザーインターフェイス基盤である。Windows アプリケーションにおけるウィンドウやダイアログに対応する。
    以下は C++/CLI における Windows フォーム の例となっている。

    ソースコード

    #using <System.dll>
    #using <System.Drawing.dll>
    #using <System.Windows.Forms.dll>
     
    using namespace System;
    using namespace System::Drawing;
    using namespace System::Windows::Forms;
     
    public ref class HelloForm : public Form
    {
    public:
        HelloForm()
        {
            this->Size = System::Drawing::Size( 640, 480 );
            this->Text = "Hello, World!";
            Label^ label1 = gcnew Label();
            label1->Text = "Hello, Windows Forms(C++/CLI) World!";
            label1->Size = System::Drawing::Size( 320, 20 );
            this->Controls->Add( label1 );
        }
    };
     
    int main( array<System::String^>^ args )
    {
       HelloForm^ form = gcnew HelloForm();
       Application::Run(form);
     
       return 0;
    }

    コンパイル方法

    C:¥> cl Hello.cpp /clr /link /SUBSYSTEM:WINDOWS /ENTRY:main

    実行結果

    +------------------------------------------+
    |Hello, World!                    [_][~][X]|
    +------------------------------------------+
    |Hello, Windows Forms(C++/CLI) World!      |
    |                                          |
    |                                          |
    |                                          |
    |                                          |
    |                                          |
    |                                          |
    |                                          |
    |                                          |
    |                                          |
    +------------------------------------------+
  7. Hello, COM(C++/CLI) World!

    Posted on 5月 21st, 2012 by cx20

    COM(C++/CLI)

    COM(Component Object Model)はマイクロソフトの提唱するプログラム部品の仕様である。
    COM を用いて開発された部品であれば言語を問わず利用することができる。
    以下は C++/CLI による COM クライアントの例となっている。

    ソースコード

    using namespace System;
    using namespace System::Runtime::InteropServices;
    using namespace Shell32;
     
    int main(array<System::String ^> ^args)
    {
        Shell^ shell = gcnew Shell;
        Folder^ folder;
        Object^ vRootFolder = (long)ShellSpecialFolderConstants::ssfWINDOWS;
        folder = shell->BrowseForFolder(0, "Hello, COM(C++/CLI) World!", 0, vRootFolder);
        if (folder != nullptr)
        {
            Marshal::ReleaseComObject(folder);
        }
        Marshal::ReleaseComObject(shell);
        return 0;
    }

    コンパイル方法

    C:¥> SET PATH=C:\Program Files (x86)\Microsoft SDKs\Windows\v7.0A\Bin;%PATH%
    C:¥> tlbimp %SystemRoot%\system32\shell32.dll /out:Shell32.dll
    C:¥> cl /FU Shell32.dll Hello.cs /clr

    実行結果

    +----------------------------------------+
    |Browse For Folder                    [X]|
    +----------------------------------------+
    | Hello, COM(C#) Wolrd!                  |
    |                                        |
    | +------------------------------------+ |
    | |[Windows]                           | |
    | | +[addins]                          | |
    | | +[AppCompat]                       | |
    | | +[AppPatch]                        | |
    | | +[assembly]                        | |
    | |     :                              | |
    | |     :                              | |
    | |     :                              | |
    | +------------------------------------+ |
    | [Make New Folder]    [  OK  ] [Cancel] |
    +----------------------------------------+
  8. Hello, Win32 API(C++/CLI) World!

    Posted on 4月 15th, 2012 by cx20

    Win32 API(C++/CLI)

    Win32 API は、Windows の機能にアクセスする為の API(Application Programming Interface)である。
    以下は C++/CLI からの呼出し例である。

    ソースコード

    #include <windows.h>
    #include <tchar.h>
     
    int _tmain( int argc, TCHAR* argv[] )
    {
        MessageBox( NULL, _T("Hello, Win32 API(C++/CLI) World!"), _T("Hello, World!"), MB_OK );
        return 0;
    }

    コンパイル方法(Visual C++)

    C:¥> cl hello.cpp /clr /link user32.lib

    実行結果

    ---------------------------
    Hello, World!
    ---------------------------
    Hello, Win32 API(C++/CLI) World!
    ---------------------------
    OK   
    ---------------------------
  9. Hello, C++/CLI World!

    Posted on 1月 19th, 2012 by cx20

    C++/CLI

    C++/CLI は C++ で .NET Framework を使用できるようにした、比較的新しい言語仕様である。Visual C++ 2005 より導入された。
    C++ の構文が使える他、.NET Framework のライブラリが使用できる。

    ソースコード

    #include <iostream>
     
    using namespace std;
     
    int main( int argc, char* argv[] )
    {
        cout << "Hello, C++/CLI World!" << endl;
        return 0;
    }

    ソースコード(.NET のライブラリを使用した場合)

    #using <mscorlib.dll>
     
    using namespace System;
     
    int main( array<String ^> ^args )
    {
        Console::WriteLine( "Hello, C++/CLI World!" );
        return 0;
    }

    コンパイル方法(Visual C++)

    C:¥> cl /clr hello.cpp

    実行結果

    Hello, C++/CLI World!