Разобрался. Просто я не правильно имя указал.
T> //PCTSTR MachineName = "\\MyComputer";
надо:
PCTSTR MachineName = "\\\\MyComputer";
Теперь другая проблема. CM_Get_DevNode_Status_Ex возвращает ошибку CR_INVALID_DEVINST при обращении к удалённой машине. Для локальной машины всё ок. Хотя вроде всё должно быть ок. В MSDN пишут что:
dnDevInst
Caller-supplied device instance handle, obtained from the SP_DEVINFO_DATA structure that is used with the device installation functions.
я вроде его оттуда и взял, с чего это он вдруг invalid.
Вот полный код:
#define _WIN32_WINNT 0x0501
#define WINVER 0x0501
#include <windows.h>
#include <stdio.h>
#include <conio.h>
#include <setupapi.h>
#include <devguid.h>
#include <cfgmgr32.h>
BOOL GetRegistryProperty(HDEVINFO DeviceInfoSet,
PSP_DEVINFO_DATA DeviceInfoData,
ULONG Property,
LPTSTR* Buffer,
PULONG Length)
{
while (!SetupDiGetDeviceRegistryProperty(
DeviceInfoSet,
DeviceInfoData,
Property,
NULL,
(PBYTE)(*Buffer),
*Length,
Length
))
{
int nError = GetLastError();
if (nError == ERROR_INSUFFICIENT_BUFFER) {
if (*(LPTSTR *)Buffer)
LocalFree(*(LPTSTR *)Buffer);
*Buffer = (LPTSTR)LocalAlloc(LPTR, *Length);
}else {
return FALSE;
}
}
return TRUE;
}
void main(int argc, char* argv[]) {
BOOL bShowHidden = TRUE;
DWORD MemberIndex = 0;
SP_DEVINFO_DATA DeviceInfoData;
DeviceInfoData.cbSize = sizeof(SP_DEVINFO_DATA);
ULONG Status = 0;
ULONG Problem = 0;
LPSTR pBuffer = 0;
ULONG Length = 0;
HMACHINE hMachine = 0;
PCTSTR MachineName = "\\\\Computer";
if (argc > 1)
bShowHidden = FALSE;
if (CR_SUCCESS != CM_Connect_Machine(MachineName, &hMachine))
{
printf("CM_Connect_Machine() error\n");
return;
}
HDEVINFO DeviceInfoSet = SetupDiGetClassDevsEx(
(LPGUID) &GUID_DEVCLASS_NET,
NULL,
NULL,
DIGCF_PRESENT,
NULL,
MachineName,
NULL);
if (INVALID_HANDLE_VALUE == DeviceInfoSet)
{
int nError = GetLastError();
LPVOID lpMsgBuf;
if (!FormatMessage(
FORMAT_MESSAGE_ALLOCATE_BUFFER |
FORMAT_MESSAGE_FROM_SYSTEM |
FORMAT_MESSAGE_IGNORE_INSERTS,
NULL,
nError,
MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), // Default language
(LPTSTR) &lpMsgBuf,
0,
NULL ))
{
// Handle the error.
printf("Error: unknown error. Code: %u\n", nError);
return;
}
// Display the string.
printf("Error: %s\n", (LPCTSTR)lpMsgBuf);
// Free the buffer.
LocalFree( lpMsgBuf );
return;
}
while (SetupDiEnumDeviceInfo(DeviceInfoSet, MemberIndex, &DeviceInfoData))
{
MemberIndex++;
CONFIGRET nError = CM_Get_DevNode_Status_Ex(&Status, &Problem, DeviceInfoData.DevInst,0, hMachine);
if (CR_SUCCESS != nError)
{
printf("Error occured: %u\n", nError);
continue;
}
if (!bShowHidden && (Status & DN_NO_SHOW_IN_DM))
{
continue;
}
if (!GetRegistryProperty(DeviceInfoSet,
&DeviceInfoData,
SPDRP_FRIENDLYNAME,
&pBuffer,
&Length))
{
if (!GetRegistryProperty(DeviceInfoSet,
&DeviceInfoData,
SPDRP_DEVICEDESC,
&pBuffer,
&Length))
{
continue;
}
}
printf("%d. %s\n", MemberIndex, pBuffer);
}
}