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);

 

posted @ 2020-05-16 18:19  一菲聪天  阅读(2509)  评论(0编辑  收藏  举报