📄 freeotfe4pdaregistry.c
字号:
{
BOOL retval = TRUE;
WCHAR* fullProfileKey;
REGDETAILS_BUILTIN detailsBuiltin;
DEBUGOUTLIB(DEBUGLEV_ENTER, (TEXT("RegDetailsDeleteAllByKey\n")));
// Get corresponding profile key...
DEBUGOUTLIB(DEBUGLEV_INFO, (TEXT("Getting corresponding profile key...\n")));
fullProfileKey = NULL;
if (RegDetailsGetBuiltinByKey(builtinKey, &detailsBuiltin))
{
// The "Profile" member of detailsBuiltin only lists the profile, not
// the registry key.
fullProfileKey = calloc(
wcslen(REGKEY_PROFILE) +
wcslen(REGKEY_SEPARATOR) +
wcslen(detailsBuiltin.Profile) +
1, // +1 for terminating NULL char
sizeof(*fullProfileKey)
);
if (fullProfileKey == NULL)
{
DEBUGOUTLIB(DEBUGLEV_ERROR, (TEXT("Unable to malloc storage for full profile key\n")));
}
else
{
// Build up registry key...
wcscpy(fullProfileKey, REGKEY_PROFILE);
wcscat(fullProfileKey, REGKEY_SEPARATOR);
wcscat(fullProfileKey, detailsBuiltin.Profile);
DEBUGOUTLIB(DEBUGLEV_INFO, (TEXT("Profile key from builtin: %ls\n"), fullProfileKey));
}
}
// Carry out deletion of builtin and profile keys...
DEBUGOUTLIB(DEBUGLEV_INFO, (TEXT("Deleting builtin regkey...\n")));
if (RegDeleteKey(
REGHIVE,
builtinKey
) != ERROR_SUCCESS)
{
DEBUGOUTLIB(DEBUGLEV_INFO, (TEXT("Failed to delete builtin registry key\n")));
retval = FALSE;
}
if (
retval ||
force
)
{
DEBUGOUTLIB(DEBUGLEV_INFO, (TEXT("Deleting profile regkey...\n")));
if (RegDeleteKey(
REGHIVE,
fullProfileKey
) != ERROR_SUCCESS)
{
DEBUGOUTLIB(DEBUGLEV_INFO, (TEXT("Failed to delete profile registry key\n")));
retval = FALSE;
}
}
SecZeroAndFreeWCHARMemory(fullProfileKey);
SecZeroMemory(&detailsBuiltin, sizeof(detailsBuiltin));
DEBUGOUTLIB(DEBUGLEV_EXIT, (TEXT("RegDetailsDeleteAllByKey\n")));
return retval;
}
// =========================================================================
BOOL RegDetailsDeleteAllByID(int id, BOOL force)
{
BOOL retval = TRUE;
WCHAR* keyBuiltin;
WCHAR* keyProfile;
DEBUGOUTLIB(DEBUGLEV_ENTER, (TEXT("RegDetailsDeleteAllByID\n")));
RegKeyGenerate(id, &keyBuiltin, &keyProfile);
DEBUGOUTLIB(DEBUGLEV_INFO, (TEXT("Deleting builtin regkey...\n")));
if (RegDeleteKey(
REGHIVE,
keyBuiltin
) != ERROR_SUCCESS)
{
DEBUGOUTLIB(DEBUGLEV_INFO, (TEXT("Failed to delete builtin registry key\n")));
retval = FALSE;
}
if (
retval ||
force
)
{
DEBUGOUTLIB(DEBUGLEV_INFO, (TEXT("Deleting profile regkey...\n")));
if (RegDeleteKey(
REGHIVE,
keyProfile
) != ERROR_SUCCESS)
{
DEBUGOUTLIB(DEBUGLEV_INFO, (TEXT("Failed to delete profile registry key\n")));
retval = FALSE;
}
}
SecZeroAndFreeWCHARMemory(keyBuiltin);
SecZeroAndFreeWCHARMemory(keyProfile);
DEBUGOUTLIB(DEBUGLEV_EXIT, (TEXT("RegDetailsDeleteAllByID\n")));
return retval;
}
// =========================================================================
void RegKeyGenerate(int id, WCHAR** keyBuiltin, WCHAR** keyProfile)
{
WCHAR* profileName;
WCHAR* registryRootKey;
WCHAR* builtinKey;
HKEY hKey;
DEBUGOUTLIB(DEBUGLEV_ENTER, (TEXT("RegKeyGenerate\n")));
profileName = ProfileNameGenerate(id);
if (keyBuiltin != NULL)
{
// Get builtin key from registry
registryRootKey = NULL;
if (RegOpenKeyEx(
REGHIVE,
REGKEY_DRIVERS,
0,
0,
&hKey
) == ERROR_SUCCESS)
{
RegistryGetString(
hKey,
REGNAME_DRIVERROOTKEY,
®istryRootKey
);
}
if (registryRootKey == NULL)
{
builtinKey = REGKEY_BUILTIN_FALLBACK;
DEBUGOUTLIB(DEBUGLEV_INFO, (TEXT("Using fallback rootkey: %ls\n"), builtinKey));
}
else
{
builtinKey = registryRootKey;
DEBUGOUTLIB(DEBUGLEV_INFO, (TEXT("Using driver rootkey from registry: %ls\n"), builtinKey));
}
*keyBuiltin = calloc(
(
wcslen(builtinKey) +
wcslen(REGKEY_SEPARATOR) +
wcslen(profileName) +
1 // Zero terminator
),
sizeof(WCHAR)
);
wsprintf(
*keyBuiltin,
TEXT("%s%s%s"),
builtinKey,
REGKEY_SEPARATOR,
profileName
);
SecZeroAndFreeWCHARMemory(registryRootKey);
DEBUGOUTLIB(DEBUGLEV_INFO, (TEXT("Full builtin registry keyname: %s\n"), *keyBuiltin));
}
if (keyProfile != NULL)
{
*keyProfile = calloc(
(
wcslen(REGKEY_PROFILE) +
wcslen(REGKEY_SEPARATOR) +
wcslen(profileName) +
1 // Zero terminator
),
sizeof(WCHAR)
);
wsprintf(
*keyProfile,
TEXT("%s%s%s"),
REGKEY_PROFILE,
REGKEY_SEPARATOR,
profileName
);
DEBUGOUTLIB(DEBUGLEV_INFO, (TEXT("Full profile registry keyname: %s\n"), *keyProfile));
}
SecZeroAndFreeWCHARMemory(profileName);
DEBUGOUTLIB(DEBUGLEV_EXIT, (TEXT("RegKeyGenerate\n")));
}
// =========================================================================
WCHAR* ProfileNameGenerate(int id)
{
WCHAR* retval;
DEBUGOUTLIB(DEBUGLEV_ENTER, (TEXT("ProfileNameGenerate\n")));
retval = calloc(
(
wcslen(REGKEY_KEY_PREFIX) +
5 // Zero terminator and number padding (overestimate)
),
sizeof(WCHAR)
);
wsprintf(
retval,
REGKEY_KEY_PREFIX,
id
);
DEBUGOUTLIB(DEBUGLEV_INFO, (TEXT("Profile name: %s\n"), retval));
DEBUGOUTLIB(DEBUGLEV_EXIT, (TEXT("ProfileNameGenerate\n")));
return retval;
}
// =========================================================================
BOOL RegDetailsGetAllByID(
int id,
REGDETAILS_BUILTIN* detailsBuiltin,
REGDETAILS_PROFILE* detailsProfile
)
{
BOOL retval = TRUE;
DEBUGOUTLIB(DEBUGLEV_ENTER, (TEXT("RegDetailsGetAllByID\n")));
retval = RegDetailsGetBuiltinByID(id, detailsBuiltin);
if (retval)
{
retval = RegDetailsGetProfileByID(
id,
detailsProfile
);
}
DEBUGOUTLIB(DEBUGLEV_EXIT, (TEXT("RegDetailsGetAllByID\n")));
return retval;
}
// =========================================================================
BOOL RegDetailsGetAllByKey(
WCHAR* builtinKey,
REGDETAILS_BUILTIN* detailsBuiltin,
REGDETAILS_PROFILE* detailsProfile
)
{
BOOL retval = TRUE;
WCHAR* fullProfileKey;
DEBUGOUTLIB(DEBUGLEV_ENTER, (TEXT("RegDetailsGetAllByKey\n")));
retval = RegDetailsGetBuiltinByKey(builtinKey, detailsBuiltin);
if (retval)
{
// The "Profile" member of detailsBuiltin only lists the profile, not
// the registry key.
fullProfileKey = calloc(
wcslen(REGKEY_PROFILE) +
wcslen(REGKEY_SEPARATOR) +
wcslen(detailsBuiltin->Profile) +
1, // +1 for terminating NULL char
sizeof(*fullProfileKey)
);
// Build up registry key...
wcscpy(fullProfileKey, REGKEY_PROFILE);
wcscat(fullProfileKey, REGKEY_SEPARATOR);
wcscat(fullProfileKey, detailsBuiltin->Profile);
DEBUGOUTLIB(DEBUGLEV_INFO, (TEXT("Profile key from builtin: %ls\n"), fullProfileKey));
retval = RegDetailsGetProfileByKey(
fullProfileKey,
detailsProfile
);
SecZeroAndFreeWCHARMemory(fullProfileKey);
}
DEBUGOUTLIB(DEBUGLEV_EXIT, (TEXT("RegDetailsGetAllByKey\n")));
return retval;
}
// =========================================================================
// Free off WCHARS associated with all of the elements in "detailsActive"
void RegDetailsFreeAllActive(
int cnt, // Number of elements in detailsActive array
REGDETAILS_ACTIVE* detailsActive // Actually an array of REGDETAILS_ACTIVE
)
{
int i;
DEBUGOUTLIB(DEBUGLEV_ENTER, (TEXT("RegDetailsFreeAllActive\n")));
if (detailsActive != NULL)
{
for (i = 0; i < cnt; i++)
{
RegDetailsFree(
NULL,
NULL,
&(detailsActive[i])
);
}
}
DEBUGOUTLIB(DEBUGLEV_EXIT, (TEXT("RegDetailsFreeAllActive\n")));
}
// =========================================================================
// Returns -1 on failure
// Set either (or both) of detailsActive/detailsActive to 0/NULL to just
// return a count of the number that would otherwise be returned
// If the buffer supplied is too small, this function will only fill in the
// first (detailsActiveSize / sizeof(*detailsActive)) entries - and will
// return *that* number
// IMPORTANT: It is the callers responsibility to free off the WCHAR*s
// populated in detailsActive (e.g. call RegDetailsFreeAllActive)
int RegDetailsGetAllActive(
int detailsActiveSize, // Size in bytes of detailsActive buffer
// On exit, this will be set to the amount of the
// buffer used
REGDETAILS_ACTIVE *detailsActive // Actually an array of REGDETAILS_ACTIVE
)
{
int cntFound;
HKEY hKey;
DWORD dwIndex;
WCHAR buffer[512]; // Just needs to be big enough for the registry key
// (just the final part). In practice, this is a
// number, so a couple of bytes should do.
DWORD tmpBufSize;
REGDETAILS_ACTIVE currDetailsActive;
BOOL returningDetails;
WCHAR* tmpEnumFullKey;
int bufferUsed;
DEBUGOUTLIB(DEBUGLEV_ENTER, (TEXT("RegDetailsGetAllActive\n")));
bufferUsed = 0;
cntFound = -1;
if (RegOpenKeyEx(
REGHIVE,
REGKEY_DRIVERS_ACTIVE,
0,
0,
&hKey
) != ERROR_SUCCESS)
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -