如何在c ++中返回两个不同的变量? [英] How to return two different variable in c++?

查看:244
本文介绍了如何在c ++中返回两个不同的变量?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

$ p $ d code code $ c

$ $ $ $ $ $ $ $ $ $ $ $ $
{
return x;
}
}

在C#

  [DllImport(example.dll,CallingConvention = CallingConvention.Cdecl)] 
public static extern int mainfun();

我只知道如何从C ++返回并调用一个变量到C#。我正在编写一个程序,我需要从C ++ dll
like return x,y;)中调用C#中的两个不同的变量。请求我需要帮助。



EDIT1:



在C ++中

  struct Point 
{
int count_err;
int语句;
} obj
externC
{
__declspec(dllexport)Point mainfun()
{
return obj;
}
}

在C#

  [StructLayout(LayoutKind.Sequential)] 
public struct Point
{
public int errsize;
public int statmnt;
}
[DllImport(example.dll,CallingConvention = CallingConvention.Cdecl)]
public static extern Point mainfun();

errsize = mainfun();
statmnt = mainFun();

这里errsize发出错误 - errsize在当前上下文中不存在 .. 该怎么办?



EDIT2:



在C#中

  total_errors.Text = p.errsize.ToString(); 

给出相同的错误 - 当前的名称p不存在。上下文

解决方案

定义新的数据结构或数组,这样:



C ++:

  struct Point 
{
int count_err;
int语句;
} obj;
externC
{
__declspec(dllexport)Point mainfun()
{
return obj;
}
}

C#:

  [StructLayout(LayoutKind.Sequential)] 
public struct Point
{
public int errsize;
public int statmnt;
}
[DllImport(example.dll,CallingConvention = CallingConvention.Cdecl)]
public static extern Point mainfun();

Point p = mainfun();
var errsize = p.errsize;
var statmnt = p.statmnt;


dll C++

extern "C"  
{
     __declspec(dllexport) int mainfun()
     {
         return x;
     }
}

In C#

[DllImport("example.dll", CallingConvention = CallingConvention.Cdecl)]
public static extern int mainfun();

I only know how to return and call one variable from C++ to C#. I am writing a program where i need to call two different varables in C# from c++ dll (like return x,y;). Please i need help.

EDIT1:

In C++

struct Point
{
    int count_err;
    int statement;
} obj;
extern "C"  
{
     __declspec(dllexport) Point mainfun()
     {
         return obj;
     }
}

In C#

[StructLayout(LayoutKind.Sequential)] 
public struct Point 
{ 
   public int errsize; 
   public int statmnt; 
} 
[DllImport("example.dll", CallingConvention = CallingConvention.Cdecl)]    
public static extern Point mainfun();

errsize = mainfun();
statmnt = mainfun();

Here errsize is giving an error-"the name 'errsize' does not exist in the current context".. What to do?

EDIT2:

In C#

total_errors.Text = p.errsize.ToString();

giving same error-"the name 'p' does not exist in the current." context"

解决方案

Define new struct or array of data. Something like this:

C++:

struct Point
{
    int count_err;
    int statement;
} obj;
extern "C"  
{
     __declspec(dllexport) Point mainfun()
     {
         return obj;
     }
}

C#:

[StructLayout(LayoutKind.Sequential)] 
public struct Point 
{ 
   public int errsize; 
   public int statmnt; 
} 
[DllImport("example.dll", CallingConvention = CallingConvention.Cdecl)]    
public static extern Point mainfun();

Point p = mainfun();
var errsize = p.errsize;
var statmnt = p.statmnt;

这篇关于如何在c ++中返回两个不同的变量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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