首页 > 解决方案 > 将字符串文件路径转换为 ​​IntPtr

问题描述

我正在使用Mupen64Plus

我正在使用C#与用Cmupen64plus.dll编写的 API交谈。

我正在尝试加载一个保存状态文件并将String路径转换为一个IntPtr,以便 API 可以读取它。

问题是当我用 Mupen 的M64CMD_STATE_LOAD命令加载它时,它给了我错误Failed to open savestate file (null),这可能意味着字符串没有正确转换并且指针错误。


Mupen64Plus_v2.0_Core_Front-End

M64CMD_STATE_LOAD

此命令将尝试加载已保存的状态文件。如果 ParamPtr 不为 NULL,此函数将从该指针指定的完整路径名加载状态文件。否则(ParamPtr 为 NULL),它将从当前槽加载。


API

我在这里使用这个 API 代码mupen64plusCoreApi.cs

[UnmanagedFunctionPointer(CallingConvention.Cdecl)]
delegate m64p_error CoreDoCommandPtr(m64p_command Command, int ParamInt, IntPtr ParamPtr);
CoreDoCommandPtr m64pCoreDoCommandPtr;

private enum m64p_command
{
    M64CMD_STATE_LOAD,
    ...
}

加载保存状态文件(问题)

// Open Select File Window
Microsoft.Win32.OpenFileDialog selectFile = new Microsoft.Win32.OpenFileDialog();
Nullable<bool> result = selectFile.ShowDialog();

if (result == true)
{
    // Convert String File Path to IntPtr
    string fileName = selectFile.FileName;

    IntPtr filePath = Marshal.StringToHGlobalAnsi(fileName);
    GCHandle gch = GCHandle.Alloc(filePath, GCHandleType.Pinned);
    IntPtr pinnedFilePath = gch.AddrOfPinnedObject();

    // API Load Save State File from Path
    m64pCoreDoCommandPtr(m64p_command.M64CMD_STATE_LOAD, 0, pinnedFilePath); // Problem here

    gch.Free();
}

标签: c#c

解决方案


推荐阅读