如何从非托管 C++ 代码获取结构化列表值到 C#?
How to get structured list values from unmanaged c++ code to c#?
我正在用 win32 c++ 编写 "ICS/VCS" 文件解析器。
从文件反序列化后,我将事件数据存储在事件结构列表中。
现在我想在 C# 中访问该结构化列表。
为此,我在 DLL 中导出了函数和列表。
我不知道如何在 c# 中访问该列表。
这是我的事件结构:
struct Event {
Event(): Alarms(new list<Alarm>), RecurrenceNo(0), BaseEvent(this) {}
Event(const Event &Base) :
UID(Base.UID),
Organizer(Base.Organizer),
Summary(Base.Summary),
Description(Base.Description),
Attendee(),
Categories(Base.Categories),
DtStamp(Base.DtStamp),
DtStart(Base.DtStart),
DtEnd(Base.DtEnd),
RRule(Base.RRule),
Alarms(Base.Alarms),
AttendeeCounter(Base.AttendeeCounter),
RecurrenceNo(Base.RecurrenceNo)
{
BaseEvent = Base.BaseEvent == (Event *)&Base ? (Event *)&Base : Base.BaseEvent;
}
~Event() {
if (BaseEvent == this)
delete Alarms;
}
operator string() const;
bool HasAlarm(const Date &From, const Date &To);
string UID, Summary, Description, Categories, Attendee[100], Organizer;
int AttendeeCounter=0;
Date DtStamp, DtStart, DtEnd;
Recurrence RRule;
list<Alarm> *Alarms;
unsigned short RecurrenceNo;
Event *BaseEvent;
};
这里是ExportFunction.h
extern list <Event *> ListofAllEvents;
extern "C" __declspec(dllexport) list <Event *> ParseCalendar(std::string FilePath);
这是我要导出的函数
"ExportFunction.cpp"
list <Event *> ParseCalendar(std::string FilePath)
{
const char * InputPath = FilePath.c_str();
ICalendar Calendar(InputPath);
return ListofAllEvents;
}
这是Program.cs
[DllImport("D:\Projects\iCalander\x64\Debug\iCalander.dll", CallingConvention = CallingConvention.Cdecl)]
public static extern IntPtr ParseCalendar(string FilePath);
显示访问冲突错误。这是预期的,因为我没有正确访问。
我是 pInvoke 和封送处理的新手。
如果有人想做同样的事情,请回答我自己的问题以供将来参考。
我按照本教程进行了试验并了解了互操作性。
https://www.red-gate.com/simple-talk/dotnet/net-development/creating-ccli-wrapper/
然后我将事件结构的原型 class 实现到 c++/cli 中。
public ref class iCalendarEvent
{
public:
String ^mUID;
String ^mOrgaizer;
String ^mSummary;
String ^mDescription;
String ^mCategories;
String ^mDtStart;
String ^mDtEnd;
String ^mDtStamp;
cli::array<String ^>^ mAttendee = gcnew cli::array<String ^>(100);
int mAttendeeLength;
};
然后创建了一个 iCalendarEvent 类型的通用列表。然后我将从c++返回的每个事件对象映射到c++/cli的class obj,并将其插入到c++/CLI的通用列表中。
我正在用 win32 c++ 编写 "ICS/VCS" 文件解析器。
从文件反序列化后,我将事件数据存储在事件结构列表中。
现在我想在 C# 中访问该结构化列表。 为此,我在 DLL 中导出了函数和列表。
我不知道如何在 c# 中访问该列表。
这是我的事件结构:
struct Event {
Event(): Alarms(new list<Alarm>), RecurrenceNo(0), BaseEvent(this) {}
Event(const Event &Base) :
UID(Base.UID),
Organizer(Base.Organizer),
Summary(Base.Summary),
Description(Base.Description),
Attendee(),
Categories(Base.Categories),
DtStamp(Base.DtStamp),
DtStart(Base.DtStart),
DtEnd(Base.DtEnd),
RRule(Base.RRule),
Alarms(Base.Alarms),
AttendeeCounter(Base.AttendeeCounter),
RecurrenceNo(Base.RecurrenceNo)
{
BaseEvent = Base.BaseEvent == (Event *)&Base ? (Event *)&Base : Base.BaseEvent;
}
~Event() {
if (BaseEvent == this)
delete Alarms;
}
operator string() const;
bool HasAlarm(const Date &From, const Date &To);
string UID, Summary, Description, Categories, Attendee[100], Organizer;
int AttendeeCounter=0;
Date DtStamp, DtStart, DtEnd;
Recurrence RRule;
list<Alarm> *Alarms;
unsigned short RecurrenceNo;
Event *BaseEvent;
};
这里是ExportFunction.h
extern list <Event *> ListofAllEvents;
extern "C" __declspec(dllexport) list <Event *> ParseCalendar(std::string FilePath);
这是我要导出的函数 "ExportFunction.cpp"
list <Event *> ParseCalendar(std::string FilePath)
{
const char * InputPath = FilePath.c_str();
ICalendar Calendar(InputPath);
return ListofAllEvents;
}
这是Program.cs
[DllImport("D:\Projects\iCalander\x64\Debug\iCalander.dll", CallingConvention = CallingConvention.Cdecl)]
public static extern IntPtr ParseCalendar(string FilePath);
显示访问冲突错误。这是预期的,因为我没有正确访问。
我是 pInvoke 和封送处理的新手。
如果有人想做同样的事情,请回答我自己的问题以供将来参考。
我按照本教程进行了试验并了解了互操作性。
https://www.red-gate.com/simple-talk/dotnet/net-development/creating-ccli-wrapper/
然后我将事件结构的原型 class 实现到 c++/cli 中。
public ref class iCalendarEvent
{
public:
String ^mUID;
String ^mOrgaizer;
String ^mSummary;
String ^mDescription;
String ^mCategories;
String ^mDtStart;
String ^mDtEnd;
String ^mDtStamp;
cli::array<String ^>^ mAttendee = gcnew cli::array<String ^>(100);
int mAttendeeLength;
};
然后创建了一个 iCalendarEvent 类型的通用列表。然后我将从c++返回的每个事件对象映射到c++/cli的class obj,并将其插入到c++/CLI的通用列表中。