Thread: Help with Class Library .NET and Classes :0

  1. #1
    Master At Novice
    Join Date
    Oct 2005
    Location
    Cardassia & Canada, eh!
    Posts
    31

    Post Help with Class Library .NET and Classes :0

    Hey there, just wondering if anyone could tell me where I can go to learn about using classes in external files (how to link them) and if anyone has a link to download a sample Windows Forms project using dll's (nothing specific, just a basic idea of how it all works together).

    thanks,

    Rob

    ------> Below added November 27th

    Ok, here's what I'm playing around with:

    <<This is a VC++ Windows Forms .NET project>>

    My CMainDLL.cpp reads:

    Code:
    // This is the main DLL file.
    #include "stdafx.h"
    #include "CMainDLL.h"
    And my CMainDLL.h reads:

    Code:
    // CMainDLL.h
    
    #pragma once
    
    using namespace System;
    
    namespace CMainDLL
    {
    	public __gc class Class1
    	{
    		// TODO: Add your methods for this class here.
    	public:
    		double trouble;
    		double dare;
    	};
    }
    I'm just seeing if I can use (for now until I get better at it) these stupid little things:

    Form1.h reads:

    Code:
    #pragma once
    #using "CMainDLL.dll"
    namespace CMain
    {
    	
    	//using CMainDLL::Class1;
    	using namespace System;
    	using namespace System::ComponentModel;
    	using namespace System::Collections;
    	using namespace System::Windows::Forms;
    	using namespace System::Data;
    	using namespace System::Drawing;
    
    	/// <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 __gc class Form1 : public System::Windows::Forms::Form
    	{	
    	public:
    		Form1(void)
    		{
    			InitializeComponent();
    		}
      
    	protected:
    		void Dispose(Boolean disposing)
    		{
    			if (disposing && components)
    			{
    				components->Dispose();
    			}
    			__super::Dispose(disposing);
    		}
    	private: System::Windows::Forms::Button *  button1;
    
    	private:
    		/// <summary>
    		/// Required designer variable.
    		/// </summary>
    		System::ComponentModel::Container * components;
    
    		/// <summary>
    		/// Required method for Designer support - do not modify
    		/// the contents of this method with the code editor.
    		/// </summary>
    		void InitializeComponent(void)
    		{
    			this->button1 = new System::Windows::Forms::Button();
    			this->SuspendLayout();
    			// 
    			// button1
    			// 
    			this->button1->Location = System::Drawing::Point(112, 216);
    			this->button1->Name = S"button1";
    			this->button1->TabIndex = 0;
    			this->button1->Text = S"button1";
    			this->button1->Click += new System::EventHandler(this, button1_Click);
    			// 
    			// Form1
    			// 
    			this->AutoScaleBaseSize = System::Drawing::Size(5, 13);
    			this->ClientSize = System::Drawing::Size(292, 266);
    			this->Controls->Add(this->button1);
    			this->Name = S"Form1";
    			this->Text = S"Form1";
    			this->ResumeLayout(false);
    
    		}	
    
    
    	private: System::Void button1_Click(System::Object *  sender, System::EventArgs *  e)
    			 {	
    //
    //Something like this, obviously it's not finished, just an example:
    //
    				 CMainDLL::Class1 Rob;
    				 Rob.dare = 23.3;
    
    			 }
    
    	};
    }
    and Form1.cpp reads:
    Code:
    #include "stdafx.h"
    #include "Form1.h"
    #include <windows.h>
    
    using namespace CMain;
    
    int APIENTRY _tWinMain(HINSTANCE hInstance,
                         HINSTANCE hPrevInstance,
                         LPTSTR    lpCmdLine,
                         int       nCmdShow)
    {
    	System::Threading::Thread::CurrentThread->ApartmentState = System::Threading::ApartmentState::STA;
    	Application::Run(new Form1());
    	return 0;
    }
    When I compile my error reads:
    error C2262: 'Rob': cannot be destroyed.

    Seems like a lot of code to go through above...

    Sure would be nice to be able to use the same code over and over instead of having to re-write it every time I add a new .cpp!

    Is there a link somewhere that can teach me how to do this?

    Thanks,

    Rob Sitter.
    Last edited by Robert_Sitter; 11-27-2005 at 10:54 AM. Reason: Not very specific

  2. #2
    Registered User
    Join Date
    Mar 2005
    Location
    Mountaintop, Pa
    Posts
    1,058
    Managed C++ extensions sample of Windows forms calling a dll:

    The DLL
    Code:
    // DLL filename is Sitter.cpp
    // Compile: cl /clr /LD sitter.cpp
    
    #using <mscorlib.dll>
    
    using namespace System;
    
    namespace Sitter
    {
        public __gc class SitterClass
        {
        public:
        
            String* Hello(String* str)
            {
                return String::Concat(S"Hello ",str);
            }
        };
    }
    Windows Form:
    Code:
    #using <mscorlib.dll>
    #include <tchar.h>
    
    #using <System.dll>
    #using <System.Drawing.dll>
    #using <System.Windows.Forms.dll>
    #using <System.Data.dll>
    
    using namespace System;
    using namespace System::Drawing;
    using namespace System::Collections;
    using namespace System::ComponentModel;
    using namespace System::Windows::Forms;
    using namespace System::Data;
    using namespace System::Reflection;
    
    namespace cs1
    {
        public __gc class Form1 : public System::Windows::Forms::Form
        {
            private:
            String *s1;
            Button* button1;
            Label* label1;
            System::ComponentModel::Container* components;
    
            public:
            Form1()
            {
                components = NULL;
                InitializeComponent();
            }
            protected:
            void Dispose( bool disposing )
            {
                if( disposing )
                {
                    if (components != NULL)
                    {
                        components->Dispose();
                    }
                }
                Form::Dispose( disposing );
            }
         private:
            void InitializeComponent()
            {
                button1 = new Button();
                label1 = new Label();
                SuspendLayout();
                button1->Location = Point(48, 96);
                button1->Name = "button1";
                button1->TabIndex = 0;
                button1->Text = "Call DLL";
                button1->Click += new System::EventHandler(this,
                    &Form1::button1_Click);
                label1->Location = Point(184, 96);
                label1->Name = "label1";
                label1->TabIndex = 1;
                AutoScaleBaseSize =  System::Drawing::Size(5, 13);
                ClientSize =  System::Drawing::Size(292, 273);
                Controls->Add(label1);
                Controls->Add(button1);
                Name = "Form1";
                Text = "Form1";
                ResumeLayout(false);
            }
            private:
            void button1_Click(Object* sender, System::EventArgs* e)
            {
                CallDll("Robert Sitter");
                label1->Text = s1;
            }
         private:
          void CallDll(String *inputString)
          {
                // Load our assembly based on the DLL namespace
                // Our DLL must be named Sitter.DLL
                Assembly *SitterAssembly = Assembly::Load("Sitter");
                // Get the type of the object
                Type *SitterType = SitterAssembly->GetType("Sitter.SitterClass");
                // Describe our method
                MethodInfo *mi = SitterType->GetMethod("Hello");    
                // Create an instance using default constructor
                Object *SitterObject = Activator::CreateInstance(SitterType);
                //Create the argument list
                String *args[] = new String*[1]; 
                args[0] = inputString;
                // Invoke the call to the Hello Method
                s1=static_cast<String*>(mi->Invoke(SitterObject,args));
            }
        };
    }
    
    
    int WinMain()
    {
        Application::Run(new cs1::Form1());
        return 0;
    }

  3. #3
    Master At Novice
    Join Date
    Oct 2005
    Location
    Cardassia & Canada, eh!
    Posts
    31
    Great, thanks!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. C++ Library Design
    By CornedBee in forum C++ Programming
    Replies: 2
    Last Post: 09-30-2004, 10:40 AM
  2. im extreamly new help
    By rigo305 in forum C++ Programming
    Replies: 27
    Last Post: 04-23-2004, 11:22 PM
  3. Prime Number Generator... Help !?!!
    By Halo in forum C++ Programming
    Replies: 9
    Last Post: 10-20-2003, 07:26 PM
  4. include question
    By Wanted420 in forum C++ Programming
    Replies: 8
    Last Post: 10-17-2003, 03:49 AM
  5. structure vs class
    By sana in forum C++ Programming
    Replies: 13
    Last Post: 12-02-2002, 07:18 AM