我正在尝试一个小的互操作应用程序,其中我从C#调用了一些C++方法。我有一个非常基本的例子,用于调用一个方法并返回一个整数,这很好。将C++数组返回到C#
InteropApp.h
#ifdef DLLFUNCTIONEXPOSETEST_EXPORTS
#define DLLFUNCTIONEXPOSETEST_API __declspec(dllexport)
#else
#define DLLFUNCTIONEXPOSETEST_API __declspec(dllimport)
#endif
extern "C" DLLFUNCTIONEXPOSETEST_API int fnSumofTwoDigits(int a, int b);
InteropApp.cpp
#include "stdafx.h"
#include "DLLFunctionExposeTest.h"
BOOL APIENTRY DllMain(HMODULE hModule,
DWORD ul_reason_for_call,
LPVOID lpReserved
)
{
return TRUE;
}
DLLFUNCTIONEXPOSETEST_API int fnSumofTwoDigits(int a, int b)
{
return a + b;
}
C#InteropAppTest
static class TestImport
{
[DllImport("DLLFunctionExposeTest.dll", CallingConvention = CallingConvention.Cdecl, EntryPoint = "fnSumofTwoDigits")]
public static extern int fnSumofTwoDigits(int a, int b);
}
public partial class MainWindow : Window
{
public MainWindow()
{
try
{
InitializeComponent();
int g = TestImport.fnSumofTwoDigits(2, 1);
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
}
}
}
在AB Ove C++应用程序,我想要一种方法返回一个strings
的数组,另一种方法返回一个integers
的数组。但是,正如我读过的一样,如果这涉及编组,我很困惑。方法原型对于C++和C#都会是什么样子?在C#应用程序中还会改变什么来调用C++数组返回函数?
对于上面的问题,很容易得到一个简单的例子,因为我无法找到任何简单的例子来开始。
你有什么试过的?你的意思是什么类型? 'std :: string'(或'char *'?)和'int'?或者没有关系? – svick 2012-02-18 10:22:44
@svick:我宁愿以'char *'和'int'数组返回方法开始。 – Cipher 2012-02-18 10:37:27