The DefineDosDevice function can assign a disk letter to a partition. However, its behavior varies depending on the account and elevation status under which the function is called. If running as LocalSystem the assignment is made in the global namespace. Otherwise, it is made in the session's namespace. If running as Administrator with elevated privileges the assignment is visible to the elevated process but not visible to unelevated processes (e.g., explorer). If running without elevation the assignment is visible to unelevated processes like explorer, but not to an elevated process.
Following minimal example with virtually no error checking can assign a drive letter without causing any notifications and so will not automatically appear in explorer.
The sample code -
#define WIN32_LEAN_AND_MEAN
#include <Windows.h>
#include <stdio.h>
int wmain(int argc, WCHAR *argv[])
{
if (argc != 4)
{
wprintf(L"AddDev deviceletter disknumber partitionnumber\n"
L"For example AddDev S: 2 1\n");
return 1;
}
WCHAR szDevice[MAX_PATH]{}, szTargetPath[MAX_PATH]{};
swprintf_s(szDevice, L"Harddisk%sPartition%s", argv[2], argv[3]);
if (QueryDosDeviceW(szDevice, szTargetPath, MAX_PATH))
{
if (DefineDosDeviceW(DDD_RAW_TARGET_PATH | DDD_NO_BROADCAST_SYSTEM, argv[1], szTargetPath))
wprintf_s(L"Drive letter %s assigned\n", argv[1]);
else
wprintf_s(L"DefineDosDeviceW for %s failed with %d\n", argv[1], GetLastError());
}
else
wprintf_s(L"QueryDosDeviceW for %s failed with %d\n", szDevice, GetLastError());
return 0;
}
Following shows results of creating, attaching and partitioning a vhdx. No drive letters were assigned to any of the partitions.
You can see that it was added as Disk 2.
From an unelevated command prompt assign drive letter S: to the first partition (PARTITION_SYSTEM_GUID). Note that the letter did not automatically appear in explorer and there were no notifications.