【问题标题】:Trying to upgrade to .NET Framework 4尝试升级到 .NET Framework 4
【发布时间】:2011-06-11 06:55:36
【问题描述】:

我正在尝试升级到 MS .NET Framework 4,但遇到 dllimport 函数错误(见下文)。当代码到达 dllimport 时,程序会以错误代码 0x80000003 退出。我尝试更改它,以便输入和输出是 StringBuilder,我还尝试通过更改字符集(为 unicode 或 ansi)和设置入口点(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;
    }

谢谢!

【问题讨论】:

  • 你是不是也偶然从 32 位切换到 64 位?
  • 你为什么使用 Win32 api 来解析命令行参数,完全在托管代码中这样做不是更好吗?我知道这并不能回答你的问题,但是为这个任务调用 win32 似乎很奇怪。
  • 在将其编译成一个小的示例命令行应用程序时,我没有收到任何错误,尽管我只是传入了一些垃圾文本。它默认为 x86。但是,当强制项目输出 X64 bin 时,我得到了一个错误(虽然是什么错误,我不确定 - 不知何故,我实际上并没有获得异常信息,也无法在事件查看器中看到很多细节)。

标签: c# .net dll c#-4.0 dllimport


【解决方案1】:

我不确定本地 dll 在 PathGetArgs 中返回什么,但 Marshal 类可以提供帮助。

[DllImport("Shlwapi.dll")]
public static extern IntPtr PathGetArgs([In] string path);

fileArgs = System.Runtime.InteropServices.Marshal.PtrToStringAnsi(PathGetArgs(strCmdLine.ToString()));

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多