最近有点忙,因为设计了很久的项目终于开绐写代码了。但坚持写博是我的习惯,于是这会儿吃了晚饭的休息时间记录了一点注册表API 操作的东西。
下面这段代码主要是用来枚举windows mobile下的plug in,然后可以设置所有的plug in的enabled键值。但windows mobile 的plug in有一个特例就是date plug in.它没有和其它的plug in一样,创建在同一个键的下面,而是单独的存储。
#define RGE_TODAY_ITEMS L"Software//Microsoft//Today//Items"
#define RGE_TODAY_DATE L"Software//Microsoft//Today"
#define ITEM_NAME_SIZE 128
#define MAX_STRING 256
#define ITEM_VALUE L"Enabled"
#define HOME_PLUG_NAME L"PlugInDemo"
#define DATE_PLUG L"Date"
BOOL EnableAllTodayItem(BOOL flag)
{
BOOL bRs = TRUE;
HKEY hTodayItem;
HKEY hItem;
WCHAR szItemName[ITEM_NAME_SIZE]= {0};
WCHAR szItemPath[MAX_STRING]={0};
DWORD dwSubkey = ITEM_NAME_SIZE;
DWORD bEnableValue = flag;
WCHAR szHomePlugName[ITEM_NAME_SIZE]= {0};
wsprintfW((LPWSTR)&szHomePlugName, L"%s",HOME_PLUG_NAME);
//enable date plug in
if (ERROR_SUCCESS != RegOpenKeyEx(HKEY_LOCAL_MACHINE, RGE_TODAY_DATE, 0, NULL, &hTodayItem))
{
bRs = FALSE;
goto Exit;
}
if (ERROR_SUCCESS != RegSetValueEx(hTodayItem, DATE_PLUG, 0, REG_DWORD,(LPBYTE)&bEnableValue,sizeof(bEnableValue)))
{
goto Exit;
}
//enable other items plugin
if (ERROR_SUCCESS != RegOpenKeyEx(HKEY_LOCAL_MACHINE, RGE_TODAY_ITEMS, 0, NULL, &hTodayItem))
{
bRs = FALSE;
goto Exit;
}
//enum all sub key below item directory, then open it and set it's value
for (int i= 0;
ERROR_NO_MORE_ITEMS != RegEnumKeyEx(hTodayItem, i, (LPWSTR)&szItemName, &dwSubkey,
NULL, NULL, NULL, NULL);
i++)
{
dwSubkey = ITEM_NAME_SIZE;
if (0 == wcsncmp((wchar_t *)&szHomePlugName, (wchar_t *)&szItemName, sizeof(szHomePlugName)))
{
continue;
}
wsprintfW((LPWSTR)&szItemPath, L"Software//Microsoft//Today//Items//%s",szItemName);
//open current item and set it's value
if (ERROR_SUCCESS != RegOpenKeyEx(HKEY_LOCAL_MACHINE, szItemPath, 0, NULL, &hItem))
{
bRs = FALSE;
goto Exit;
}
if (ERROR_SUCCESS != RegSetValueEx(hItem, ITEM_VALUE, 0, REG_DWORD,(LPBYTE)&bEnableValue,sizeof(bEnableValue)))
{
bRs = FALSE;
goto Exit;
}
}
Exit:
return bRs;
}