User Name Password Register
DaniWeb IT Discussion Community
All
What is DaniWeb IT Discussion Community?
You're currently browsing the C++ section within the Software Development category of DaniWeb, a massive community of 423,610 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 3,211 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our C++ advertiser: Programming Forums
Views: 951 | Replies: 6
Reply
Join Date: Nov 2007
Posts: 14
Reputation: dallaseve is an unknown quantity at this point 
Rep Power: 1
Solved Threads: 0
dallaseve dallaseve is offline Offline
Newbie Poster

Help Managed and unmanaged code

  #1  
Nov 26th, 2007
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.
AddThis Social Bookmark Button
Reply With Quote  
Join Date: Aug 2005
Posts: 4,782
Reputation: iamthwee is a glorious beacon of light iamthwee is a glorious beacon of light iamthwee is a glorious beacon of light iamthwee is a glorious beacon of light iamthwee is a glorious beacon of light 
Rep Power: 17
Solved Threads: 319
iamthwee's Avatar
iamthwee iamthwee is offline Offline
Industrious Poster

Re: Managed and unmanaged code

  #2  
Nov 26th, 2007
c++.net was a mistake... Stick with c# or vb.net.
I'm not a programmer. My attitude starts with ignorance, holds steady at conversation, and ends with a trip to the hospital. Get used to it.
Reply With Quote  
Join Date: Nov 2007
Posts: 14
Reputation: dallaseve is an unknown quantity at this point 
Rep Power: 1
Solved Threads: 0
dallaseve dallaseve is offline Offline
Newbie Poster

Re: Managed and unmanaged code

  #3  
Nov 26th, 2007
Agreed, but I have no choice. The instructor is making us do this.
Reply With Quote  
Join Date: Aug 2005
Posts: 4,782
Reputation: iamthwee is a glorious beacon of light iamthwee is a glorious beacon of light iamthwee is a glorious beacon of light iamthwee is a glorious beacon of light iamthwee is a glorious beacon of light 
Rep Power: 17
Solved Threads: 319
iamthwee's Avatar
iamthwee iamthwee is offline Offline
Industrious Poster

Re: Managed and unmanaged code

  #4  
Nov 26th, 2007
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.
Last edited by iamthwee : Nov 26th, 2007 at 3:46 pm.
I'm not a programmer. My attitude starts with ignorance, holds steady at conversation, and ends with a trip to the hospital. Get used to it.
Reply With Quote  
Join Date: Nov 2007
Posts: 14
Reputation: dallaseve is an unknown quantity at this point 
Rep Power: 1
Solved Threads: 0
dallaseve dallaseve is offline Offline
Newbie Poster

Help Re: Managed and unmanaged code

  #5  
Nov 26th, 2007
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.
Reply With Quote  
Join Date: Dec 2006
Location: india
Posts: 1,074
Reputation: vijayan121 is a name known to all vijayan121 is a name known to all vijayan121 is a name known to all vijayan121 is a name known to all vijayan121 is a name known to all vijayan121 is a name known to all 
Rep Power: 9
Solved Threads: 161
vijayan121 vijayan121 is offline Offline
Veteran Poster

Re: Managed and unmanaged code

  #6  
Nov 27th, 2007
> 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.
  1. #include "stdafx.h"
  2. #include <iostream>
  3. #include <algorithm>
  4. #include <iterator>
  5. using namespace System;
  6. using namespace std ;
  7.  
  8. #pragma unmanaged
  9. void sort_and_print( int* p, size_t N )
  10. {
  11. std::sort( p, p+N ) ;
  12. std::copy( p, p+N, ostream_iterator<int>(cout,"\n") ) ;
  13. }
  14.  
  15. #pragma managed
  16. int main( array<System::String ^> ^args )
  17. {
  18. enum { SIZE = 100 };
  19. array<int>^ a = gcnew array<int>(SIZE);
  20. System::Random^ rg = gcnew System::Random() ;
  21. for( int i=0 ; i<SIZE; ++i ) a[i] = rg->Next( 500, 1000 ) ;
  22.  
  23. // pin the array (don't let the gc move it around).
  24. pin_ptr<int> pinned_address = &( a[0] ) ;
  25. int* cpp_pointer = pinned_address ;
  26. sort_and_print( cpp_pointer, SIZE ) ;
  27. }
Last edited by vijayan121 : Nov 27th, 2007 at 9:52 am.
Reply With Quote  
Join Date: Nov 2007
Posts: 14
Reputation: dallaseve is an unknown quantity at this point 
Rep Power: 1
Solved Threads: 0
dallaseve dallaseve is offline Offline
Newbie Poster

Re: Managed and unmanaged code

  #7  
Nov 28th, 2007
Thank you very much, I will go give this a try.
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

DaniWeb C++ Marketplace
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

Thread Tools Display Modes

Similar Threads
Other Threads in the C++ Forum

All times are GMT -4. The time now is 8:41 pm.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC