ICode9

精准搜索请尝试: 精确搜索
首页 > 编程语言> 文章详细

如何将类从C代码返回到C#

2019-11-20 20:06:44  阅读:190  来源: 互联网

标签:c-cli dllimport c


我从C#代码调用C方法.除了将多个参数返回给C#之外,一切工作正常.

在我的情况下,这些参数是:int x,y,width,height;

我要做的是将类或结构从C代码返回到C#.

我提供了一个示例,因此我想的更加清楚.我知道一种方法是使用元帅课程,也许是唯一的方法.

C#代码

public class ImageMatch
{
    //method that is used to call pass string parameters and call c++ method
    [System.Runtime.InteropServices.DllImport("ImageComputingWrapper.dll", CallingConvention = System.Runtime.InteropServices.CallingConvention.Cdecl)]
    static extern ImageComputingWrapper.ImageParams ComputeImage(string imgPath, string templPath);

    public  GetImgParams(string imgPath, string templPath)
    {
        //a class from C++ code
        ImageComputingWrapper.ImageParams imgParams;
        //retreive all the data
        imgParams = ComputeImage(imgPath, templPath);
    }
}

C代码

//ImageComputingWrapper.cpp
extern "C" __declspec(dllexport) ImageComputingWrapper::ImageParams ComputeImage(const char* imgPath, const char* templPath)
{
    computeImage* compImage = new computeImage(imgPath, templPath);
    ImageComputingWrapper::ImageParams imageParams;

    imageParams.x = compImage->x;
    imageParams.y = compImage->y;
    imageParams.width = compImage->width;
    imageParams.height = compImage->height;

    return imageParams;
}

//ImageComputingWrapper.h
//class to return back to c#
public ref class ImageParams
{
    public:
        ImageParams(){}
        int x;
        int y;
        int width;
        int height;
};

我确实知道不可能像本例中那样将类从C代码返回到C#.只是为了轻松理解我的意思.

有一点要指出,我是C#程序员,所以C代码(指针)中可能有问题.

解决方法:

您不能使用p / invoke返回引用类.您可以做的是在C/C++LI程序集中声明一个ref类,然后从C#中简单地使用它.

首先,您需要一个C/C++LI类库.例如:

// ClassLibrary1.h

#pragma once

using namespace System;

namespace ClassLibrary1 
{
    public ref class Class1
    {
    public:
        int x;
        int y;
        int width;
        int height;
    public:
        Class1() : x(42), y(666), width(24), height(13) {}
    };
}

然后,您可以像使用其他任何托管程序集一样使用此类库:

using System;
using ClassLibrary1;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            Class1 instance = new Class1();
            Console.WriteLine(instance.x);
            Console.WriteLine(instance.y);
            Console.WriteLine(instance.width);
            Console.WriteLine(instance.height);
        }
    }
}

这就是全部.

您在注释中询问如何将字符串参数传递给C/C++LI代码.在C/C++LI端使用System :: String ^.这是引用.net字符串类型的C/C++LI方法.因此,您的构造函数可能变为:

public ref class Class1
{
....
public:
    Class1(System::String^ imgPath, System::String^ tempPath)
    {
        ....
    }
};

在C#端,您将创建如下实例:

string imgPath = "...";
string tempPath = "...";
Class1 instance = new Class1(imgPath, tempPath);

标签:c-cli,dllimport,c
来源: https://codeday.me/bug/20191120/2046561.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有