使用Framework 4.0将结构从c ++传递到用c#编写的LIB [英] Passing a structure from c++ into a LIB written in c# using Framework 4.0

查看:95
本文介绍了使用Framework 4.0将结构从c ++传递到用c#编写的LIB的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对c#了解不多,但我需要创建一个可以从c ++调用的c#lib。  c#中的函数需要接收数据结构。 我的代码如下:

I don't know much about c#, but I need to create a c# lib that can be called from c++.  The functions in c# need to receive a structure of data.  My code is listed below:

c ++  (LIB包含在项目中)

c++  (LIB is included in project)

    struct Name

     {

         char FileName [100];

         char FileVersion [100];

    };

    名称;



    名称和NBSP; szFile;

    struct Name
    {
        char FileName[100];
        char FileVersion[100];
    };
    Name;

    Name  szFile;

    VisionScape Vs;

    Vs.LoadCameraWithConfigFile(szFile);

    VisionScape Vs;
    Vs.LoadCameraWithConfigFile( szFile);

c#LIB

使用System;

使用System.Collections.Generic;
$
使用System.Linq;

使用System.Text;

使用System.Threading.Tasks;

使用System.Runtime.InteropServices;



命名空间MicroScanFrontRunner

{

    公共课VisionScape

    {

        [StructLayout(LayoutKind.Sequential,CharSet = CharSet.Ansi)]

        public struct名称

        {

            [MarshalAs(UnmanagedType.ByValTStr,SizeConst = 100)]

           公共字符串FileName;

            [MarshalAs(UnmanagedType.ByValTStr,SizeConst = 100)]

            public string FileVersion;

        }


        public int LoadCameraWithConfigFile(Name sName)

        {

            int值;

           值= 2;

           返回值;

        }
    }
}

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Runtime.InteropServices;

namespace MicroScanFrontRunner
{
    public class VisionScape
    {
        [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Ansi)]
        public struct Name
        {
            [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 100)]
            public string FileName;
            [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 100)]
            public string FileVersion;
        };

        public int LoadCameraWithConfigFile( Name sName )
        {
            int value;
            value = 2;
            return value;
        }
    }
}

我收到以下编译错误:

严重性    代码    描述    项目    文件    线    抑制状态

错误     C2664    'int MicroScanFrontRunner :: VisionScape :: LoadCameraWithConfigFile(MicroScanFrontRunner :: VisionScape :: Name)':无法将参数1从'InitializeScanner :: Name'转换为'MicroScanFrontRunner :: VisionScape :: Name'  
  MicroScan     C:\ Users\efthss \Desktop \Plant Moves \MicroScan Vision \C dll \MicroScanCamreaGige \ MicroScan.cpp     100    

Severity    Code    Description    Project    File    Line    Suppression State
Error    C2664    'int MicroScanFrontRunner::VisionScape::LoadCameraWithConfigFile(MicroScanFrontRunner::VisionScape::Name)': cannot convert argument 1 from 'InitializeScanner::Name' to 'MicroScanFrontRunner::VisionScape::Name'    MicroScan    C:\Users\efthss\Desktop\Plant Moves\MicroScan Vision\C dll\MicroScanCamreaGige\MicroScan.cpp    100    

推荐答案

您无法从C ++代码创建C#类。这不是受支持的情况。除非你使用C ++ / CLI,否则你也不能走另一条路。你的选择有限。第一个选项是在C#代码中实现COM接口,然后在C ++应用程序中将代码加载
作为COM。这就像使用C#类一样接近。另一种方法是定义您的本机代码调用的可以指向托管函数的回调。

You cannot create C# classes from C++ code. This is not a supported situation. Nor can you go the other way unless you're using C++/CLI. You have limited options. The first option is to implement a COM interface in your C# code and then load the code in your C++ app as COM. This is as close to using a C# class as you can get. The alternative approach is to define callbacks that your native code calls that can point to a managed function.

另请注意,您不能简单地在C ++代码中加载C#程序集。必须运行CLR才能支持托管代码。听起来你需要在这里使用COM。请注意,您可以在托管进程中加载​​本机DLL,这支持
,但您可以采用另一种方式。

Also note that you cannot simply load a C# assembly in C++ code. The CLR has to be running to support managed code. It really sounds like you need to be using COM here. Note that you can load native DLLs inside a managed process and this is supported but you're going the other way here.


这篇关于使用Framework 4.0将结构从c ++传递到用c#编写的LIB的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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