我想从我的C#代码中调用这个C ++函数:
void GetArrayOfNames(char** names, int nbOfNames);
要在C ++中调用它,我只需定义一个char *:
char* aNames[20];
并在循环中分配每个名称:
for(int i-0; i<20; i++)
{
aNames[i] = new char[50];
}
然后致电:
GetArrayOfNames(aNames, 20);
在我的C#代码中,我有:
[DllImport("MyDLL.dll")]
unsafe static extern void GetArrayOfNames(char** ppNames, int nbOfNames);
现在,我如何进行内存分配并调用GetArrayOfNames?另外,任何方式都不必将我的功能声明为“不安全”?
答案 0 :(得分:2)
大概:
static extern void GetArrayOfNames([MarshalAs(UnmanagedType.LPArray, ArraySubType=UnmanagedType.LPStr] StringBuilder[] args, int count); // Call using StringBuilder[] arr = new StringBuilder[20]; for (int i = 0; i < arr.Length; i++) { arr[i] = new StringBuilder(50); } GetArrayOfNames(arr, arr.Length);
答案 1 :(得分:1)
据我所知char**
只是对字符串的引用。我为自己创建了一个小测试,它运行良好:
class Program
{
[DllImport("TestCppLib.dll", CharSet = CharSet.Ansi, EntryPoint = "?fnTestCppLib@@YAHPAPAD@Z", CallingConvention=CallingConvention.Cdecl)]
extern static int fnTestCppLib(ref string s);
static void Main(string[] args)
{
var s = "some";
var t = fnTestCppLib(ref s);
Debug.Assert(s == "test");
}
}
C ++中函数的实现是:
TESTCPPLIB_API int fnTestCppLib(char ** str)
{
*str = "test";
return 42;
}
答案 2 :(得分:0)
这最终起作用了:
static extern int GetArrayOfNames(IntPtr[] astr, int iLength);
并像这样调用/ setup:
IntPtr[] intArr = new IntPtr[200];
for (int i = 0; i < intArr.Length; i++)
{
intArr[i] = Marshal.AllocHGlobal(256);
}
int nbOfNames = 2;
GetArrayOfNames(intArr, nbOfNames);
然后放回一个字符串:
string tmp;
tmp = Marshal.PtrToStringAnsi(intArr[i]);
答案 3 :(得分:0)
我可以帮你吗?我在C ++中的示例代码:
//CamMethods.cpp:
#include <iostream>
using namespace std;
#include "CamPS.h"
namespace Export {
char * CameraPS::CamStart(char *s){
return "teste";
};
}
//CamPs.h
namespace Export{
class CameraPS
{
public:
_declspec(dllexport) char * _stdcall CamStart(char *s);
};
}
并在C#中致电:
using UnityEngine;
using System;
using System.Collections;
using System.Runtime.InteropServices;
public class DllTeste : MonoBehaviour
{
public int x;
[DllImport ("PS_Cam.dll", EntryPoint="CamStart", CallingConvention = CallingConvention.StdCall)]
private static extern IntPtr CamStart (int s);//i can put <string> in same place of <int>
void Start ()
{
}
void Update ()
{
// IntPtr a = CamStart(x);
// string b = Marshal.PtrToStringAnsi(a);
// Debug.Log(b);
Debug.Log(Marshal.PtrToStringAnsi(CamStart(x)));
}
}
抱歉我的英文。