2011-06-11 132 views
2

我想升級到MS .NET Framework 4,並遇到dllimport函數錯誤(請參見下文)。當代碼到達dllimport時,該程序僅以錯誤代碼0x80000003退出。我試圖改變它,所以輸入和輸出是StringBuilder,我也嘗試通過更改charset(unicode或ansi)和設置EntryPoint(PathGetArgsA或PathGetArgsW)來設置字符集。此代碼在v3.5中正常工作,但不在v4中。試圖升級到.NET Framework 4

[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; 
    } 

謝謝!

+2

你是否也從32位切換到64位? – 2011-06-11 07:00:26

+2

爲什麼使用Win32 API只是解析命令行參數,是不是更好地在託管代碼中完成?我意識到這並不能回答你的問題,但爲此任務調用win32似乎很奇怪。 – Haukman 2011-06-11 07:02:27

+0

編譯成一個小的示例命令行應用程序時,我沒有錯誤,但我只是傳入一些垃圾文本。它默認爲x86。然而,當強制項目輸出一個X64 bin時,我得到了一個錯誤(儘管出現了什麼錯誤,我不確定 - 不知何故,我實際上沒有得到異常信息,也不能在事件查看器中看到很多細節)。 – 2011-06-11 07:05:55

回答

3

我不確定本地dll在PathGetArgs中回退的是什麼,但是元帥類可以提供幫助。

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

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

謝謝!那就是訣竅。 – ub3rst4r 2011-06-11 17:56:40