C# DllImport 方法,出現 The system cannot find the file specified 錯誤


最近使用 C# 調用 dll 中的 UpdateDriverForPlugAndPlayDevices() 方法去安裝驅動,遇到了 “The system cannot find the file specified” 問題,

看着錯誤提示,是指系統沒有找到指定文件,但是我檢查硬盤,該文件確實存在,但是因為我測試時使用的是 Hebrew 希伯來語的系統,
我就考慮會不會是因為路徑中包含了特殊字符,導致該方法不能正常工作,我當前的方法聲明如下:

[DllImport("newdev.dll", SetLastError = true)]
public static extern bool UpdateDriverForPlugAndPlayDevices(IntPtr hwndParent, string HardwareId, string FullInfPath, UpdateDriverInstallFlag install, out Boolean bRebootRequired);

后面經查資料,添加了 CharSet = CharSet.Auto,問題解決, 修改后的方法聲明:

[DllImport("newdev.dll", SetLastError = true, CharSet = CharSet.Auto)]
public static extern bool UpdateDriverForPlugAndPlayDevices(IntPtr hwndParent, string HardwareId, string FullInfPath, UpdateDriverInstallFlag install, out Boolean bRebootRequired);

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM