Can anybody help me with getting managed code to talk to unmanaged code? I'm okay coding via clr, but I don't have a clue what I'm doing when I try using the GUI interface. I'm using MS Visual C++ 2005 and I'm totally lost VB is much easier. I could send what I have and what I'm trying to do if someone can help, otherwise, thanks anyway. After spending 3 vacation days on this, I figure I've got nothing to loose by asking for help. Yes, it is homework, but yes I'm about to give up.

Agreed, but I have no choice. The instructor is making us do this.

In that case you might want to post your code here, although you are unlikely to find much help since this is predominantly a c++ forum.

In my opinion, the syntax of c++.net is an insult to Bjarne Stroustrup's original idea, and looks just as dirty as MFC. :)

Here's the generated cpp code, that I've been told not to touch.

// MyTree.cpp : main project file.

#include "stdafx.h"
#include "Form1.h"

using namespace MyTree;

[STAThreadAttribute]
int main(array<System::String ^> ^args)
{
	// Enabling Windows XP visual effects before any controls are created
	Application::EnableVisualStyles();
	Application::SetCompatibleTextRenderingDefault(false); 

	// Create the main window and run it
	Application::Run(gcnew Form1());
	return 0;
}

This is also generated form1.h code, with the exception of the code between the first "public" and the first "private" (which I'm pretty sure I did wrong) and the private: System::Void SubmitBtn_Click code ...that's where it all goes bad...of course that's where I don't know what I'm doing.

#pragma once
#include <iostream>
#include "string.h"
#include "BinaryTree.h"
#include "DataMerge.h"
using namespace std;

namespace MyTree {

	using namespace System;
	using namespace System::ComponentModel;
	using namespace System::Collections;
	using namespace System::Windows::Forms;
	using namespace System::Data;
	using namespace System::Drawing;
	using namespace System::Runtime::InteropServices;
	
		
	/// <summary>
	/// Summary for Form1
	///
	/// 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 Form1 : public System::Windows::Forms::Form
	{
	public:
		Form1(void)
		{
			InitializeComponent();
	
			//
			//TODO: Add the constructor code here
			dataentry = new CBinaryTree();
			//pData = new CDataMerge();			
			}
	public: 
		//CDataMerge *pData;
		CBinaryTree *dataentry;
			
	protected:
		/// <summary>
		/// Clean up any resources being used.
		/// </summary>
		~Form1()
		{
			if (components)
			{
				delete components;
			}
		}
	private: System::Windows::Forms::TextBox^  StudentTxt;
	protected: 

	private: System::Windows::Forms::Label^  IDlbl;
	private: System::Windows::Forms::TextBox^  LastTxt;
	private: System::Windows::Forms::TextBox^  FirstTxt;

	protected: 



	private: System::Windows::Forms::TextBox^  textBox4;
	private: System::Windows::Forms::Label^  Lastlbl;
	private: System::Windows::Forms::Label^  Firstlbl;
	private: System::Windows::Forms::Label^  Degreelbl;
	private: System::Windows::Forms::Button^  SubmitBtn;
	private: System::Windows::Forms::Button^  SearchBtn;
	private: System::Windows::Forms::Button^  ClearBtn;





	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)
		{
			this->StudentTxt = (gcnew System::Windows::Forms::TextBox());
			this->IDlbl = (gcnew System::Windows::Forms::Label());
			this->LastTxt = (gcnew System::Windows::Forms::TextBox());
			this->FirstTxt = (gcnew System::Windows::Forms::TextBox());
			this->textBox4 = (gcnew System::Windows::Forms::TextBox());
			this->Lastlbl = (gcnew System::Windows::Forms::Label());
			this->Firstlbl = (gcnew System::Windows::Forms::Label());
			this->Degreelbl = (gcnew System::Windows::Forms::Label());
			this->SubmitBtn = (gcnew System::Windows::Forms::Button());
			this->SearchBtn = (gcnew System::Windows::Forms::Button());
			this->ClearBtn = (gcnew System::Windows::Forms::Button());
			this->SuspendLayout();
			// 
			// StudentTxt
			// 
			this->StudentTxt->Location = System::Drawing::Point(113, 33);
			this->StudentTxt->Name = L"StudentTxt";
			this->StudentTxt->Size = System::Drawing::Size(76, 20);
			this->StudentTxt->TabIndex = 0;
			// 
			// IDlbl
			// 
			this->IDlbl->AutoSize = true;
			this->IDlbl->Location = System::Drawing::Point(12, 40);
			this->IDlbl->Name = L"IDlbl";
			this->IDlbl->Size = System::Drawing::Size(56, 13);
			this->IDlbl->TabIndex = 1;
			this->IDlbl->Text = L"Student Id";
			// 
			// LastTxt
			// 
			this->LastTxt->Location = System::Drawing::Point(113, 88);
			this->LastTxt->Name = L"LastTxt";
			this->LastTxt->Size = System::Drawing::Size(100, 20);
			this->LastTxt->TabIndex = 2;
			this->LastTxt->UseWaitCursor = true;
			// 
			// FirstTxt
			// 
			this->FirstTxt->Location = System::Drawing::Point(328, 86);
			this->FirstTxt->Name = L"FirstTxt";
			this->FirstTxt->Size = System::Drawing::Size(100, 20);
			this->FirstTxt->TabIndex = 3;
			// 
			// textBox4
			// 
			this->textBox4->Location = System::Drawing::Point(113, 141);
			this->textBox4->Name = L"textBox4";
			this->textBox4->Size = System::Drawing::Size(100, 20);
			this->textBox4->TabIndex = 4;
			// 
			// Lastlbl
			// 
			this->Lastlbl->AutoSize = true;
			this->Lastlbl->Location = System::Drawing::Point(21, 93);
			this->Lastlbl->Name = L"Lastlbl";
			this->Lastlbl->Size = System::Drawing::Size(58, 13);
			this->Lastlbl->TabIndex = 5;
			this->Lastlbl->Text = L"Last Name";
			// 
			// Firstlbl
			// 
			this->Firstlbl->AutoSize = true;
			this->Firstlbl->Location = System::Drawing::Point(256, 94);
			this->Firstlbl->Name = L"Firstlbl";
			this->Firstlbl->Size = System::Drawing::Size(57, 13);
			this->Firstlbl->TabIndex = 6;
			this->Firstlbl->Text = L"First Name";
			// 
			// Degreelbl
			// 
			this->Degreelbl->AutoSize = true;
			this->Degreelbl->Location = System::Drawing::Point(29, 149);
			this->Degreelbl->Name = L"Degreelbl";
			this->Degreelbl->Size = System::Drawing::Size(42, 13);
			this->Degreelbl->TabIndex = 7;
			this->Degreelbl->Text = L"Degree";
			// 
			// SubmitBtn
			// 
			this->SubmitBtn->Location = System::Drawing::Point(24, 198);
			this->SubmitBtn->Name = L"SubmitBtn";
			this->SubmitBtn->Size = System::Drawing::Size(85, 26);
			this->SubmitBtn->TabIndex = 8;
			this->SubmitBtn->Text = L"SUBMIT";
			this->SubmitBtn->UseVisualStyleBackColor = true;
			this->SubmitBtn->Click += gcnew System::EventHandler(this, &Form1::SubmitBtn_Click);
			// 
			// SearchBtn
			// 
			this->SearchBtn->Location = System::Drawing::Point(184, 198);
			this->SearchBtn->Name = L"SearchBtn";
			this->SearchBtn->Size = System::Drawing::Size(99, 26);
			this->SearchBtn->TabIndex = 9;
			this->SearchBtn->Text = L"SEARCH";
			this->SearchBtn->UseVisualStyleBackColor = true;
			this->SearchBtn->Click += gcnew System::EventHandler(this, &Form1::SearchBtn_Click);
			// 
			// ClearBtn
			// 
			this->ClearBtn->Location = System::Drawing::Point(346, 198);
			this->ClearBtn->Name = L"ClearBtn";
			this->ClearBtn->Size = System::Drawing::Size(81, 26);
			this->ClearBtn->TabIndex = 10;
			this->ClearBtn->Text = L"CLEAR";
			this->ClearBtn->UseVisualStyleBackColor = true;
			this->ClearBtn->Click += gcnew System::EventHandler(this, &Form1::ClearBtn_Click);
			// 
			// Form1
			// 
			this->AutoScaleDimensions = System::Drawing::SizeF(6, 13);
			this->AutoScaleMode = System::Windows::Forms::AutoScaleMode::Font;
			this->ClientSize = System::Drawing::Size(462, 293);
			this->Controls->Add(this->ClearBtn);
			this->Controls->Add(this->SearchBtn);
			this->Controls->Add(this->SubmitBtn);
			this->Controls->Add(this->Degreelbl);
			this->Controls->Add(this->Firstlbl);
			this->Controls->Add(this->Lastlbl);
			this->Controls->Add(this->textBox4);
			this->Controls->Add(this->FirstTxt);
			this->Controls->Add(this->LastTxt);
			this->Controls->Add(this->IDlbl);
			this->Controls->Add(this->StudentTxt);
			this->Name = L"Form1";
			this->Text = L"Form1";
			this->ResumeLayout(false);
			this->PerformLayout();

		}
#pragma endregion
char value1;
char value2;
//char result;

private: System::Void SubmitBtn_Click(System::Object^  sender, System::EventArgs^  e)
		 { 
			 value1=NULL;
			 String ^buf = LastTxt->Text->ToString();
			 value1 = Convert::ToInt32(buf);
			 
			 const char* TextBuffer = (const char*)
			(Marshal::StringToHGlobalAnsi(buf)).ToPointer();
			 strcpy_s (MyData->insert, TextBuffer);
			//Marshal::FreeHGlobal(IntPtr((void*)TextBuffer));
			CBinaryTree<string> tree1;
			 tree1.insert(TextBuffer);
			 //String ^buf = FirstTxt->Text->ToString();
			 
		 }
private: System::Void SearchBtn_Click(System::Object^  sender, System::EventArgs^  e)
		 {
		 }
private: System::Void ClearBtn_Click(System::Object^  sender, System::EventArgs^  e)
		 {
		 }
};
}

This is the code I used to generate a tree. The assignment was to use the gui to add to, search and clear the tree; simplistically anyway.

#pragma once

#ifndef BINARYTREE_H
#define BINARYTREE_H

template <typename Item>
class TreeNode
{
public:
   Item data;
   TreeNode<Item> *left;
   TreeNode<Item> *right;
   TreeNode<Item> *parent;
   TreeNode ()
   {
	   left = NULL;
	   right = NULL;
   }
   TreeNode(Item data)
   {
	   this->data = data;
	   left = NULL;
	   right = NULL;
   }
};
template <typename Item>
class CBinaryTree
{
public:
	CBinaryTree(void); //create empty tree with default root node
 	CBinaryTree(Item dataentry[], int arraySize); 
	bool insert(Item data);
	void inorder();
	void preorder();
	void postorder();
	int getSize();

  private:
//   TreeNode<Item>* current;
   TreeNode<Item>* root;
   bool subtree; //does it reference a part of a larger object
   int size;
   void inorder(TreeNode<Item> *root);
   void postorder(TreeNode<Item> *root);
   void preorder(TreeNode<Item> *root);
};

template <typename Item>
CBinaryTree<Item>::CBinaryTree()
{
   //create a root node with no value
   root = NULL;
   current = NULL;
   size = 0;
   subtree = false;
}
	
template <typename Item>
CBinaryTree<Item>::CBinaryTree(Item dataentry[], int arraySize)
{
   root = NULL;
   size = 0;
   for (int i = 0; i < arraySize; i++)
   {
	   insert(dataentry[i]);
   }
}
template <typename Item>
bool CBinaryTree<Item>::insert(Item data)
{
	if (root == NULL)
		root = new TreeNode<Item>(data);
	else
	{
		TreeNode<Item> *parent = NULL;
		TreeNode<Item> *current = root;
		while(current != NULL)
			if (data < current->data)
			{
				parent = current;
				current = current->left;
			}
			else if (data > current->data)
				{
				parent = current;
				current = current->right;
			}
			else
				return false;
			if (data < parent->data)
				parent->left = new TreeNode<Item>(data);
			else
				parent->right = new TreeNode<Item>(data);
	}
	size ++;
	return true;
}
template <typename Item>
void CBinaryTree<Item>::inorder()
{
	inorder(root);
}
template <typename Item>
void CBinaryTree<Item>::inorder(TreeNode<Item> *root)
{
	if(root == NULL) return;
	inorder(root->left);
	cout << root->data<< " ";
	inorder(root->right);
}
template <typename Item>
void CBinaryTree<Item>::postorder()
{
	postorder(root);
}
template <typename Item>
void CBinaryTree<Item>::postorder(TreeNode<Item> *root)
{
	if(root == NULL) return;
	postorder(root->left);
	postorder(root->right);
	cout << root->data<< " ";
}
template <typename Item>
void CBinaryTree<Item>::preorder()
{
	preorder(root);
}
template <typename Item>
void CBinaryTree<Item>::preorder(TreeNode<Item> *root)
{
	if(root == NULL) return;
	cout << root->data<< " ";
	preorder(root->left);
	preorder(root->right);
	
}
template <typename Item>
int CBinaryTree<Item>::getSize()
{
	return size;
}
#endif

I think that's all of it with the exception of the form itself. Any help is greatly appreciated, thanks in advance. Thanks iamthwee for having me post it anyway.

> getting managed code to talk to unmanaged code?
the easy way is to just mix the managed and unmanaged code as required.
this is called IJW ( "it just works" ). using p-invoke is only slightly harder.

#include "stdafx.h"
#include <iostream>
#include <algorithm>
#include <iterator>
using namespace System;
using namespace std ;

#pragma unmanaged
void sort_and_print( int* p, size_t N ) 
{
  std::sort( p, p+N ) ;
  std::copy( p, p+N, ostream_iterator<int>(cout,"\n") ) ;
}

#pragma managed
int main( array<System::String ^> ^args )
{
    enum { SIZE = 100 };
    array<int>^ a = gcnew array<int>(SIZE);
    System::Random^ rg = gcnew System::Random() ;
    for( int i=0 ; i<SIZE; ++i ) a[i] = rg->Next( 500, 1000 ) ;

    // pin the array (don't let the gc move it around).
    pin_ptr<int> pinned_address = &( a[0] ) ;   
    int* cpp_pointer = pinned_address ;
    sort_and_print( cpp_pointer, SIZE ) ;
}
This article has been dead for over six months. Start a new discussion instead.