Click here to Skip to main content
15,884,629 members
Articles / Desktop Programming / Win32

Mixing .NET and native code

Rate me:
Please Sign up or sign in to vote.
4.85/5 (49 votes)
7 Apr 2014CPOL9 min read 163.6K   7.2K   139  
A first approach to mixing .NET and native code, using the C++/CLI gateway.
#pragma once

using namespace System;
using namespace System::ComponentModel;
using namespace System::Collections;
using namespace System::Windows::Forms;
using namespace System::Data;
using namespace System::Drawing;


namespace AdR {
   namespace Samples {
      namespace NativeCallingCLR {
         namespace ConsoleWithMFCAndCLR {

	/// <summary>
	/// Summary for FileContentFrm
	///
	/// WARNING: If you change the name of this class, you will need to change the
	///          'Resource File Name' property for the managed resource compiler tool
	///          associated with all .resx files this class depends on.  Otherwise,
	///          the designers will not be able to interact properly with localized
	///          resources associated with this form.
	/// </summary>
	public ref class FileContentFrm : public System::Windows::Forms::Form
	{
	public:
		FileContentFrm(void)
		{
			InitializeComponent();
			//
			//TODO: Add the constructor code here
			//
		}

	protected:
		/// <summary>
		/// Clean up any resources being used.
		/// </summary>
		~FileContentFrm()
		{
			if (components)
			{
				delete components;
			}
		}
   public: System::Windows::Forms::TextBox^  txtFileContent;
   protected: 

   protected: 

	private:
		/// <summary>
		/// Required designer variable.
		/// </summary>
		System::ComponentModel::Container ^components;

#pragma region Windows Form Designer generated code
		/// <summary>
		/// Required method for Designer support - do not modify
		/// the contents of this method with the code editor.
		/// </summary>
		void InitializeComponent(void)
		{
         System::Windows::Forms::Button^  btnClose;
         this->txtFileContent = (gcnew System::Windows::Forms::TextBox());
         btnClose = (gcnew System::Windows::Forms::Button());
         this->SuspendLayout();
         // 
         // btnClose
         // 
         btnClose->Anchor = System::Windows::Forms::AnchorStyles::Bottom;
         btnClose->Location = System::Drawing::Point(259, 331);
         btnClose->Name = L"btnClose";
         btnClose->Size = System::Drawing::Size(75, 23);
         btnClose->TabIndex = 1;
         btnClose->Text = L"&Close";
         btnClose->UseVisualStyleBackColor = true;
         btnClose->Click += gcnew System::EventHandler(this, &FileContentFrm::btnClose_Click);
         // 
         // txtFileContent
         // 
         this->txtFileContent->Anchor = static_cast<System::Windows::Forms::AnchorStyles>((((System::Windows::Forms::AnchorStyles::Top | System::Windows::Forms::AnchorStyles::Bottom) 
            | System::Windows::Forms::AnchorStyles::Left) 
            | System::Windows::Forms::AnchorStyles::Right));
         this->txtFileContent->BackColor = System::Drawing::SystemColors::Window;
         this->txtFileContent->Font = (gcnew System::Drawing::Font(L"Courier New", 8.25F, System::Drawing::FontStyle::Regular, System::Drawing::GraphicsUnit::Point, 
            static_cast<System::Byte>(0)));
         this->txtFileContent->Location = System::Drawing::Point(12, 12);
         this->txtFileContent->Multiline = true;
         this->txtFileContent->Name = L"txtFileContent";
         this->txtFileContent->ReadOnly = true;
         this->txtFileContent->ScrollBars = System::Windows::Forms::ScrollBars::Both;
         this->txtFileContent->Size = System::Drawing::Size(568, 313);
         this->txtFileContent->TabIndex = 0;
         // 
         // FileContentFrm
         // 
         this->AutoScaleDimensions = System::Drawing::SizeF(6, 13);
         this->AutoScaleMode = System::Windows::Forms::AutoScaleMode::Font;
         this->ClientSize = System::Drawing::Size(592, 366);
         this->Controls->Add(btnClose);
         this->Controls->Add(this->txtFileContent);
         this->Name = L"FileContentFrm";
         this->ShowInTaskbar = false;
         this->SizeGripStyle = System::Windows::Forms::SizeGripStyle::Show;
         this->StartPosition = System::Windows::Forms::FormStartPosition::CenterParent;
         this->Text = L"File content";
         this->Load += gcnew System::EventHandler(this, &FileContentFrm::FileContentFrm_Load);
         this->ResumeLayout(false);
         this->PerformLayout();

      }
#pragma endregion

   private:
      System::Void btnClose_Click(System::Object^ sender, System::EventArgs^ e)
      {
         UNREFERENCED_PARAMETER(sender);
         UNREFERENCED_PARAMETER(e);
         Close();
      }
   private: System::Void FileContentFrm_Load(System::Object^ sender, System::EventArgs^ e)
      {
         UNREFERENCED_PARAMETER(sender);
         UNREFERENCED_PARAMETER(e);

         this->txtFileContent->SelectionStart = 0;
         this->txtFileContent->SelectionLength = 0;
      }
   };
} } } }

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Architect
France France
I started working in 1991 as an engineer. My pecialization: development in robotics.

After two years in this job, I began a new career leaving robotics, and coming to standard development as C++ expert.

This enabled me to diversify my knowledge by putting one foot on the side of the systems. Then began my double competence.

In 2000 I started a new job as consultant. Many experiences came with that new job, continuing with both development and systems subjects.

I reinforced my knowledge in technical architectures, IT security, and IT production management.

2011: a new experience. I founded the company Net-InB, a computer services company specialized in the fields of infrastructures, systems and software.

Now I am both technical and software architect specialized in Microsoft products and technologies.

Comments and Discussions