SetupDiSetClassInstallParamsW Windows API failing - c++

Below is the code which is calling Windows SetupDiSetClassInstallParamsW
method --
bool
Util::SvmDisableNic(HDEVINFO devInfoSet, SP_DEVINFO_DATA deviceInfo)
{
SP_PROPCHANGE_PARAMS params = {0};
SP_DEVINSTALL_PARAMS_W installParams = {0};
//
// Disable device
//
params.ClassInstallHeader.cbSize = sizeof(SP_CLASSINSTALL_HEADER);
params.ClassInstallHeader.InstallFunction = DIF_PROPERTYCHANGE;
params.StateChange = DICS_DISABLE;
params.Scope = DICS_FLAG_GLOBAL;
params.HwProfile = 0;
if (!SetupDiSetClassInstallParamsW(devInfoSet, &deviceInfo,
&(params.ClassInstallHeader),
sizeof params)) {
DWORD err = GetLastError();
SYSMSG_FUNC(
Debug, _T("Failed setting device params to disable: Err: 0x%X"), err);
return false;
}
This code is running successfully, however, in a couple of VMs, the
SetupDiSetClassInstallParamsW is failing, and GetLastError is returning an error code of 0xD. From microsoft doc, looks like this corresponds to ERROR_INVALID_DATA. The VMs in which this code is running successfully and in those in which it is failing, are identical in terms of hardware/software configuration. Any idea how we can debug why this windows API is failing?

Related

Reading network computer names using mfc in windows 10

I tried to read network computer names using WNetOpenEnum. I am getting only 'Microsoft Terminal Services', 'Microsoft Windows Network' and 'Web Client Network'. Not getting the other machine connected in network.
Is there any way to read the names/IP of computers connected to network?.
if(NO_ERROR == WNetOpenEnum(RESOURCE_GLOBALNET, RESOURCETYPE_ANY, 0, pnetrParent, &hEnum))
{
DWORD dwCount = 1;
char szBuffer[512];
char* psz = szBuffer;
DWORD dwBufferSize = sizeof(szBuffer);
while (NO_ERROR == WNetEnumResource(hEnum, &dwCount, &szBuffer, &dwBufferSize))
{
NETRESOURCE* pnetResource = (NETRESOURCE*)psz;
if (NULL != pnetResource->lpRemoteName && *pnetResource->lpRemoteName)
{
m_lstIPAddress.AddString(pnetResource->lpRemoteName);
}
dwBufferSize = sizeof(szBuffer);
}
DWORD retValue = WNetCloseEnum(hEnum);
}
Any help would be appreciated.
You need to call it recursively. Microsoft Windows Network has computers listed. So next call to WNetOpenEnum will have handle to Microsoft Windows Network you received as first parameter and so on.
I believe this would give you answer: https://learn.microsoft.com/en-us/windows/win32/wnet/enumerating-network-resources

LookupAccountName Always Fails With Error 122 (ERROR_INSUFFICIENT_BUFFER)

Perhaps somebody can enlighten me here.
I'm attempting to automate a WiFi connection process where the SSID is determined by a serial number. Since this is always different, I figured I need to save a temporary profile each time I wish to connect.
WlanSaveTemporaryProfile() wants a LPCWSTR strAllUserProfileSecurity to define permissions for this profile. So far the rabbit hole has led me to try using LookupAccountNameW(). I have tried to AllocateAndInitializeSid() to no avail. I tried plugging in an empty buffer with the same result. In both cases, I get an error 122, which says the buffer was too small.
Any aid here is sincerely appreciated.
Here's the relevant code. Mostly constructed from examples in Microsoft's documentation.
DWORD GetStringSecurityDescriptor(
PWCHAR ps_securityDescriptor, /* This needs to be populated when this function completes. */
PULONG pul_securityDescriptorLen,
LPWSTR ps_accountName
)
{
DWORD dw_result = NULL;
DWORD dw_lastError = NULL;
DWORD dw_bufferSizeOfUserAccount = NULL;
/* Create a security descriptor for the profile. */
SECURITY_DESCRIPTOR secDesc;
bool success = InitializeSecurityDescriptor(&secDesc, SECURITY_DESCRIPTOR_REVISION);
if (!success)
{
wprintf(L"Security Descriptor Initialization Failed.\n");
}
PSID p_userSid = NULL;
/* Attempt 2: Straight up malloc the memory. Doesn't work any better.*/
//p_userSid = malloc(100);
/* Attempt 1: Allocate and Initialize an SID for LookupAccountNameW(). */
SID_IDENTIFIER_AUTHORITY auth = SECURITY_WORLD_SID_AUTHORITY;
BOOL b_sidReady = AllocateAndInitializeSid(
&auth,
6,
SECURITY_NULL_RID,
SECURITY_WORLD_RID,
SECURITY_LOCAL_RID,
SECURITY_LOCAL_LOGON_RID,
SECURITY_CREATOR_OWNER_RID,
SECURITY_CREATOR_GROUP_RID,
0, 0,
&p_userSid
);
LPDWORD buf = &dw_bufferSizeOfUserAccount;
WCHAR domainName[1000] = { 0 }; // Perhaps DNLEN + 1 was too small?
DWORD domainNameLen = 1000;
SID_NAME_USE use = SidTypeUser;
// Currently failing. dw_bufferSizeOfUserAccount still recieves a 28, so that wasn't it.
success = LookupAccountNameW(
NULL,
ps_accountName,
p_userSid,
buf,
domainName,
&domainNameLen,
&use);
if (!success)
{
dw_lastError = GetLastError();
switch (dw_lastError)
{
case ERROR_INSUFFICIENT_BUFFER: // LookupAccountNameW() always ends up here.
wprintf(L"The data area passed to a system call is too small.\n");
FreeSid(p_userSid);
return dw_lastError;
default:
wprintf(L"Looking up Account Name failed. See Error 0x%x.\n", dw_lastError);
FreeSid(p_userSid);
return dw_lastError;
}
}
// ... more code irrelevant to this problem...
}
Great thanks to Georgy Firsov!
I missed a statement in the documentation.
By calculating the size of the SID and storing it in dw_bufferSizeOfUserAccount, the function ran successfully.
dw_bufferSizeOfUserAccount = GetLengthSid(p_userSid);

Disabling USB keyboard after reboot

I used SetupDiSetClassInstallParams and SetupDiCallClassInstaller to disable specific USB device.
SP_PROPCHANGE_PARAMS params;
params.ClassInstallHeader.cbSize = sizeof(SP_CLASSINSTALL_HEADER);
params.ClassInstallHeader.InstallFunction = DIF_PROPERTYCHANGE;
params.StateChange = DICS_DISABLE;
// SP_DEVINFO_DATA - Info.DevInfoData
if (!SetupDiSetClassInstallParams(hDevInfo, &(Info.DevInfoData),&params.ClassInstallHeader, sizeof(params))) {
Error = GetLastError();
return Error;
}
if (!SetupDiCallClassInstaller(DIF_PROPERTYCHANGE, hDevInfo, &(Info.DevInfoData))) {
Error = GetLastError(); // error here
}
This approach works to all devices, except keyboard.
If i try to disable her this way
ERROR_NOT_DISABLEABLE returned. But after reboot keyboard will be disabled.
Before disabling, flags DN_DISABLEABLE and DN_REMOVABLE seted for this device, checked by function CM_Get_DevNode_Status.
What i am doing wrong?
This device don't support disabling

NetUserAdd failing when running on workgroup Windows Server 2008 R2

I'm trying to add a local user to Windows using the Network Management API. I'm having no problems when running on Windows 7 (either on a domain or workgroup) or windows 2008 R2 server when on a domain. However, if I run this on Windows server 2008 R2 that isn't on a domain I get an error. Problem is the error code returned is -1 rather than one of the given errors in the documentation
bool CLocalUsers::AddUser(LPCTSTR lpszUserName, LPCTSTR lpszPassword)
{
// Clear error code
m_dwLastError = 0;
USER_INFO_1 ui;
ui.usri1_name = (LPTSTR)(LPCTSTR)lpszUserName;
ui.usri1_priv = USER_PRIV_USER;
ui.usri1_home_dir = NULL;
ui.usri1_comment = NULL;
ui.usri1_flags = UF_SCRIPT | UF_DONT_EXPIRE_PASSWD;
ui.usri1_script_path = NULL;
// Point to a zero character password if null
if(lpszPassword == NULL)
{
lpszPassword = _T("");
ui.usri1_flags |= UF_PASSWD_NOTREQD;
}
ui.usri1_password = (LPTSTR)(LPCTSTR)lpszPassword;
// Add the user
NET_API_STATUS nStatus = ::NetUserAdd(NULL, 1, (LPBYTE)&ui, &m_dwLastError);
if(nStatus != NERR_Success)
{
// DEBUG ONLY
CString szErrorMsg;
szErrorMsg.Format(_T("Error code %d"), m_dwLastError);
::AfxMessageBox(szErrorMsg, MB_OK);
// DEBUG ONLY
return false;
}
return true;
}
It's being run under admin privileges. I can call other Network functions with out any problems (NetUserGetInfo, NetLocalGroupAddMembers).
I've experimented with different password sizes but I can happily create the same account with the same information using the Server Manager tool that comes with Windows 2008
Thanks
After looking at the status code rather than m_dwLastError it turns out it was NERR_PasswordTooShort as the minimum password requirement on a fresh Windows 2008 server is 8 characters.

FILE_NOT_FOUND when trying to open COM port C++

I am trying to open a com port for reading and writing using C++ but I can't seem to pass the first stage of actually opening it. I get an INVALID_HANDLE_VALUE on the handle
with GetLastError FILE_NOT_FOUND. I have searched around the web for a couple of days I'm fresh out of ideas. I have searched through all the questions regarding COM on this website too.
I have scanned through the existing ports (or so I believe) to get the name of the port right.
I also tried combinations of _T("COM1") with the slashes, without the slashes, with colon, without colon and without the _T
I'm using windows 7 on 64 bit machine.
this is the code i got
I'll be glad for any input on this
void SendToCom(char* data, int len)
{
DWORD cbNeeded = 0;
DWORD dwPorts = 0;
EnumPorts(NULL, 1, NULL, 0, &cbNeeded, &dwPorts);
//What will be the return value
BOOL bSuccess = FALSE;
LPCSTR COM1 ;
BYTE* pPorts = static_cast<BYTE*>(malloc(cbNeeded));
bSuccess = EnumPorts(NULL, 1, pPorts, cbNeeded, &cbNeeded, &dwPorts);
if (bSuccess){
PORT_INFO_1* pPortInfo = reinterpret_cast<PORT_INFO_1*>(pPorts);
for (DWORD i=0; i<dwPorts; i++)
{
//If it looks like "COMX" then
size_t nLen = _tcslen(pPortInfo->pName);
if (nLen > 3)
{
if ((_tcsnicmp(pPortInfo->pName, _T("COM"), 3) == 0) ){
COM1 =pPortInfo->pName;
//COM1 ="\\\\.\\COM1";
HANDLE m_hCommPort = CreateFile( COM1 ,
GENERIC_READ|GENERIC_WRITE, // access ( read and write)
0, // (share) 0:cannot share the COM port
NULL, // security (None)
OPEN_EXISTING, // creation : open_existing
FILE_FLAG_OVERLAPPED, // we want overlapped operation
NULL // no templates file for COM port...
);
if (m_hCommPort==INVALID_HANDLE_VALUE)
{
DWORD err = GetLastError();
if (err == ERROR_FILE_NOT_FOUND) {
MessageBox(hWnd,"ERROR_FILE_NOT_FOUND",NULL,MB_ABORTRETRYIGNORE);
}
else
if(err == ERROR_INVALID_NAME) {
MessageBox(hWnd,"ERROR_INVALID_NAME",NULL,MB_ABORTRETRYIGNORE);
}
else
{
MessageBox(hWnd,"unkown error",NULL,MB_ABORTRETRYIGNORE);
}
}
else{
WriteAndReadPort(m_hCommPort,data);
}
}
pPortInfo++;
}
}
}
}
The Solution is to use
The Problem is, if your port is Bigger then 9 then you have to use the Syntax
LPCWSTR szPortName = L"\\\\.\\COM11";.
If you are on Windows 10 - running all system updates might help !
I had the same issue that opening port "COM4" returned an error ERROR_FILE_NOT_FOUND. When running the program as "Administrator" it worked. Now after a updating to 1511 the program can open "COM4" even not running as "Administrator".
http://www.cplusplus.com/forum/windows/163855/
Use CreateFileA(...) instead of CreateFile(...)
ERROR_FILE_NOT_FOUND can be produced from CreateFile(L"\\\\.\\COM1", ...) and CreateFile(L"COM1:", ...) after using the Device Manager to change the assigned COM Port number. Disabling and re-enabling the device, or unplugging and reconnecting the USB adapter resolves the issue.
A useful test to confirm whether it is your program or the system is to send data to the port in command prompt. A successful test will show an empty line. A failed test will show an error message.
C:\drop>echo > \\.\COM1
The system cannot find the file specified.
C:\drop>echo > \\.\COM1
C:\drop>