将结构数组从 C#(.NET Core) 传递到 C++(unamnaged)

Passing Array of Structures from C#(.NET Core) to C++(unamnaged)

本文关键字:unamnaged C++ NET 数组 结构 Core      更新时间:2023-10-16

所以我在网上阅读了文档和无数的例子,如何封送结构数组。我已经编组了 int 数组,我编组了结构,但现在我完全陷入困境,无论我尝试什么都无法让它工作。已经卡在上面一天多了。

结构/类,尝试两者兼而有之

[StructLayout(LayoutKind.Sequential,CharSet = CharSet.Unicode)]
public class SaveDetails
{
[MarshalAs(UnmanagedType.LPWStr)]
public string Log;
public FILETIME FileTime;
[MarshalAs(UnmanagedType.Bool)]
public bool Saved;
}

平唤和呼叫委托

public class LogSaveFiles : IDisposable
{
[UnmanagedFunctionPointer(CallingConvention.Winapi,CharSet = CharSet.Unicode)]
private delegate Status DLogSaveFiles([ In, Out] SaveDetails[] logsToSave, string destinationPath);
private static DLogSaveFiles _dLogSaveFiles;
private IntPtr PLogSaveFiles { get; set; }
public bool LogSaveFilesAvailable => PLogSaveFiles != IntPtr.Zero;
public LogSaveFiles(Importer importer)
{
if (importer.dllLibraryPtr!= IntPtr.Zero)
{
PLogSaveFiles = Importer.GetProcAddress(importer.dllLibrary, "LogSaveFiles");
}
}
public Status SaveFiles(SaveDetails[] logsToSave,string destinationPath)
{
Status result = Status.FunctionNotAvailable;
if (LogSaveFilesAvailable)
{
_dLogSaveFiles = (DLogSaveFiles)Marshal.GetDelegateForFunctionPointer(PLogSaveFiles, typeof(DLogSaveFiles));
result = _dLogSaveFiles(logsToSave, destinationPath);
}
return result;
}
public void Dispose()
{
}
}

private void SaveLogs()
{
var logsToSave = new[]{
new SaveDetails{
FileTime = new FILETIME {dwHighDateTime = 3,dwLowDateTime = 5},
Log = LogTypes.logDeviceLog,
Saved = true},
new SaveDetails{
FileTime = new FILETIME {dwHighDateTime = 1,dwLowDateTime = 2},
Log = LogTypes.logDeviceLog,
Saved = false}
};
var pathToSave = "C:\Logs";
_logSaveFiles.SaveFiles(logsToSave, pathToSave);
}

C++ 公开调用

typedef struct _LOG_SAVE_DETAILS
{
LPTSTR      szLog;
FILETIME    fromFileTime;
BOOL        bSaved;
} LOG_SAVE_DETAILS, *PLOG_SAVE_DETAILS;

/* Function definitions */
ULY_STATUS _API LogSaveFiles (PLOG_SAVE_DETAILS   ppLogs [],
LPCTSTR                szDestinationPath);

到达目标的路径会正确传递,但结构数组永远不会通过,从而导致在尝试访问它时出现访问冲突。起初,我认为这是 LPTSTR 无法正确通过的问题,但我已经自行实现了其他调用并成功编组了它。

我已经阅读了 https://learn.microsoft.com/en-us/dotnet/framework/interop/marshaling-data-with-platform-invoke 上的所有内容,这一切都表明我的方法是正确的,但它不起作用。

任何帮助,不胜感激。

简单的解决方案:C 端将PLOG_SAVE_DETAILS ppLogs []更改为LOG_SAVE_DETAILS ppLogs [],然后将 C# 端public class SaveDetails更改为public struct SaveDetails

封送对象数组似乎很困难(我无法做到(。封送结构数组有效。另一种方法是手动进行封送处理,但这很痛苦。

手动封送处理的"痛苦"(仅修改代码行(:

[UnmanagedFunctionPointer(CallingConvention.Winapi, CharSet = CharSet.Unicode)]
private delegate Status DLogSaveFiles(IntPtr[] logsToSave, string destinationPath);

然后

public Status SaveFiles(SaveDetails[] logsToSave, string destinationPath)
{
Status result = Status.FunctionNotAvailable;
if (LogSaveFilesAvailable)
{
if (_dLogSaveFiles == null)
{
_dLogSaveFiles = (DLogSaveFiles)Marshal.GetDelegateForFunctionPointer(PLogSaveFiles, typeof(DLogSaveFiles));
}
int size = Marshal.SizeOf(typeof(SaveDetails));
IntPtr basePtr = IntPtr.Zero;
IntPtr[] ptrs = new IntPtr[logsToSave.Length + 1];
try
{
basePtr = Marshal.AllocHGlobal(size * logsToSave.Length);
for (int i = 0; i < logsToSave.Length; i++)
{
ptrs[i] = IntPtr.Add(basePtr, (i * size));
Marshal.StructureToPtr(logsToSave[i], ptrs[i], false);
}
result = _dLogSaveFiles(ptrs, destinationPath);
}
finally
{
if (basePtr != IntPtr.Zero)
{
for (int i = 0; i < logsToSave.Length; i++)
{
if (ptrs[i] != IntPtr.Zero)
{
Marshal.DestroyStructure(ptrs[i], typeof(SaveDetails));
}
}
Marshal.FreeHGlobal(basePtr);
}
}
}
return result;
}

重要提示:这是一个封送拆收器 C#->C++。C++不得以任何方式修改收到的数组,否则会出现内存泄漏。