【发布时间】:2020-02-15 09:05:06
【问题描述】:
我只需要知道如何从具有以下结构的 C++ 中的 PInvoke 返回结构。目前我可以处理它是空白的,我只想知道如何在代码中设置的条件下返回结构。
我已经尝试使用我需要返回的整个结构并隔离结构的每个部分以了解哪个部分给我带来了问题(这将在提供的代码中显而易见)。
我尝试了相同的方法,希望在结构中返回几个整数,效果很好。 (尝试使用 ***, ___ 使这个粗体)
//.header file
typedef struct { //Defintion of my struct in C++
TCHAR msg[256];
}testTCHAR;
//.cpp file
extern "C" {
__declspec(dllexport) testTCHAR* (_stdcall TestChar(testTCHAR* AR))
{
AR->msg;
return AR;
}
}
在我的 C# 中,我将 .dll 称为:
[UnmanagedFunctionPointer(CallingConvention.StdCall)]
public delegate void testChar(testTCHAR AR);
[DllImport("C:\\Users\\jch\\source\\repos\\FlatPanelSensor\\x64\\Debug\\VADAV_AcqS.dll", EntryPoint = "TestCallBackChar", CallingConvention = CallingConvention.Cdecl)]
public unsafe static extern testTCHAR TestCallBackChar([MarshalAs(UnmanagedType.FunctionPtr)] testChar call);
//Struct
public struct testTCHAR
{
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 256)]
public string rMsg; //I assume the error should be fixed here
but to what exactly I don't know.
}
//Defining the callback
testChar tChar =
(test) =>
{
//As far as I'm aware this part can be left blank
as I followed a tutorial online
};
testTCHAR returned = TestCallBackChar(tChar); //This is where the error
happens
我只需要返回结构体,最好附上一个值。
我得到的错误是“方法的类型签名与 PInvoke 不兼容。”这在标题中,但我涵盖了所有基础。
如果您需要更多有关这方面的信息,请询问,我应该能够提供。
【问题讨论】: