将VC ++ 2008 Dll与C#.Net 2008 Exe链接 [英] Linking VC++ 2008 Dll with C#.Net 2008 Exe

查看:75
本文介绍了将VC ++ 2008 Dll与C#.Net 2008 Exe链接的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经在C#.NET 2008中创建了一个小应用程序,我正在其中尝试使用VC ++ .NET 2008 Dll.

在Dll中,创建了一个类,其中添加了一个函数和一个变量.
在C#,Window窗体应用程序中,我添加了VC Dll参考.

现在我可以创建Dll的对象,但是无法访问它的函数或变量.
它给出错误消息为-

"____不包含____的定义,并且找不到扩展方法____接受类型为_____的第一个参数(您是否缺少using指令或程序集引用)"


在此处发布我的代码段
-------------------------------------------------- -------------
VC 2008代码-

I have created a small application in C# .NET 2008, in which i am trying to use VC++ .NET 2008 Dll.

In Dll, one class is created, in that one function and one variable are added.
In C#, Window form application, i have added VC Dll reference.

Now i can create object of Dll, but i am unable to access it function or variable.
It give error message as -

"____ does not contain a definition for ____ and no extension method ____ accepting a first argument of type _____ could be found (are you missing a using directive or an assembly reference)"


Posting here my code snippet
---------------------------------------------------------------
VC 2008 code -

#include "stdafx.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#endif

namespace DllNameSp
{       
    public class Test
    {
    public:         
        int abc;
        int iTest()
        {
            int z=0,x=0,y=0;
            return z=x+y;
        }
    };
}


-------------------------------------------------- -----
C#代码


-------------------------------------------------------
C# code

        private void Form1_Load(object sender, EventArgs e)
{
    DllNameSp.Test obj1 = new DllNameSp.Test();
    obj1.abc = 90;
    int k = obj1.iTest();   
}

推荐答案

您应在混合模式C ++程序集中创建托管对象.

public ref class Test.

C#将无法直接调用非托管代码.推荐的方法是任何公共类都被管理.

如果要混合使用托管代码和非托管代码,还有很多要学习的东西.特别是,您将必须学习构造函数,析构函数和终结器的工作方式,并且如果需要保持托管代码的话,gcroot helper类也可能很有用.在非托管类中处理.

您可能还想知道C ++中提供了3种clr编译模式.默认值是通常用于混合代码的内容.这也是一种更严格的纯净和安全模式.
You should create managed object in your mixed-mode C++ assembly.

That is, public ref class Test.

C# will not be able to call unmanaged code directly. The recommanded way is that any public class are managed one.

There is a lot more to learn if you want to mix managed and unmanaged code... Particulary, you will have to learn how constructors, destructors and finalizers works and also gcroot helper class could be usefull if you need to keep a managed handle inside an unmanaged class.

You might also want to known that there are 3 clr compilation mode available in C++. The default one is what you will generally used for mixed code. The is also a pure and a safe mode that are more restrictive.


您不能在不通过DllImport属性定义它们的情况下调用C ++函数,如本P/调用教程 [
You cannot call C++ functions without defining them via the DllImport attribute as described in this P/Invoke tutorial[^].
My mistake, I misread the original question.


这篇关于将VC ++ 2008 Dll与C#.Net 2008 Exe链接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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