gpt4 book ai didi

c# - 如何强制winCE启动后看到SD/MMC卡

转载 作者:行者123 更新时间:2023-11-30 13:02:57 26 4
gpt4 key购买 nike

我正在为一种与某些科学设备交互的新仪器开发软件。用户界面通过 Windows CE 掌上电脑 (Windows CE 600 V3.01 Build 195) 实现。该仪器设置为将一根柔性电缆的一端始终插入 PC 上的 SD 卡插槽中。电缆进入仪器,另一端插入已安装的 SD 卡(如果存在)。 SD 卡包含与仪器一起使用的芯片,以及需要的软件/固件更新和与芯片相关的数据文件。我有一个在 WinCE 启动时运行的 c# 软件应用程序。此应用程序检查是否存在 SDMMC 卡,因为它依赖于与特定功能的随附芯片相关的数据。

我的问题:如果在 WinCE 启动时将 SD 卡插入柔性电缆的另一端,winCE 会检测到卡的存在并创建\SDMMC 文件夹以允许软件读取数据。如果只插入了柔性电缆,但另一端没有 SDMMC 卡,则 Windows 不会创建该文件夹。这是我期待的。但是我们的现场工程师会在软件处于事件状态时出于各种原因将卡换入换出以更换芯片。如果在插入卡之前启动 Windows,则会出现问题。由于柔性电缆,WinCE 永远不会检测到卡已插入。它也永远不会检测到它已被删除。

软件每 5 秒轮询一次 SD 卡。固件可以通过位设置判断是否插入了卡,并将此信息传递给软件。如果卡以前不存在,现在被检测到,但是\SDMMC文件夹不存在,我希望软件触发WinCE再次尝试检测。我考虑过使用注册表值,但不清楚是否可以写入 HKEY_LOCAL_MACHINE SDMMc 值。我也不太确定这些值是什么意思。可以重置这些值吗?我看到 Storage Manager Registry 将定义如下:

[HKEY_LOCAL_MACHINE\System\StorageManager\Profiles\SDMMC] 
"Name"="SD MMC device"
"Folder"="SD Card

有没有什么办法可以读/写这个注册表来戳winCE,看看网线的另一端插入了一张卡?我完全没有这个想法吗?还有其他方法吗?

我为这个冗长的问题道歉。我一般在应用层玩。谷歌搜索这个问题并没有给我我需要的答案,尽管我可能还不够了解以提出正确的问题。感谢您能给我的任何帮助。

更新:我仍在尝试找到解决此问题的方法。我目前的想法是当我的仪器固件检测到卡已插入时,强制 Windows CE 重新枚举设备。我的代码轮询固件以在我的 c# 应用程序中接收有关此的通知。对于我的仪器/应用程序,SD 卡始终位于“Dsk2:”。当 c# 应用程序收到 SD 卡已插入的通知时,它会调用执行以下操作的方法:

代码

     hDevice = CreateFile("\\Dsk2:", (0x80000000) | (0x40000000), 0,
IntPtr.Zero, OPEN_EXISTING, 0, IntPtr.Zero);

if (hDevice == INVALID_HANDLE_VALUE)
{
int hDeviceError = Marshal.GetLastWin32Error();
// THis is an error - call GetLastERror to find
// out what happened.
using (StreamWriter bw = new StreamWriter(File.Open(App.chipDebugFile, FileMode.Append)))
{
String iua = "DevDriverInterface: error from CreateFile: " + hDeviceError.ToString();
bw.WriteLine(iua);
}
return false;
}

bResult = DeviceIoControl(hDevice,
IOCTL_DISK_UPDATE_PROPERTIES,
null,
0,
null,
0,
ref nBytesReturned,
nOverLapped);

/代码

在上面,CreateFile() 调用失败并出现错误 55:“指定的网络资源或设备不再可用”。

我的问题:我尝试做的重新枚举设备是否合理? CreateFile 错误是否表明我应该改为调用 ActivateDevice()?我在这里看到一个例子:Problems getting a device driver to load at startup - WM6.1有人从 c# 代码调用 ActivateDevice() 并想知道这是否会解决让 WIndows CE 识别现在插入 SD 卡的问题。任何人都可以帮助我了解发送到窗口的 ActivateDevice() 命令的参数吗?我对这种方法有异议吗?

这对我来说是全新的领域,非常感谢您提供的任何帮助。谢谢。

最佳答案

对于任何感兴趣的人,这里是解决问题的代码。当固件检测到 SD 卡在开机后已插入或删除时调用此方法。本质上,它卸载/加载设备驱动程序:

[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Auto)]
public struct DEVMGR_DEVICE_INFORMATION
{
public uint dwSize;
public IntPtr hDevice;
public IntPtr hParentDevice;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 6)]
public string szLegacyName;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 260)]
public string szDeviceKey;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 260)]
public string szDeviceName;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 260)]
public string szBusName;
}

public enum DeviceSearchType : int
{
DeviceSearchByLegacyName = 0,
DeviceSearchByDeviceName = 1,
DeviceSearchByBusName = 2,
DeviceSearchByGuid = 3,
DeviceSearchByParent = 4
}
class DevDriverInterface
{
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Auto)]
public struct SECURITY_ATTRIBUTES
{
public int nLength;
public IntPtr lpSecurityDescriptor;
public bool bInheritHandle;
}

[DllImport("coredll.dll", SetLastError = true)]
public extern static int ActivateDeviceEx(string device, IntPtr regEnts,
UInt32 cRegEnts, IntPtr devKey);

[DllImport("coredll.dll", SetLastError = true)]
public extern static bool DeactivateDevice(int handle);

[DllImport("coredll.dll", SetLastError = true)]
public static extern int FindFirstDevice(DeviceSearchType
searchType, IntPtr searchParam, ref DEVMGR_DEVICE_INFORMATION pdi);

// Constructor
public DevDriverInterface() { }

public bool MountSDCardDrive()
{
const int INVALID_HANDLE_VALUE = -1;

string mRegPath1 = "";
int handle = INVALID_HANDLE_VALUE;
DeviceSearchType searchType = DeviceSearchType.DeviceSearchByDeviceName;

DEVMGR_DEVICE_INFORMATION di = new DEVMGR_DEVICE_INFORMATION();
di.dwSize = (uint)Marshal.SizeOf(typeof(DEVMGR_DEVICE_INFORMATION));

String searchParamString = "SDH1";

IntPtr searchParam = Marshal.StringToBSTR(searchParamString);

handle = FindFirstDevice(searchType, searchParam, ref di);

if (handle == INVALID_HANDLE_VALUE)
{
// Failure - print error
int hFindFirstDeviceError = Marshal.GetLastWin32Error();
using (StreamWriter bw = new StreamWriter(File.Open(App.chipDebugFile,
FileMode.Append)))
{
String iua = "DevDriverInterface: error from FindFirstDevice: " +
hFindFirstDeviceError.ToString();
bw.WriteLine(iua);
}
return false;
}
else
{
mRegPath1 = di.szDeviceKey;
bool deactBool = DeactivateDevice((int) di.hDevice);
if (deactBool == false)
{
using (StreamWriter bw = new StreamWriter(File.Open(App.chipDebugFile,
FileMode.Append)))
{
String iua = "DevDriverInterface: DeactivateDevice: returned false -
FAILED";
bw.WriteLine(iua);
}
return false;
}

Thread.Sleep(50);
// Call ActiveDevice to setup the device driver
handle = ActivateDeviceEx(mRegPath1, IntPtr.Zero, 0, IntPtr.Zero);
if (handle == INVALID_HANDLE_VALUE)
{
// Failure - print error
int hActivateDeviceError = Marshal.GetLastWin32Error();

using (StreamWriter bw = new StreamWriter(File.Open(App.chipDebugFile,
FileMode.Append)))
{
String iua = "DevDriverInterface: error from ActivateDevice: " +
hActivateDeviceError.ToString();
bw.WriteLine(iua);
}
return false;
}

}

return true;
}

}; // end class

关于c# - 如何强制winCE启动后看到SD/MMC卡,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14058885/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com