Windows Biometric Framework fail - visual-studio

My friend asked me to write a simple program that captures a fingerprint from the built-in reader on a computer and prints out some kind of identifier. I can choose operating system myself, like a laptop with Windows or Linux, or an Android phone.
I thought that would be simple, surely there are many API:s for this, and I noticed that Microsoft themselves actually provides an API for it. Since I can log into my win10 laptop with the fingerprint reader, I know that the reader works.
For some reason the example that Microsoft themselves provide in their documention does not work for me. https://learn.microsoft.com/en-us/windows/desktop/api/Winbio/nf-winbio-winbiocapturesample
I suppose that the people writing those pages forgot to mention some important aspect or step, perhaps there is a way to add permission somewhere in Visual Studio.
After rewriting and trying many things, at least I get ONE step further in the process, but it still fails.
Here is the current version
#include "pch.h"
#include <iostream>
#include "Windows.h"
#include "Stdio.h"
#include "Conio.h"
#include "Winbio.h"
HRESULT CaptureSample();
void capture(WINBIO_SESSION_HANDLE sessionHandle, int flag);
int main()
{
std::cout << "Hello World!\n";
HRESULT x = CaptureSample();
}
HRESULT CaptureSample()
{
HRESULT hr = S_OK;
WINBIO_SESSION_HANDLE sessionHandle = NULL;
WINBIO_REJECT_DETAIL rejectDetail = 0;
// Connect to the system pool.
hr = WinBioOpenSession(
WINBIO_TYPE_FINGERPRINT, // Service provider
WINBIO_POOL_SYSTEM, // Pool type
WINBIO_FLAG_DEFAULT, // Access: Capture raw data
NULL, // Array of biometric unit IDs
0, // Count of biometric unit IDs
WINBIO_DB_DEFAULT, // Default database
&sessionHandle // [out] Session handle
);
if (FAILED(hr))
{
wprintf_s(L"WinBioOpenSession failed. hr = 0x%x\n", hr);
goto e_Exit;
}
wprintf_s(L"Start my fingerprint capturing...\n");
capture(sessionHandle, WINBIO_DATA_FLAG_INTEGRITY);
capture(sessionHandle, WINBIO_DATA_FLAG_PRIVACY);
capture(sessionHandle, WINBIO_DATA_FLAG_SIGNED);
capture(sessionHandle, WINBIO_DATA_FLAG_OPTION_MASK_PRESENT);
capture(sessionHandle, WINBIO_DATA_FLAG_RAW);
capture(sessionHandle, WINBIO_DATA_FLAG_INTERMEDIATE);
capture(sessionHandle, WINBIO_DATA_FLAG_PROCESSED);
hr = WinBioEnrollCapture(sessionHandle, &rejectDetail);
wprintf_s(L"WinBioEnrollCapture hr=%x rejection = %d\n", hr, rejectDetail);
if (sessionHandle != NULL)
{
WinBioCloseSession(sessionHandle);
sessionHandle = NULL;
}
e_Exit:
wprintf_s(L"\n Press any key to exit...");
_getch();
return hr;
}
void capture(WINBIO_SESSION_HANDLE sessionHandle, int flag) {
WINBIO_UNIT_ID unitId = 0;
WINBIO_REJECT_DETAIL rejectDetail = 0;
PWINBIO_BIR sample = NULL;
SIZE_T sampleSize = 0;
wprintf_s(L"\n Calling WinBioCaptureSample. Flag = %d.\n", flag);
HRESULT hr = WinBioCaptureSample(
sessionHandle,
WINBIO_PURPOSE_IDENTIFY,
flag,
&unitId,
&sample,
&sampleSize,
&rejectDetail
);
if (FAILED(hr))
{
if (hr == WINBIO_E_BAD_CAPTURE)
{
wprintf_s(L"\n Bad capture; reason: %d\n", rejectDetail);
}
else if (hr == E_ACCESSDENIED)
{
wprintf_s(L"\n WinBioCaptureSample failed, access denied.");
}
else
{
wprintf_s(L"\n WinBioCaptureSample failed. hr = 0x%x\n", hr);
}
goto e_Exit;
}
wprintf_s(L"\n Swipe processed - Unit ID: %d\n", unitId);
wprintf_s(L"\n Captured %d bytes.\n", sampleSize);
e_Exit:
if (sample != NULL)
{
WinBioFree(sample);
sample = NULL;
}
}
and here is the result of running it:
Hello World!
Start my fingerprint capturing...
Calling WinBioCaptureSample. Flag = 1.
WinBioCaptureSample failed, access denied.
Calling WinBioCaptureSample. Flag = 2.
WinBioCaptureSample failed, access denied.
Calling WinBioCaptureSample. Flag = 4.
WinBioCaptureSample failed, access denied.
Calling WinBioCaptureSample. Flag = 8.
WinBioCaptureSample failed, access denied.
Calling WinBioCaptureSample. Flag = 32.
WinBioCaptureSample failed, access denied.
Calling WinBioCaptureSample. Flag = 64.
WinBioCaptureSample failed, access denied.
Calling WinBioCaptureSample. Flag = 128.
WinBioCaptureSample failed, access denied.WinBioEnrollCapture hr=8009802c rejection = 0
Press any key to exit...
What am I missing?

I think you should try using another API, though your objective is not clear here. You can check the CloudABIS™ which is superscalar, biometrics-as-a-service (BaaS) matching system for fast deployment at lower costs. The API is provided by M2SYS TEchnology and they have an vast array of hardware also.

Related

How to access Windows.Services.Store namespace from a Win32 app converted to UWP with the "Project Centennial converter" to enable in-app purchases?

I have a native C++/MFC app that is developed in VS 2008, no .NET stuff, that I converted into a UWP app using the Project Centennial converter. So now I have an .appx package that runs in Windows 10 v 1607 as a UWP app.
My next goal is to add in-app purchase support before submission to Windows Store.
The question though is how do I access Windows.Services.Store namespace from a pure Win32 app from a native C or C++ code?
Use WRL. Here's an example on how to purchase an in app purchase:
#include <windows.h>
#include <Windows.Services.Store.h>
#include <wrl.h>
using namespace ABI::Windows::Foundation;
using namespace ABI::Windows::Services::Store;
using namespace Microsoft::WRL;
using namespace Microsoft::WRL::Wrappers;
#define CheckHr(hr) do { if (FAILED(hr)) __debugbreak(); } while (false)
const wchar_t kItemFriendlyName[] = L"10 coins";
const wchar_t kItemStoreId[] = L"ten_coins";
void OnPurchaseOperationDone(IAsyncOperation<StorePurchaseResult*>* operation, AsyncStatus status);
void Purchase10Coins()
{
ComPtr<IStoreContextStatics> storeContextStatics;
auto hr = RoGetActivationFactory(HStringReference(L"Windows.Services.Store.StoreContext").Get(), __uuidof(storeContextStatics), &storeContextStatics);
CheckHr(hr);
ComPtr<IStoreContext> storeContext;
hr = storeContextStatics->GetDefault(&storeContext);
CheckHr(hr);
ComPtr<IStorePurchasePropertiesFactory> purchasePropertiesFactory;
hr = RoGetActivationFactory(HStringReference(L"Windows.Services.Store.StorePurchaseProperties").Get(), __uuidof(purchasePropertiesFactory), &purchasePropertiesFactory);
CheckHr(hr);
ComPtr<IStorePurchaseProperties> purchaseProperties;
hr = purchasePropertiesFactory->Create(HStringReference(kItemFriendlyName).Get(), &purchaseProperties);
CheckHr(hr);
ComPtr<IAsyncOperation<StorePurchaseResult*>> purchaseOperation;
hr = storeContext->RequestPurchaseWithPurchasePropertiesAsync(HStringReference(kItemStoreId).Get(), purchaseProperties.Get(), &purchaseOperation);
CheckHr(hr);
// Change the following line to call Callback<IAsyncOperationCompletedHandler<StorePurchaseResult*>> if you want the callback to happen back on the UI thread
// Implementing FtmBase allows it to fire on the thread the operation finished
auto onCompletedCallback = Callback<Implements<RuntimeClassFlags<ClassicCom>, IAsyncOperationCompletedHandler<StorePurchaseResult*>, FtmBase>>(
[](IAsyncOperation<StorePurchaseResult*>* operation, AsyncStatus status)
{
OnPurchaseOperationDone(operation, status);
return S_OK;
});
hr = purchaseOperation->put_Completed(onCompletedCallback.Get());
CheckHr(hr);
}
void OnPurchaseOperationDone(IAsyncOperation<StorePurchaseResult*>* operation, AsyncStatus status)
{
if (status != AsyncStatus::Completed)
{
// It failed for some reason. Find out why.
ComPtr<IAsyncInfo> asyncInfo;
auto hr = operation->QueryInterface(__uuidof(asyncInfo), &asyncInfo);
CheckHr(hr);
HRESULT errorCode;
hr = asyncInfo->get_ErrorCode(&errorCode);
CheckHr(hr);
// Do something with the errorCode
// Return once error is handled
return;
}
ComPtr<IStorePurchaseResult> purchaseResult;
auto hr = operation->GetResults(&purchaseResult);
CheckHr(hr);
StorePurchaseStatus purchaseStatus;
hr = purchaseResult->get_Status(&purchaseStatus);
CheckHr(hr);
switch (purchaseStatus)
{
case StorePurchaseStatus_Succeeded:
case StorePurchaseStatus_AlreadyPurchased:
// Success. Product was purchased
break;
case StorePurchaseStatus_NotPurchased:
// User canceled the purchase
break;
case StorePurchaseStatus_NetworkError:
// The device could not reach windows store
break;
case StorePurchaseStatus_ServerError:
// Something broke on the server
break;
}
}
Here's how to check if application is on trial:
void CheckIsTrial(std::function<void(bool)> onCompleted)
{
ComPtr<IStoreContextStatics> storeContextStatics;
auto hr = RoGetActivationFactory(HStringReference(L"Windows.Services.Store.StoreContext").Get(), __uuidof(storeContextStatics), &storeContextStatics);
CheckHr(hr);
ComPtr<IStoreContext> storeContext;
hr = storeContextStatics->GetDefault(&storeContext);
CheckHr(hr);
ComPtr<IAsyncOperation<StoreAppLicense*>> getLicenseOperation;
hr = storeContext->GetAppLicenseAsync(&getLicenseOperation);
CheckHr(hr);
hr = getLicenseOperation->put_Completed(Callback<Implements<RuntimeClassFlags<ClassicCom>, IAsyncOperationCompletedHandler<StoreAppLicense*>, FtmBase>>(
[onCompleted{ std::move(onCompleted) }](IAsyncOperation<StoreAppLicense*>* operation, AsyncStatus status)
{
if (status != AsyncStatus::Completed)
{
// It failed for some reason. Find out why.
ComPtr<IAsyncInfo> asyncInfo;
auto hr = operation->QueryInterface(__uuidof(asyncInfo), &asyncInfo);
CheckHr(hr);
HRESULT errorCode;
hr = asyncInfo->get_ErrorCode(&errorCode);
CheckHr(hr);
// Do something with the errorCode
// Return once error is handled
return S_OK;
}
ComPtr<IStoreAppLicense> appLicense;
auto hr = operation->GetResults(&appLicense);
CheckHr(hr);
boolean isActive, isTrial = false;
hr = appLicense->get_IsActive(&isActive);
CheckHr(hr);
if (isActive)
{
hr = appLicense->get_IsTrial(&isTrial);
CheckHr(hr);
}
onCompleted(static_cast<bool>(isActive));
return S_OK;
}).Get());
CheckHr(hr);
}
See here: https://msdn.microsoft.com/en-us/library/windows/apps/Windows.Services.Store.StoreContext.aspx
It states:
Note  In a Windows desktop application that uses the Desktop Bridge,
you must add some additional code to configure the StoreContext object
before your app can use this object. For more information, see Using
the StoreContext class in a desktop application that uses the Desktop
Bridge.
https://msdn.microsoft.com/windows/uwp/monetize/in-app-purchases-and-trials#desktop
With the following changes, this compiled and worked for me:
1) #include <utility.h>
2) write an onCompleted handler:
void onCompleted(bool bActiveLicense)
{
// App has active license or not
}
3) Change capture as follows:
[=, onCompleted{ std::move(onCompleted) }]

Parsing events in real time ETW consumer on Windows

We are working on ETW real time consumer application by referring to https://msdn.microsoft.com/en-us/library/windows/desktop/aa364157(v=vs.85).aspx sample.
We have been successful getting callback and print "ParentGuid" of EVENT_TRACE structure within callback. However we are getting MofData pointer as always NULL and MofLength as always 0 (zero).
On the other hand if we use non real time ETW consumer method i.e. file mode; reading from .etl file we are able to get valid MofData pointer.
We are trying to consume Kernel events such as CPU usage, DISK IO details from Events in real time.
So does it mean we cannot consume Kernel events in real time? Can some one suggest why we are not getting valid pointer/MofData?
// ConsoleApplication5.cpp : Defines the entry point for the console application.
//
//Turns the DEFINE_GUID for EventTraceGuid into a const.
#define INITGUID
#include "stdafx.h"
#include <windows.h>
#include <stdio.h>
#include <evntrace.h>
#define LOGSESSION_NAME L"power"
// Used to calculate CPU usage
ULONG g_TimerResolution = 0;
void WINAPI ProcessEvent(PEVENT_TRACE pEvent);
void wmain(void)
{
ULONG status = ERROR_SUCCESS;
EVENT_TRACE_LOGFILE trace;
TRACE_LOGFILE_HEADER* pHeader = &trace.LogfileHeader;
TRACEHANDLE hTrace = 0;
HRESULT hr = S_OK;
// Identify the log file from which you want to consume events
// and the callbacks used to process the events and buffers.
ZeroMemory(&trace, sizeof(EVENT_TRACE_LOGFILE));
trace.LoggerName = (LPWSTR)LOGSESSION_NAME;
trace.CurrentTime = 0;
trace.BuffersRead = 0;
trace.BufferSize = 0;
trace.Filled = 0;
trace.EventsLost = 0;
trace.Context = NULL;
trace.ProcessTraceMode = PROCESS_TRACE_MODE_REAL_TIME | PROCESS_TRACE_MODE_EVENT_RECORD;
trace.EventCallback = (PEVENT_CALLBACK)(ProcessEvent);
trace.BufferCallback = (PEVENT_TRACE_BUFFER_CALLBACK)(ProcessBuffer);
hTrace = OpenTrace(&trace);
if ((TRACEHANDLE)INVALID_HANDLE_VALUE == hTrace)
{
wprintf(L"OpenTrace failed with %lu\n", GetLastError());
goto cleanup;
}
if (pHeader->TimerResolution > 0)
{
g_TimerResolution = pHeader->TimerResolution / 10000;
}
wprintf(L"Number of events lost: %lu\n", pHeader->EventsLost);
// Use pHeader to access all fields prior to LoggerName.
// Adjust pHeader based on the pointer size to access
// all fields after LogFileName. This is required only if
// you are consuming events on an architecture that is
// different from architecture used to write the events.
if (pHeader->PointerSize != sizeof(PVOID))
{
pHeader = (PTRACE_LOGFILE_HEADER)((PUCHAR)pHeader +
2 * (pHeader->PointerSize - sizeof(PVOID)));
}
wprintf(L"Number of buffers lost: %lu\n\n", pHeader->BuffersLost);
status = ProcessTrace(&hTrace, 1, 0, 0);
if (status != ERROR_SUCCESS && status != ERROR_CANCELLED)
{
wprintf(L"ProcessTrace failed with %lu\n", status);
goto cleanup;
}
cleanup:
if ((TRACEHANDLE)INVALID_HANDLE_VALUE != hTrace)
{
status = CloseTrace(hTrace);
}
}
VOID WINAPI ProcessEvent(PEVENT_TRACE pEvent)
{
PBYTE pEventData = NULL;
pEventData = (PBYTE)(pEvent->MofData);
printf("\n hi%d", pEventData);
printf("\n length %d", pEvent->MofLength);
}

C++ - Adding task using Task Scheduler 1.0 Interfaces creates non runnable task

So I'm writing a c++ code that its goal is to add a simple task to the task scheduler.
The application is to be run in windows XP so I'm using Task Scheduler 1.0 Interfaces.
I followed the example, C/C++ Code Example: Creating a Task Using NewWorkItem, and tweaked it a little to fit my needs.
#include <windows.h>
#include <initguid.h>
#include <ole2.h>
#include <mstask.h>
#include <msterr.h>
#include <objidl.h>
#include <wchar.h>
#include <stdio.h>
int main(int argc, char **argv)
{
HRESULT hr = S_OK;
ITaskScheduler *pITS;
/////////////////////////////////////////////////////////////////
// Call CoInitialize to initialize the COM library and then
// call CoCreateInstance to get the Task Scheduler object.
/////////////////////////////////////////////////////////////////
hr = CoInitialize(NULL);
if (SUCCEEDED(hr))
{
hr = CoCreateInstance(CLSID_CTaskScheduler,
NULL,
CLSCTX_INPROC_SERVER,
IID_ITaskScheduler,
(void **) &pITS);
if (FAILED(hr))
{
CoUninitialize();
return 1;
}
}
else
{
return 1;
}
/////////////////////////////////////////////////////////////////
// Call ITaskScheduler::NewWorkItem to create new task.
/////////////////////////////////////////////////////////////////
LPCWSTR pwszTaskName;
ITask *pITask;
IPersistFile *pIPersistFile;
pwszTaskName = L"Test Task";
hr = pITS->NewWorkItem(pwszTaskName, // Name of task
CLSID_CTask, // Class identifier
IID_ITask, // Interface identifier
(IUnknown**)&pITask); // Address of task
// interface
pITS->Release(); // Release object
if (FAILED(hr))
{
CoUninitialize();
fprintf(stderr, "Failed calling NewWorkItem, error = 0x%x\n",hr);
return 1;
}
// set exe path
hr = pITask->SetApplicationName("some_exe.exe");
if (FAILED(hr))
{
CoUninitialize();
return 1;
}
/////////////////////////////////////////////////////////////////
// Call IUnknown::QueryInterface to get a pointer to
// IPersistFile and IPersistFile::Save to save
// the new task to disk.
/////////////////////////////////////////////////////////////////
hr = pITask->QueryInterface(IID_IPersistFile,
(void **)&pIPersistFile);
pITask->Release();
if (FAILED(hr))
{
CoUninitialize();
fprintf(stderr, "Failed calling QueryInterface, error = 0x%x\n",hr);
return 1;
}
hr = pIPersistFile->Save(NULL,
TRUE);
pIPersistFile->Release();
if (FAILED(hr))
{
CoUninitialize();
fprintf(stderr, "Failed calling Save, error = 0x%x\n",hr);
return 1;
}
CoUninitialize();
printf("Created task.\n");
return 0;
}
The tweak is basically adding a call to ITask::SetApplicationName method which I don't think is related to my issue.
The code compiles and runs well. I can see that a task was added.
Problem
When I try to run the task I get:
Could not start
Inspecting file SchedLgU.txt I see:
""Test Task.job" (some_exe.exe) 12/9/2014 2:23:42 AM ** ERROR **
The attempt to retrieve account information for the specified task failed; therefore, the task did not run. Either an error occurred, or no account information existed for the task.
The specific error is:
0x8004130f: No account information could be found in the Task Scheduler security database for the task indicated..job" (some_exe.exe) 12/9/2014 2:23:42 AM ** ERROR **
The attempt to retrieve account information for the specified task failed; therefore, the task did not run. Either an error occurred, or no account information existed for the task.
The specific error is:
0x8004130f: No account information could be found in the Task Scheduler security database for the task indicated.
I can see here that the cause might be due to:
The account credentials are missing.
Any ideas?
I found the issue. This line was missing:
hr = pITask->SetAccountInformation(L"", NULL);
Disclaimer
This only works in administrator user accounts. I haven't figured out a way to make it work in non administrator accounts.

process started from service with CreateProcessWithLogonW terminates right away

In a test framework process A has to start process B under different user credentials (say, _limited_user) using CreateProcessWithLogonW API. lpStartupInfo->lpDesktop is NULL, so process B is supposed to run in the same desktop and window station as process A.
Everything works fine when process A is started manually (as _glagolig). But when process A is started by the test framework service (running under designated test framework’s user account _test_framework) that does not work. CreateProcessWithLogonW returns success but process B is unable to do any work. It terminates right away apparently because its conhost.exe fails to initialize user32.dll and returns 0xC0000142 (I got that from SysInternals’ procmon.exe logs). So it looks like the problem is with desktop/window station access.
I would like to understand the root cause. It is not clear what makes test framework service’s desktop/window station objects different from those of a user that logged in manually.
Also I would like to find a workaround while keeping the overall scheme the same (test framework’s service under account _test_framework has to start process B under _limited_user).
Addendum: according to the documentation, it should be possible to use CreateProcessAsUser without going through these steps, provided you don't want the new process to interact with the user. I haven't tested this yet, but assuming it is true, that would be a much simpler solution for many scenarios.
It turns out that Microsoft has already provided sample code to manipulate window station and desktop access rights, under the title Starting an Interactive Client Process in C++. As of Windows Vista, starting a subprocess in the default window station is no longer enough to allow the subprocess to interact with the user, but it does allow the subprocess to run with alternate user credentials.
I should note that Microsoft's code uses LogonUser and CreateProcessAsUser rather than CreateProcessWithLogonW. This does mean that the service will need SE_INCREASE_QUOTA_NAME privilege and possibly SE_ASSIGNPRIMARYTOKEN_NAME. It may be preferable to substitute CreateProcessWithTokenW which only requires SE_IMPERSONATE_NAME. I don't recommend the use of CreateProcessWithLogonW in this context because it doesn't allow you to access the logon SID prior to launching the subprocess.
I wrote a minimal service to demonstrate using Microsoft's sample code:
/*******************************************************************/
#define _WIN32_WINNT 0x0501
#include <windows.h>
/*******************************************************************/
// See http://msdn.microsoft.com/en-us/library/windows/desktop/aa379608%28v=vs.85%29.aspx
// "Starting an Interactive Client Process in C++"
BOOL AddAceToWindowStation(HWINSTA hwinsta, PSID psid);
BOOL AddAceToDesktop(HDESK hdesk, PSID psid);
BOOL GetLogonSID (HANDLE hToken, PSID *ppsid);
VOID FreeLogonSID (PSID *ppsid);
BOOL StartInteractiveClientProcess (
LPTSTR lpszUsername, // client to log on
LPTSTR lpszDomain, // domain of client's account
LPTSTR lpszPassword, // client's password
LPTSTR lpCommandLine // command line to execute
);
/*******************************************************************/
const wchar_t displayname[] = L"Demo service for CreateProcessWithLogonW";
const wchar_t servicename[] = L"demosvc-createprocesswithlogonw";
DWORD dwWin32ExitCode = 0, dwServiceSpecificExitCode = 0;
/*******************************************************************/
#define EXCEPTION_USER 0xE0000000
#define FACILITY_USER_DEMOSVC 0x0001
#define EXCEPTION_USER_LINENUMBER (EXCEPTION_USER | (FACILITY_USER_DEMOSVC << 16))
HANDLE eventloghandle;
/*******************************************************************/
wchar_t subprocess_username[] = L"harry-test1";
wchar_t subprocess_domain[] = L"scms";
wchar_t subprocess_password[] = L"xyzzy916";
wchar_t subprocess_command[] = L"cmd.exe /c dir";
void demo(void)
{
if (!StartInteractiveClientProcess(subprocess_username, subprocess_domain, subprocess_password, subprocess_command))
{
const wchar_t * strings[] = {L"Creating subprocess failed."};
DWORD err = GetLastError();
ReportEventW(eventloghandle,
EVENTLOG_ERROR_TYPE,
0,
2,
NULL,
_countof(strings),
sizeof(err),
strings,
&err);
return;
}
{
const wchar_t * strings[] = {L"Creating subprocess succeeded!"};
ReportEventW(eventloghandle,
EVENTLOG_INFORMATION_TYPE,
0,
1,
NULL,
_countof(strings),
0,
strings,
NULL);
}
return;
}
/*******************************************************************/
CRITICAL_SECTION service_section;
SERVICE_STATUS service_status; // Protected by service_section
SERVICE_STATUS_HANDLE service_handle = 0; // Constant once set, so can be used from any thread
static DWORD WINAPI ServiceHandlerEx(DWORD control, DWORD eventtype, LPVOID lpEventData, LPVOID lpContext)
{
if (control == SERVICE_CONTROL_INTERROGATE)
{
EnterCriticalSection(&service_section);
if (service_status.dwCurrentState != SERVICE_STOPPED)
{
SetServiceStatus(service_handle, &service_status);
}
LeaveCriticalSection(&service_section);
return NO_ERROR;
}
return ERROR_CALL_NOT_IMPLEMENTED;
}
static VOID WINAPI ServiceMain(DWORD argc, LPTSTR * argv)
{
SERVICE_STATUS status;
EnterCriticalSection(&service_section);
service_handle = RegisterServiceCtrlHandlerEx(argv[0], ServiceHandlerEx, NULL);
if (!service_handle) RaiseException(EXCEPTION_USER_LINENUMBER | __LINE__, EXCEPTION_NONCONTINUABLE, 0, NULL);
service_status.dwServiceType = SERVICE_WIN32_OWN_PROCESS;
service_status.dwCurrentState = SERVICE_RUNNING;
service_status.dwControlsAccepted = 0;
service_status.dwWin32ExitCode = STILL_ACTIVE;
service_status.dwServiceSpecificExitCode = 0;
service_status.dwCheckPoint = 0;
service_status.dwWaitHint = 500;
SetServiceStatus(service_handle, &service_status);
LeaveCriticalSection(&service_section);
/************** service main function **************/
{
const wchar_t * strings[] = {L"Service started!"};
ReportEventW(eventloghandle,
EVENTLOG_INFORMATION_TYPE,
0,
2,
NULL,
_countof(strings),
0,
strings,
NULL);
}
demo();
/************** service shutdown **************/
EnterCriticalSection(&service_section);
status.dwServiceType = SERVICE_WIN32_OWN_PROCESS;
status.dwCurrentState = service_status.dwCurrentState = SERVICE_STOPPED;
status.dwControlsAccepted = 0;
status.dwCheckPoint = 0;
status.dwWaitHint = 500;
status.dwWin32ExitCode = dwWin32ExitCode;
status.dwServiceSpecificExitCode = dwServiceSpecificExitCode;
LeaveCriticalSection(&service_section);
SetServiceStatus(service_handle, &status); /* NB: SetServiceStatus does not return here if successful,
so any code after this point will not normally run. */
return;
}
int wmain(int argc, wchar_t * argv[])
{
const static SERVICE_TABLE_ENTRY servicetable[2] = {
{(wchar_t *)servicename, ServiceMain},
{NULL, NULL}
};
InitializeCriticalSection(&service_section);
eventloghandle = RegisterEventSource(NULL, displayname);
if (!eventloghandle) return GetLastError();
{
const wchar_t * strings[] = {L"Executable started!"};
ReportEventW(eventloghandle,
EVENTLOG_INFORMATION_TYPE,
0,
2,
NULL,
_countof(strings),
0,
strings,
NULL);
}
if (StartServiceCtrlDispatcher(servicetable)) return 0;
return GetLastError();
}
This must be linked with Microsoft's sample code. You can then install the service using the sc command:
sc create demosvc-createprocesswithlogonw binPath= c:\path\demosvc.exe DisplayName= "Demo service for CreateProcessWithLogonW"
I ended up with the following workaround. I configured a different service running as _limited_user and started on demand. Then the test framework can start and stop the limited user service. And the limited user service can run the processes required for my tests.
The workaround works. Hence my processes do not require interactive desktops (even though they load user32.dll). Apparently user32.dll can be loaded in non-interactive context. But there is some unknown subtlety that does not allow the process to run when started directly from test framework service using CreateProcessWithLogonW.

How to detect if a Microphone is present

I just had a very long tech support call because a customer didn't have a Mic on their laptop. (Stupid me: they said they'd used the mic earlier and I have never heard of a laptop not having a Mic).
I'm wondering if there is a way to detect whether there is a Microphone (recording capability) on Windows XP, Vista, 7.
(I've got error handling enabled and it logs the error and then exits the Function but the app just crashes on Windows 7 if there's no Microphone. )
I'd use IMMDeviceEnumerator::GetDefaultAudioEndpoint - this returns the default audio device for the specified role and data flow.
In particular, you would use:
CComPtr<IMMDeviceEnumerator> pEnumerator;
CComPtr<IMMDevice> pDevice;
hr = CoCreateInstance(__uuidof(MMDeviceEnumerator), NULL,
CLSCTX_ALL, IID_PPV_ARGS(&pEnumerator));
if (SUCCEEDED(hr))
{
hr = pEnumerator->GetDefaultAudioEndpoint(eCapture, eConsole, &pDevice);
}
if (!pDevice || hr == ERROR_NOT_FOUND)
{
// no microphone
}
Check out System Tray Audio Device Switcher
In this VB source code you will an example on how to enumerate audio I/O devices.
in C++
#include "stdafx.h"
#include "Mmdeviceapi.h"
#include <atlbase.h>
int _tmain(int argc, _TCHAR* argv[])
{
CoInitializeEx(NULL, COINIT_MULTITHREADED);
CComPtr<IMMDeviceEnumerator> pEnumerator = NULL;
CComPtr<IMMDevice> pDevice;
const CLSID CLSID_MMDeviceEnumerator = __uuidof(MMDeviceEnumerator);
const IID IID_IMMDeviceEnumerator = __uuidof(IMMDeviceEnumerator);
HRESULT hr = CoCreateInstance(
CLSID_MMDeviceEnumerator, NULL,
CLSCTX_ALL, IID_IMMDeviceEnumerator,
(void**)&pEnumerator);
if (FAILED(hr))
{
printf("failed");
}
else
{
hr = pEnumerator->GetDefaultAudioEndpoint(eCapture, eConsole, &pDevice);
if (!pDevice || hr == ERROR_NOT_FOUND)
{
printf("no microphone");
}
else
{
printf("microphone present");
}
}
return 0;
}
I think the only way you will be able to do this in VB 6 is through Direct X:
http://msdn.microsoft.com/en-us/library/bb318770(VS.85).aspx
You can check this out:
http://msdn.microsoft.com/en-us/library/bb280815(VS.85).aspx
CaptureDevices Collection Class (Microsoft.DirectX.DirectSound)
http://msdn.microsoft.com/en-us/library/ms810619.aspx
you can also call dxdiag..

Resources