在C#应用程序中接收到编组结构后不久,堆损坏

时间:2018-10-26 19:49:51

标签: c# pinvoke marshalling projfs

我正在尝试用C#包装新的Windows 10 ProjFS功能。第一步as outlined on MSDN运行正常:我将目录设置为虚拟化根目录,然后注册我的投影提供程序,包括其回调,如下所示:

static void Main(string[] args)
{
    // Create and mark clean directory as virtualization root.
    const string directory = @"C:\test_projfs"; // Warning: Deleted to get clean directory.
    if (Directory.Exists(directory))
        Directory.Delete(directory);
    Directory.CreateDirectory(directory);

    Guid guid = Guid.NewGuid();
    Marshal.ThrowExceptionForHR(PrjMarkDirectoryAsPlaceholder(directory, null, IntPtr.Zero,
        ref guid));

    // Set up the callback table for the projection provider.
    PrjCallbacks callbackTable = new PrjCallbacks
    {
        StartDirectoryEnumerationCallback = StartDirectoryEnumerationCallback,
        EndDirectoryEnumerationCallback = EndDirectoryEnumerationCallback,
        GetDirectoryEnumerationCallback = GetDirectoryEnumerationCallback,
        GetPlaceholderInfoCallback = GetPlaceholderInfoCallback,
        GetFileDataCallback = GetFileDataCallback
    };
    // Start the projection provider.
    IntPtr instanceHandle = IntPtr.Zero;
    Marshal.ThrowExceptionForHR(PrjStartVirtualizing(directory, ref callbackTable,
        IntPtr.Zero, IntPtr.Zero, ref instanceHandle));

    // Keep a test console application running.
    Console.ReadLine();
}

// Managed callbacks, simply returning S_OK for now.
static int StartDirectoryEnumerationCallback(ref PrjCallbackData callbackData, ref Guid enumerationId)
{
    return 0;
}
static int EndDirectoryEnumerationCallback(ref PrjCallbackData callbackData, ref Guid enumerationId) => 0;
static int GetDirectoryEnumerationCallback(ref PrjCallbackData callbackData, ref Guid enumerationId, string searchExpression, IntPtr dirEntryBufferHandle) => 0;
static int GetPlaceholderInfoCallback(ref PrjCallbackData callbackData) => 0;
static int GetFileDataCallback(ref PrjCallbackData callbackData, ulong byteOffset, uint length) => 0;

本机声明如下(很抱歉,此代码块可以滚动的事实-基础知识已经包装了很多):

// Methods to mark directory as virtualization root, and start the projection provider.
[DllImport("ProjectedFSLib.dll", CharSet = CharSet.Unicode)]
static extern int PrjMarkDirectoryAsPlaceholder(string rootPathName,
    string targetPathName, IntPtr versionInfo, ref Guid virtualizationInstanceID);
[DllImport("ProjectedFSLib.dll", CharSet = CharSet.Unicode)]
static extern int PrjStartVirtualizing(string virtualizationRootPath,
    ref PrjCallbacks callbacks, IntPtr instanceContext, IntPtr options,
    ref IntPtr namespaceVirtualizationContext);

// Structure configuring the projection provider callbacks.
[StructLayout(LayoutKind.Sequential)]
struct PrjCallbacks
{
    public PrjStartDirectoryEnumerationCb StartDirectoryEnumerationCallback;
    public PrjEndDirectoryEnumerationCb EndDirectoryEnumerationCallback;
    public PrjGetDirectoryEnumerationCb GetDirectoryEnumerationCallback;
    public PrjGetPlaceholderInfoCb GetPlaceholderInfoCallback;
    public PrjGetFileDataCb GetFileDataCallback;
    public PrjQueryFileNameCb QueryFileNameCallback;
    public PrjNotificationCb NotificationCallback;
    public PrjCancelCommandCb CancelCommandCallback;
}

// Callback signatures.
delegate int PrjStartDirectoryEnumerationCb(ref PrjCallbackData callbackData, ref Guid enumerationId);
delegate int PrjEndDirectoryEnumerationCb(ref PrjCallbackData callbackData, ref Guid enumerationId);
delegate int PrjGetDirectoryEnumerationCb(ref PrjCallbackData callbackData, ref Guid enumerationId, string searchExpression, IntPtr dirEntryBufferHandle);
delegate int PrjGetPlaceholderInfoCb(ref PrjCallbackData callbackData);
delegate int PrjGetFileDataCb(ref PrjCallbackData callbackData, ulong byteOffset, uint length);
delegate int PrjQueryFileNameCb(ref PrjCallbackData callbackData);
delegate int PrjNotificationCb(ref PrjCallbackData callbackData, bool isDirectory, int notification, string destinationFileName, IntPtr operationParameters);
delegate int PrjCancelCommandCb(ref PrjCallbackData callbackData);

// Callback data passed to each of the callbacks above.
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)]
struct PrjCallbackData
{
    public uint Size;
    public uint Flags;
    public IntPtr NamespaceVirtualizationContext;
    public int CommandId;
    public Guid FileId;
    public Guid DataStreamId;
    public string FilePathName;
    public IntPtr VersionInfo;
    public uint TriggeringProcessId;
    public string TriggeringProcessImageFileName;
    public IntPtr InstanceContext;
}

但是,在整理从操作系统传递到回调的结构时,我似乎做错了,因为它导致托管应用立即停止运行而没有托管异常,并且仅在Windows事件中记录堆损坏错误代码查看器。

详细地,我收到了PRJ_START_DIRECTORY_ENUMERATION_CB回调(代码中的PrjStartDirectoryEnumerationCb)来开始枚举目录。它将指针指向PRJ_CALLBACK_DATA结构(在我的代码中为PrjCallbackData)。

现在,虽然我显然在托管回调中很好地接收了该结构,但所有值对最后一个成员InstanceContext都是有意义的,但尝试返回值0(S_OK时,应用程序立即崩溃)。

static int StartDirectoryEnumerationCallback(ref PrjCallbackData callbackData, ref Guid enumerationId)
{
    return 0; // Crashes here or when stepping out of this method.
}

我试图确定错误的出处,但是由于调试立即停止而没有任何异常(我没有过滤掉任何东西),所以我没有走得很远。我意识到,当我将回调更改为愚蠢地接受IntPtr而不是ref PrjCallbackData时,应用程序不会崩溃。

static int StartDirectoryEnumerationCallback(IntPtr callbackData, ref Guid enumerationId)
{
    return 0; // No crash executing this with IntPtr passed in.
}

delegate int PrjStartDirectoryEnumerationCb(IntPtr callbackData, ref Guid enumerationId);

从逻辑上讲,如果没有我可以访问的重要信息,这并不会使我走远。

我在这里错过了一步吗?像这样简单地映射结构不是直接不可能的吗?

如果感兴趣,将显示事件查看器条目。我正在新样式的C#项目文件(应解释“ dotnet.exe”可执行文件名称)中使用.NET Framework 4.6运行该应用程序。如果需要其他信息,我很乐意提供。

Faulting application name: dotnet.exe, version: 2.1.26919.1, time stamp: 0x5ba1bb46
Faulting module name: ntdll.dll, version: 10.0.17763.1, time stamp: 0xa369e897
Exception code: 0xc0000374
Fault offset: 0x00000000000fb349
Faulting process id: 0xfa8
Faulting application start time: 0x01d46d623aee076d
Faulting application path: C:\Program Files\dotnet\dotnet.exe
Faulting module path: C:\WINDOWS\SYSTEM32\ntdll.dll
Report Id: adcfba5c-dfd4-428d-8eb5-81aceada1983
Faulting package full name: 
Faulting package-relative application ID: 

请注意,如果您想尝试上面的示例代码,则必须在Windows 10 1809中安装Projected Filesystem功能,然后将其编译为x64(没有用于x86 / AnyCPU配置的本机库)。

0 个答案:

没有答案