如何在Windows窗体应用程序中使用c ++/cli dll(在VC ++模板下的CLR中存在) [英] how to use c++/cli dll in windows form applicaton (present in CLR under VC++ template)

查看:106
本文介绍了如何在Windows窗体应用程序中使用c ++/cli dll(在VC ++模板下的CLR中存在)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

亲爱的所有人,
我试图创建一个dll(使用c ++/cli),为此,我做到了-
选择VC ++模板-> CLR->并选择类库以使用方法sum制作简单的dll.代码如下所示:-头文件代码为:-

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:-

// 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代码为:-


.cpp code is :-

// 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;
}


当我构建它时,它成功构建,在这里我在调试文件夹中得到一个dll_for_sum.dll.现在我想在-visual studio中使用它->新建->选择并扩展可视c ++模板->选择CLR-> *窗口表单应用程序*
我想在button_click事件上调用sum函数.
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??????????

当我想知道我发现-CLR-> CLass库的类型为 c ++/cli托管代码时,所以我们只需要.dll文件来调用它,在这里我通过在Windows应用程序中设置.dll的引用使用以下步骤:-
For references i go ->Properties ->references->framework & references ->add new references ->then browse and choose .dll file.
在这个on_button_click区域之后,我写了:-


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:-

private: System::Void button1_Click(System::Object^  sender, System::EventArgs^  e) {
                Class1 obj;
                obj.sum(2,3);
            }
   };


Perhaps object is created by differently in c++/CLI so i wrote:-

or
Perhaps object is created by differently in c++/CLI so i wrote:-

Class1^ objectt = gcnew Class1();
				 
objectt->sum(10,12);


here in both cases i got the error like :-
1> c:\ documents and settings \ administrator \ mydocuments \ visual studio 2010 \ projects \ call_sum_method \ call_sum_method \ Form1.h(81):错误C2065:"Class1":未声明的标识符
1> c:\ documents and settings \ administrator \ mydocuments \ visual studio 2010 \ projects \ call_sum_method \ call_sum_method \ Form1.h(81):错误C2065:"objectt":未声明的标识符
1> c:\ documents and settings \ administrator \ mydocuments \ visual studio 2010 \ projects \ call_sum_method \ call_sum_method \ Form1.h(81):错误C2061:语法错误:标识符"Class1"
1> c:\ documents and settings \ administrator \ mydocuments \ Visual Studio 2010 \ projects \ call_sum_method \ call_sum_method \ Form1.h(82):错误C2065:"objectt":未声明的标识符
1> c:\ documents and settings \ administrator \ mydocuments \ Visual Studio 2010 \ projects \ call_sum_method \ call_sum_method \ Form1.h(82):错误C2227:-> sum"的左侧必须指向类/结构/联合/通用类型

Give me some way to come out with this problem


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

推荐答案

最后,我得到了解决方案.欢呼!!!!
我问过
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??????????
我的ans-
此处仅需要.dll文件,而您需要做的是通过以下方式在项目中引用它:-

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

步骤2进入解决方案资源管理器->头文件-> stdafx.h和write
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
// TODO: reference additional headers your program requires here

using namespace dll_for_sum;



步骤-3
在按钮上单击写​​以下"以调用求和函数:-



step -3
on a button click write following to call sum function:-

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;-

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;
}


这篇关于如何在Windows窗体应用程序中使用c ++/cli dll(在VC ++模板下的CLR中存在)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆