I found this draft code in my archives, I had probably posted a cleaner code in the MSDN thread which has ben removed :
// WmiOpenBlockTest.cpp : Defines the entry point for the application.
//
#include "framework.h"
#include "WmiOpenBlockTest.h"
#include <initguid.h>
#include <setupapi.h>
#pragma comment(lib, "setupapi")
#include <devguid.h> // for GUID_DEVCLASS_NET
#define MAX_LOADSTRING 100
//DEVMGR.DLL WmiOpenBlock({a9546a82-feb0-11d0-bd26-00aa00b7b32a}, 0, 0x000001b202456640) STATUS_SUCCESS 0.0001894
//GUID_POWER_DEVICE_WAKE_ENABLE
//MSPower_DeviceWakeEnable
//DEVMGR.DLL WmiOpenBlock(
//{827c0a6f-feb0-11d0-bd26-00aa00b7b32a}, 0, 0x000001b202456478) STATUS_SUCCESS 0.0001258
//GUID_POWER_DEVICE_ENABLE
//DEVMGR.DLL WmiOpenBlock({a14f1c97-8839-4f8a-9996-a28996ebbf1d }, 0, 0x000001b202456808) STATUS_SUCCESS 0.0001184
//MSNdis_DeviceWakeOnMagicPacketOnly
DEFINE_GUID(GUID_POWER_DEVICE_ENABLE, 0x827c0a6fL, 0xfeb0, 0x11d0, 0xbd, 0x26, 0x00, 0xaa, 0x00, 0xb7, 0xb3, 0x2a);
DEFINE_GUID(GUID_POWER_DEVICE_TIMEOUTS, 0xa45da735L, 0xfeb0, 0x11d0, 0xbd, 0x26, 0x00, 0xaa, 0x00, 0xb7, 0xb3, 0x2a);
DEFINE_GUID(GUID_POWER_DEVICE_WAKE_ENABLE, 0xa9546a82L, 0xfeb0, 0x11d0, 0xbd, 0x26, 0x00, 0xaa, 0x00, 0xb7, 0xb3, 0x2a);
DEFINE_GUID(GUID_POWER_DEVICE_WAKE_ON_MAGIC_PACKET_ONLY, 0xa14f1c97L, 0x8839, 0x4f8a, 0x99, 0x96, 0xa2, 0x89, 0x96, 0xeb, 0xbf, 0x1d);
typedef struct _WNODE_HEADER {
ULONG BufferSize;
ULONG ProviderId;
union {
ULONG64 HistoricalContext;
struct {
ULONG Version;
ULONG Linkage;
} DUMMYSTRUCTNAME;
} DUMMYUNIONNAME;
union {
ULONG CountLost;
HANDLE KernelHandle;
LARGE_INTEGER TimeStamp;
} DUMMYUNIONNAME2;
GUID Guid;
ULONG ClientContext;
ULONG Flags;
} WNODE_HEADER, * PWNODE_HEADER;
typedef struct tagWNODE_SINGLE_INSTANCE {
struct _WNODE_HEADER WnodeHeader;
ULONG OffsetInstanceName;
ULONG InstanceIndex;
ULONG DataBlockOffset;
ULONG SizeDataBlock;
UCHAR VariableData[];
} WNODE_SINGLE_INSTANCE, * PWNODE_SINGLE_INSTANCE;
// Global Variables:
HINSTANCE hInst; // current instance
WCHAR szTitle[MAX_LOADSTRING]; // The title bar text
WCHAR szWindowClass[MAX_LOADSTRING]; // the main window class name
// Forward declarations of functions included in this code module:
ATOM MyRegisterClass(HINSTANCE hInstance);
BOOL InitInstance(HINSTANCE, int);
LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
INT_PTR CALLBACK About(HWND, UINT, WPARAM, LPARAM);
int APIENTRY wWinMain(_In_ HINSTANCE hInstance,
_In_opt_ HINSTANCE hPrevInstance,
_In_ LPWSTR lpCmdLine,
_In_ int nCmdShow)
{
UNREFERENCED_PARAMETER(hPrevInstance);
UNREFERENCED_PARAMETER(lpCmdLine);
// TODO: Place code here.
// Initialize global strings
LoadStringW(hInstance, IDS_APP_TITLE, szTitle, MAX_LOADSTRING);
LoadStringW(hInstance, IDC_WMIOPENBLOCKTEST, szWindowClass, MAX_LOADSTRING);
MyRegisterClass(hInstance);
// Perform application initialization:
if (!InitInstance (hInstance, nCmdShow))
{
return FALSE;
}
HACCEL hAccelTable = LoadAccelerators(hInstance, MAKEINTRESOURCE(IDC_WMIOPENBLOCKTEST));
MSG msg;
// Main message loop:
while (GetMessage(&msg, nullptr, 0, 0))
{
if (!TranslateAccelerator(msg.hwnd, hAccelTable, &msg))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
}
return (int) msg.wParam;
}
//
// FUNCTION: MyRegisterClass()
//
// PURPOSE: Registers the window class.
//
ATOM MyRegisterClass(HINSTANCE hInstance)
{
WNDCLASSEXW wcex;
wcex.cbSize = sizeof(WNDCLASSEX);
wcex.style = CS_HREDRAW | CS_VREDRAW;
wcex.lpfnWndProc = WndProc;
wcex.cbClsExtra = 0;
wcex.cbWndExtra = 0;
wcex.hInstance = hInstance;
wcex.hIcon = LoadIcon(hInstance, MAKEINTRESOURCE(IDI_WMIOPENBLOCKTEST));
wcex.hCursor = LoadCursor(nullptr, IDC_ARROW);
wcex.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
wcex.lpszMenuName = MAKEINTRESOURCEW(IDC_WMIOPENBLOCKTEST);
wcex.lpszClassName = szWindowClass;
wcex.hIconSm = LoadIcon(wcex.hInstance, MAKEINTRESOURCE(IDI_SMALL));
return RegisterClassExW(&wcex);
}
//
// FUNCTION: InitInstance(HINSTANCE, int)
//
// PURPOSE: Saves instance handle and creates main window
//
// COMMENTS:
//
// In this function, we save the instance handle in a global variable and
// create and display the main program window.
//
BOOL InitInstance(HINSTANCE hInstance, int nCmdShow)
{
hInst = hInstance; // Store instance handle in our global variable
HWND hWnd = CreateWindowW(szWindowClass, szTitle, WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT, 0, CW_USEDEFAULT, 0, nullptr, nullptr, hInstance, nullptr);
if (!hWnd)
{
return FALSE;
}
ShowWindow(hWnd, nCmdShow);
UpdateWindow(hWnd);
return TRUE;
}
//
// FUNCTION: WndProc(HWND, UINT, WPARAM, LPARAM)
//
// PURPOSE: Processes messages for the main window.
//
// WM_COMMAND - process the application menu
// WM_PAINT - Paint the main window
// WM_DESTROY - post a quit message and return
//
//
LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
switch (message)
{
case WM_COMMAND:
{
int wmId = LOWORD(wParam);
// Parse the menu selections:
switch (wmId)
{
case IDM_ABOUT:
{
typedef HRESULT(WINAPI* WOB) (LPGUID lpGUID, DWORD nDesiredAccess, OUT ULONG* DataBlockObject);
WOB WmiOpenBlock = NULL;
typedef HRESULT(WINAPI* WQSI)(ULONG DataBlockObject, WCHAR* wsInstanceName, __inout ULONG* nInOutBufferSize, OUT PVOID OutBuffer);
WQSI WmiQuerySingleInstance = NULL;
typedef HRESULT(WINAPI* WDITIN)(WCHAR* wsInstanceName, ULONG nSize, WCHAR* wsInstanceId, ULONG);
WDITIN WmiDevInstToInstanceName = NULL;
typedef HRESULT(WINAPI* WSSI)(ULONG DataBlockObject, WCHAR* wsInstanceName, ULONG nVersion, ULONG nValueBufferSize, PVOID ValueBuffer);
WSSI WmiSetSingleInstance = NULL;
HINSTANCE hDLL = LoadLibrary(L"advapi32.dll");
if (hDLL)
{
WmiOpenBlock = (WOB)GetProcAddress(hDLL, "WmiOpenBlock");
if (WmiOpenBlock != NULL)
{
WmiQuerySingleInstance = (WQSI)GetProcAddress(hDLL, "WmiQuerySingleInstanceW");
if (WmiQuerySingleInstance != NULL)
{
WmiSetSingleInstance = (WSSI)GetProcAddress(hDLL, "WmiSetSingleInstanceW");
if (WmiSetSingleInstance != NULL)
{
WmiDevInstToInstanceName = (WDITIN)GetProcAddress(hDLL, "WmiDevInstToInstanceNameW");
}
}
}
//FreeLibrary(hDLL);
}
ULONG nBufferSize = 0;
ULONG hWmiBlock;
BYTE* pWmiInstData;
DWORD nError;
ULONG nVersion;
GUID wmiGuid = GUID_POWER_DEVICE_ENABLE;
HRESULT hr;
HDEVINFO hdi;
SP_DEVINFO_DATA did;
BOOL bSuccess;
#define MAX_COMP_INSTID 4096
#define MAX_COMP_DESC 4096
LPTSTR lpszCompInstanceId;
LPTSTR lpszCompDescription;
DWORD dwIndex = 0;
DWORD dwRegType;
//hdi = SetupDiGetClassDevs(&GUID_DEVCLASS_NET, NULL, NULL, DIGCF_DEVICEINTERFACE | DIGCF_PRESENT);
hdi = SetupDiGetClassDevs(&GUID_DEVCLASS_NET, NULL, NULL, DIGCF_PRESENT);
lpszCompInstanceId = (LPTSTR)HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, (MAX_COMP_INSTID * sizeof(WCHAR)));
lpszCompDescription = (LPTSTR)HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, (MAX_COMP_DESC * sizeof(WCHAR)));
if ((NULL != lpszCompInstanceId) && (NULL != lpszCompDescription) && (INVALID_HANDLE_VALUE != hdi))
{
while (did.cbSize = sizeof(SP_DEVINFO_DATA), SetupDiEnumDeviceInfo(hdi, dwIndex, &did))
{
dwIndex++;
bSuccess = SetupDiGetDeviceInstanceId(hdi, &did, lpszCompInstanceId, MAX_COMP_INSTID, NULL);
if (bSuccess)
{
bSuccess = SetupDiGetDeviceRegistryProperty(hdi, &did, SPDRP_DEVICEDESC, &dwRegType, (BYTE*)lpszCompDescription, MAX_COMP_DESC, NULL);
if (wcsstr(lpszCompDescription, L"Realtek PCIe GbE Family Controller"))
{
{
nError = WmiOpenBlock((LPGUID)&wmiGuid, 0, &hWmiBlock);
WCHAR wmiInstance[MAX_PATH];
WmiDevInstToInstanceName(wmiInstance, sizeof(wmiInstance) / sizeof(wmiInstance[0]), (WCHAR*)lpszCompInstanceId, 0);
nError = WmiQuerySingleInstance(hWmiBlock, wmiInstance, &nBufferSize, NULL);
if (nBufferSize && nError == ERROR_INSUFFICIENT_BUFFER)
{
pWmiInstData = new BYTE[nBufferSize];
if (pWmiInstData)
{
nError = WmiQuerySingleInstance(hWmiBlock, wmiInstance, &nBufferSize, pWmiInstData);
nVersion = ((PWNODE_SINGLE_INSTANCE)pWmiInstData)->WnodeHeader.Version;
BOOLEAN bNewValue = FALSE;
//BOOLEAN bNewValue = TRUE;
nError = WmiSetSingleInstance(hWmiBlock, wmiInstance, nVersion, sizeof(bNewValue), &bNewValue);
nError = nError;
}
else
{
nError = ERROR_NOT_ENOUGH_MEMORY;
}
}
}
{
//nError = WmiOpenBlock((LPGUID)&GUID_POWER_DEVICE_WAKE_ENABLE, 0, &hWmiBlock);
nError = WmiOpenBlock((LPGUID)&GUID_POWER_DEVICE_WAKE_ON_MAGIC_PACKET_ONLY, 0, &hWmiBlock);
WCHAR wmiInstance[MAX_PATH];
WmiDevInstToInstanceName(wmiInstance, sizeof(wmiInstance) / sizeof(wmiInstance[0]), (WCHAR*)lpszCompInstanceId, 0);
nError = WmiQuerySingleInstance(hWmiBlock, wmiInstance, &nBufferSize, NULL);
if (nBufferSize && nError == ERROR_INSUFFICIENT_BUFFER)
{
pWmiInstData = new BYTE[nBufferSize];
if (pWmiInstData)
{
nError = WmiQuerySingleInstance(hWmiBlock, wmiInstance, &nBufferSize, pWmiInstData);
nVersion = ((PWNODE_SINGLE_INSTANCE)pWmiInstData)->WnodeHeader.Version;
BOOLEAN bNewValue = FALSE;
//BOOLEAN bNewValue = TRUE;
nError = WmiSetSingleInstance(hWmiBlock, wmiInstance, nVersion, sizeof(bNewValue), &bNewValue);
nError = nError;
}
else
{
nError = ERROR_NOT_ENOUGH_MEMORY;
}
}
}
}
}
}
SetupDiDestroyDeviceInfoList(hdi);
}
if (lpszCompInstanceId)
HeapFree(GetProcessHeap(), 0, lpszCompInstanceId);
if (lpszCompDescription)
HeapFree(GetProcessHeap(), 0, lpszCompDescription);
}
{
// "C:\Windows\System32\DeviceProperties.exe" "PCI\VEN_10EC&DEV_8168&SUBSYS_E0001458&REV_0C\4&1dcb0711&0&00E2"
// POWERCFG / DEVICEENABLEWAKE "Device name"
typedef ULONG(WINAPI*PWMIOPENBLOCK)(LPGUID, ULONG, OUT LONG*);
PWMIOPENBLOCK WmiOpenBlock;
typedef ULONG(WINAPI* PWMICLOSEBLOCK)(LONG);
PWMICLOSEBLOCK WmiCloseBlock;
typedef ULONG(WINAPI* PWMIQUERYSINGLEINSTANCE)(LONG, LPCWSTR, PULONG, PVOID);
PWMIQUERYSINGLEINSTANCE WmiQuerySingleInstance;
typedef ULONG(WINAPI* PWMISETSINGLEINSTANCE)(LONG, LPCWSTR, ULONG, ULONG, PVOID);
PWMISETSINGLEINSTANCE WmiSetSingleInstance;
typedef ULONG(WINAPI* PWMIDEVINSTTOINSTANCENAME)(PWCHAR, ULONG, PWCHAR, ULONG);
PWMIDEVINSTTOINSTANCENAME WmiDevInstToInstanceName;
HINSTANCE hDLL = LoadLibrary(L"Advapi32.dll");
WmiOpenBlock = (PWMIOPENBLOCK)GetProcAddress(hDLL, "WmiOpenBlock");
WmiCloseBlock = (PWMICLOSEBLOCK)GetProcAddress(hDLL, "WmiCloseBlock");
WmiQuerySingleInstance = (PWMIQUERYSINGLEINSTANCE)GetProcAddress(hDLL, "WmiQuerySingleInstanceW");
WmiSetSingleInstance = (PWMISETSINGLEINSTANCE)GetProcAddress(hDLL, "WmiSetSingleInstanceW");
WmiDevInstToInstanceName = (PWMIDEVINSTTOINSTANCENAME)GetProcAddress(hDLL, "WmiDevInstToInstanceNameW");
if (WmiOpenBlock != NULL && WmiCloseBlock && WmiQuerySingleInstance && WmiSetSingleInstance && WmiDevInstToInstanceName)
{
LONG hWmiHandle;
WCHAR pszDeviceId[256] = L"";
HRESULT hr = WmiOpenBlock((LPGUID)&GUID_POWER_DEVICE_ENABLE, GENERIC_READ, &hWmiHandle);
if (hr == ERROR_SUCCESS)
{
#define MAX_DEVICE_ID_LEN 200
WCHAR wsDeviceId[MAX_DEVICE_ID_LEN + 2] = TEXT("PCI\\VEN_10EC&DEV_8168&SUBSYS_E0001458&REV_0C\\4&1dcb0711&0&00E2");
WCHAR m_DevInstId[MAX_DEVICE_ID_LEN + 2];
int len = lstrlen(wsDeviceId);
hr = WmiDevInstToInstanceName(m_DevInstId, len + 3, (PTCHAR)wsDeviceId, 0);
////if (hr == ERROR_SUCCESS)
{
ULONG m_WmiInstDataSize;
BYTE* m_pWmiInstData;
ULONG BufferSize = 0;
hr = WmiQuerySingleInstance(hWmiHandle, m_DevInstId, &BufferSize, NULL);
if (BufferSize && hr == ERROR_INSUFFICIENT_BUFFER)
{
m_WmiInstDataSize = BufferSize;
m_pWmiInstData = new BYTE[BufferSize];
if (m_pWmiInstData)
{
ULONG nDataBlockSize = 0;
hr = WmiQuerySingleInstance(hWmiHandle, m_DevInstId, &BufferSize, m_pWmiInstData);
///* if (ERROR_SUCCESS == hr &&
// nDataBlockSize == ((PWNODE_SINGLE_INSTANCE)m_pWmiInstData)->SizeDataBlock)*/
if (hr == ERROR_SUCCESS)
{
PWNODE_SINGLE_INSTANCE p = (PWNODE_SINGLE_INSTANCE)m_pWmiInstData;
nDataBlockSize = ((PWNODE_SINGLE_INSTANCE)m_pWmiInstData)->SizeDataBlock;
ULONG nVersion;
nVersion = ((PWNODE_SINGLE_INSTANCE)m_pWmiInstData)->WnodeHeader.Version;
BOOL fEnable = *((BOOLEAN*)(m_pWmiInstData + ((PWNODE_SINGLE_INSTANCE)m_pWmiInstData)->DataBlockOffset));
BOOLEAN fNewValue = 0;
int n = sizeof(fNewValue);
hr = WmiSetSingleInstance(hWmiHandle, m_DevInstId, nVersion, sizeof(fNewValue), &fNewValue);
return TRUE;
}
}
}
}
}
}
}
// DialogBox(hInst, MAKEINTRESOURCE(IDD_ABOUTBOX), hWnd, About);
break;
case IDM_EXIT:
DestroyWindow(hWnd);
break;
default:
return DefWindowProc(hWnd, message, wParam, lParam);
}
}
break;
case WM_PAINT:
{
PAINTSTRUCT ps;
HDC hdc = BeginPaint(hWnd, &ps);
// TODO: Add any drawing code that uses hdc here...
EndPaint(hWnd, &ps);
}
break;
case WM_DESTROY:
PostQuitMessage(0);
break;
default:
return DefWindowProc(hWnd, message, wParam, lParam);
}
return 0;
}
// Message handler for about box.
INT_PTR CALLBACK About(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)
{
UNREFERENCED_PARAMETER(lParam);
switch (message)
{
case WM_INITDIALOG:
return (INT_PTR)TRUE;
case WM_COMMAND:
if (LOWORD(wParam) == IDOK || LOWORD(wParam) == IDCANCEL)
{
EndDialog(hDlg, LOWORD(wParam));
return (INT_PTR)TRUE;
}
break;
}
return (INT_PTR)FALSE;
}