Archive for the WINDOWS Category

Note: using SetupAPI to find a special device on Windows system,

Posted in programming, WINDOWS with tags on 2015 年 01 月 19 日 by Kun-Yi
// EnumHIDDevice.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#pragma comment(lib, "setupapi")

bool chkDigitizer(void)
{
    HDEVINFO hDevInfo = INVALID_HANDLE_VALUE;
    bool bFind = false;
    hDevInfo = SetupDiGetClassDevs(
        &GUID_DEVCLASS_HIDCLASS,
        NULL,
        NULL,
        DIGCF_PRESENT);
#if 0
    // get special device instance
    hDevInfo = SetupDiGetClassDevs(
        &GUID_DEVCLASS_HIDCLASS,
        TEXT("HID\\VID_0458&PID_501E&COL05\\7&198536FE&0&0004"),
        NULL,
        DIGCF_PRESENT | DIGCF_DEVICEINTERFACE);
#endif

    if (INVALID_HANDLE_VALUE == hDevInfo)
        return false;

    SP_DEVINFO_DATA DeviceInfoData;
    ULONG DeviceIndex;

    ZeroMemory(&DeviceInfoData, sizeof(SP_DEVINFO_DATA));
    DeviceInfoData.cbSize = sizeof(SP_DEVINFO_DATA);
    DeviceIndex = 0;
    while (SetupDiEnumDeviceInfo(hDevInfo, DeviceIndex, &DeviceInfoData))
    {
        DeviceIndex++;

        TCHAR InstanceID[MAX_PATH + 1];
        DWORD reqSize = 0;
        TCHAR CheckLists[][MAX_PATH + 1] = {
            TEXT("HID\\VID_0458&PID_5020&COL05"), // new
            TEXT("HID\\VID_0458&PID_501E&COL05"), // older
        };

        SetupDiGetDeviceInstanceId(hDevInfo, &DeviceInfoData, InstanceID, sizeof(InstanceID), &reqSize);
        for (ULONG i = 0; i < 2; i++)
            if (NULL != _tcsstr(InstanceID, CheckLists[i]))
            {
                bFind = true;
                break;
            }
    }
    if (INVALID_HANDLE_VALUE != hDevInfo)
        SetupDiDestroyDeviceInfoList(hDevInfo);

    return bFind;
}

int _tmain(int argc, _TCHAR* argv[])
{

    if (chkDigitizer())
        _tprintf(TEXT("Find out a digitizer device on system\n"));
    else
        _tprintf(TEXT("no digitizer on system\n"));

	return 0;
}

Note: create a named EVENT for share with a correct access permission on Win 7 & later

Posted in WINDOWS with tags , , , on 2014 年 12 月 08 日 by Kun-Yi

最近遇到某個工作項目需要寫個Service 來與一般的應用程式互動, 本來想用Pipe or File mapping, 做IPC
但是實際開始工作後發現 他們之間透過Event 就可以處理,
但是Windows 從Vista 開始引入UAC後 開始加強系統各種Security(可以想成linux enabled SELinux)
對各種Windows 內的 Object 都有了權限要求, 因此建立Object實際要指定好權限
這篇簡單的SDDL 產生一個 kernel object 的權限片斷程式碼,
要觀察建立好的Kernel object 可以直接透過 Process Explorer開Low Panel並且指定Handle 這樣就可以

    wchar_t  myEventName[] = L"Global\\MyEventTest";
    SECURITY_ATTRIBUTES sa = { 0 };
    PSECURITY_DESCRIPTOR pSd = NULL;
    ConvertStringSecurityDescriptorToSecurityDescriptor(
	L"D:"			// Discretionary ACL
				// ref. http://msdn.microsoft.com/en-us/library/windows/desktop/ms686670(v=vs.85).aspx
	L"(A;;0x001F0003;;;BA)"	// EVENT_FULL_ACCESS, Built-in Administrators
	L"(A;;0x00100002;;;AU)",// SYNCHRONIZE & MODIFY_EVENT_STATE, Authenticated users
	SDDL_REVISION_1, &pSd, NULL);
	sa.nLength = sizeof(sa);
	sa.bInheritHandle = FALSE;
	sa.lpSecurityDescriptor = pSd;

	// create a auto-reset event for reload configuration
    m_hSignals[EVENT_RELOAD_CONF] = CreateEvent(&sa, FALSE, FALSE, myEventName);
    if (m_hSignals[EVENT_RELOAD_CONF] == NULL)
    {
	throw GetLastError();
    }
    LocalFree(pSd); // release 

ref. http://stackoverflow.com/questions/19049412/how-to-add-synchronisation-right-in-a-sddl-string-for-createevent