正在尝试升级到.NET Framework 4

本文关键字:NET Framework | 更新日期: 2023-09-27 17:59:06

我正在尝试升级到MS.NETFramework4,但遇到dllimport函数错误(请参阅下文)。当代码到达dllimport时,程序将退出,并返回错误代码0x80000003。我尝试过更改它,使输入和输出都是StringBuilder,我还尝试过通过更改字符集(为unicode或ansi)和设置EntryPoint(PathGetArgsA或PathGetArgsW)来设置字符集。此代码在v3.5中运行良好,但在v4中则不然。

    [DllImport("Shlwapi.dll", SetLastError = true, CharSet = CharSet.Auto)] public static extern string PathGetArgs([In] string path);
    [DllImport("Shlwapi.dll", SetLastError = true, CharSet = CharSet.Auto)] public static extern void PathRemoveArgs([In, Out] StringBuilder path);
    public static bool ExtractArguments(string cmdLine, out string filePath, out string fileArgs)
    {
        StringBuilder strCmdLine = new StringBuilder(cmdLine.ToLower().Trim());
        filePath = fileArgs = "";
        if (strCmdLine.Length <= 0)
            throw new ArgumentNullException("cmdLine");

        fileArgs = string.Copy(PathGetArgs(strCmdLine.ToString())); // Error occurs here
        PathRemoveArgs(strCmdLine);
        filePath = string.Copy(strCmdLine.ToString());
        if (!string.IsNullOrEmpty(filePath))
            if (Utils.FileExists(filePath))
                return true;
        return false;
    }

谢谢!

正在尝试升级到.NET Framework 4

我不确定本机dll在PathGetArgs中返回了什么,但是Marshal类可能会有所帮助。

[DllImport("Shlwapi.dll")]
public static extern IntPtr PathGetArgs([In] string path);
fileArgs = System.Runtime.InteropServices.Marshal.PtrToStringAnsi(PathGetArgs(strCmdLine.ToString()));