Click here to Skip to main content
15,890,527 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
Dear all,
i tried to create a dll (with using c++/cli)and for this i did -
choose VC++ template ->CLR-> and choose Class Library for making a simple dll with a method sum. code is look like:- Header file code is:-
C++
// dll_for_sum.h

#pragma once

using namespace System;

namespace dll_for_sum {

	public ref class Class1
	{
		// TODO: Add your methods for this class here.
	public:
		int sum( int x, int y );
	
	};
}

.cpp code is :-
C++
// This is the main DLL file.

#include "stdafx.h"

#include "dll_for_sum.h"

int dll_for_sum::Class1::sum(int x, int y)
{
	return x+y;
}

when i build it , it successfully build, here i get one dll_for_sum.dll in debug folder. now i want to use it in -visual studio -> new->choose and expand visual c++ template ->choose CLR -> *Window form application*
i want to call sum function on a button_click event.
Could any body tell me what files ( only .dll or .dll + dll_for_sum.h ) i need to paste in window application route folder to call sum function??????????

when i wondered i found - CLR->CLass Library is of type c++/cli managed code so we only need .dll file to call it, here i set the ref of .dll in window application by using following step:-
For references i go ->Properties ->references->framework & references ->add new references ->then browse and choose .dll file.
after this on_button_click area I WROTE:-
C++
private: System::Void button1_Click(System::Object^  sender, System::EventArgs^  e) {
                Class1 obj;
                obj.sum(2,3);
            }
   };
or
Perhaps object is created by differently in c++/CLI so i wrote:-
C++
Class1^ objectt = gcnew Class1();
				 
objectt->sum(10,12);

here in both cases i got the error like :-
1>c:\documents and settings\administrator\my documents\visual studio 2010\projects\call_sum_method\call_sum_method\Form1.h(81): error C2065: 'Class1' : undeclared identifier
1>c:\documents and settings\administrator\my documents\visual studio 2010\projects\call_sum_method\call_sum_method\Form1.h(81): error C2065: 'objectt' : undeclared identifier
1>c:\documents and settings\administrator\my documents\visual studio 2010\projects\call_sum_method\call_sum_method\Form1.h(81): error C2061: syntax error : identifier 'Class1'
1>c:\documents and settings\administrator\my documents\visual studio 2010\projects\call_sum_method\call_sum_method\Form1.h(82): error C2065: 'objectt' : undeclared identifier
1>c:\documents and settings\administrator\my documents\visual studio 2010\projects\call_sum_method\call_sum_method\Form1.h(82): error C2227: left of '->sum' must point to class/struct/union/generic type

Give me some way to come out with this problem
Posted

1 solution

Finally i get the solution. Hurray!!!!
I Asked
Could any body tell me what files ( only .dll or .dll + dll_for_sum.h ) i need to paste in window application route folder to call sum function??????????
My ans-
only .dll file require here, and what u need to do is to reference it in the project by following way:-

Step -1 :- For references i go ->Properties ->references->framework & references ->add new references ->then browse and choose .dll file.

Step-2 go solution explorer -> header file -> stdafx.h and the write
VB
// TODO: reference additional headers your program requires here

using namespace dll_for_sum;


step -3
on a button click write following to call sum function:-
C++
private: System::Void button1_Click(System::Object^  sender, System::EventArgs^  e) {
                                  
Class1 obj;				
 int sum = obj.sum(100,300);				
 String ^s = Convert::ToString(sum);			
label1->Text = s;
}

or you can also write this;-
C++
private: System::Void button1_Click(System::Object^  sender, System::EventArgs^  e) {
                                
Class1 ^ obj = gcnew Class1; 				
int sum = obj->sum(10,10);				
String ^s = Convert::ToString(sum);				
label1->Text = s;
}
 
Share this answer
 
v2

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900