【发布时间】:2011-07-13 20:04:37
【问题描述】:
我正在尝试在 64 位 .NET 程序集上使用 DllImport (PInvoke) 的 PathCanonicalize 函数,它会导致内存损坏,从而导致各种不良行为(崩溃、突然出现的异常等)。 (例如:System.AccessViolationException: Attempted to read or write protected memory。这通常表明其他内存已损坏。)
[DllImport("shlwapi", CharSet = CharSet.Auto, EntryPoint="PathCanonicalize", SetLastError = true)]
private static extern bool PathCanonicalize( [Out] StringBuilder lpszDst,[In] string lpszSrc );
public static string MyPathCanonicalize(string path)
{
StringBuilder builder = new StringBuilder();
if (!PathCanonicalize(builder, path))
return path;
return builder.ToString();
}
我在this thread 中看到我可能应该使用 IntPtr 而不是直接使用字符串。谁能告诉我如何在 PathCanonicalize 的输入和输出字符串中编组?
原型是:
BOOL PathCanonicalize(
__out LPTSTR lpszDst,
__in LPCTSTR lpszSrc
);
【问题讨论】: