alternatives to polling for changes to a ManagementObjectSearcher search result - wmi

I am using ManagementObjectSearcher to detect the availability of a bluetooth device:
ManagementObjectSearcher("select Name from Win32_PnPEntity WHERE Name = 'Microsoft Bluetooth LE Enumerator'")
I'd like to know as the application runs if such device is added or removed. Is there a way to do that without polling the query?

You can create a ManagementEventWatcher and set up a method to handle the events when it occurs. Its described in this stackoverflow post
Using WMI to identify which device caused a Win32_DeviceChangeEvent

Related

Disable prevent windows log event

I'm using WMI (Windows Management Instrumentation) to try to collect some information from a allot of remote computers.
The issue is that every time I try to initiate a connection to a remote computer/resource using:
//IWbemLocator::ConnectServer method (wbemcli.h)
m_pLoc->ConnectServer ....
where
IWbemLocator *m_pLoc;
(You can assume m_pLoc is correctly initialized)
, if the remote resource is unavailable, Windows generates a log event in the Windows Event Viewer:
DCOM was unable to communicate with the computer ....using any of the
configured protocols; requested by PID .....
The problem is that given a huge number of remotes that at some point are not accessible the logs get flooded.
Is there any way to control or to prevent Windows from pushing a event in the Event Viewer every time I try to initiate a connection?
Seems that arguments for :
IWbemLocator::ConnectServer method (wbemcli.h)
or
CoCreateInstance used to intialize an IWbemLocator do not permit this sort of very custom configuration I'm looking for.
Any suggestion or alternatives?
Thank you!
Looking at the message logged in EventViewer more closely, I can see that this is a DCOM thing, and it looks like you can turn DCOM error logging off by (as usual) tweaking the registry.
The key you want is:
HKEY_LOCAL_MACHINE
SOFTWARE
Microsoft
Ole
And then create a DWORD value in there called ActivationFailureLoggingLevel and set it to 2.
Info gleaned from here. I haven't tested this myself but it looks like it should work.

Listing all Network Connections, then managing one

I'm developing an application in C++ for Windows 7, that sets up a TFTP server to communicate to an embedded device. The device is supposed to be connected directly to the Ethernet port, and it expects a TFTP server on 192.168.1.19 static address.
To simplify user experience, I want the following scenario:
present a list of all network connections registered in the system to the user (same list as in "Control Panel\Network and Internet\Network Connections")
allow to choose one connection
memorize its IPv4 configuration
set IPv4 configuration to 192.168.1.19 and 255.255.255.0
after TFTP job done, restore original IPv4 configuration
The problem I encountered is a mix of the following:
when no Ethernet cable plugged, neither of GetAdaptersInfo, GetInterfaceInfo, GetAdaptersAddresses list the connection I'm interested in;
even if I get that connection, I'm not sure which API to use to set DHCP on/off, static IP, network mask.
I tried using netsh, and it works fine, but I don't want to run it from my app, because I don't want to deal with non-English characters in the connection names.
Suggestions welcome,
Thanks!
===== edit #1
Tried WMI query "Select * From Win32_NetworkAdapter Where NetConnectionID = 'Local Area Connection'" -- no IP address in the feedback, although I can see it in the Control Panel connection properties.
Queries involving Win32_NetworkConnection don't return anything at all.
I ended up using:
"Select * From Win32_NetworkAdapter Where PhysicalAdapter='True' AND AdapterTypeID=0" query to list adapters/connection names/guid - works pretty good
registry to read configuration via guid
netsh to do configuration via connection name. Yes, I had much fun with locales.
I frankly didn't find a more reliable way to work with disconnected connections. Hope this helps. If anyone has better solutions - I'd be glad to see them!

Trigger an exe once My device is connected via USB

Once my embedded device is connected to USB port of my PC, it should trigger an exe as an event. How can I achieve this??
Should I create a service to keep monitoring the USB connector bus or is there any default API's available in Windows to achieve this??
thanks.
A simple exe which is started on connect is not possible. But you can write a service or user mode application which listens for device arrival events. WM_DEVICECHANGE is sent to all (registered) applications with a device interface guid which represents which device is plugged in. You can then use this id with the setupapi to see if its your device.
On receiving that event, you can then start your executable.
Depending on your version of Windows it might be possible with a workaround using a AutoRun.inf file in the root folder of a USB drive. For security reasons this is by default turned off, and in Windows 7 not allowed at all.
To achieve the same effect in a more robust way, you need to create a service that monitors whether your device is connected or not (e.g. iTunesHelper that monitors for connected Apple devices).
The easiest solution is probably a trivial UMDF driver. That's basically a small COM component called when your device is connected.

Getting "Capability matching failed at NIC." error while trying to set a custom profile - NativeWiFi + WinXP SP3

I am trying to create an ZeroConfig client to connect my Atheros WiFi device to an AP (for learning purpose). I am using NativeWiFi (WinXP SP3) to query my device & set a profile against the device so that it can connect to an AP. The steps I have taken follows like this -
get NativeWiFi handle by calling openHandle.
Enumerate the device list & Get a GUID of the device.
Scan & Retrieve a list of available network.
Trying to set a autoconnect profile with WlanSetProfile()
I am getting "Capability matching failed at NIC." while trying to set a custom profile using WlanSetProfile API.
I can connect to my AP using the Client Utility Application provided by the Atheros. I cannot figure out what might be the cause of this message. Can any one suggest?
Thank you in advance & sorry for my English.

Can you use WMI to create an MSMQ message queue (PRIVATE queue)?

I need to create a PRIVATE message queue on a remote machine and I have resolved to fact that I can't do this with the .NET Framework in a straight forward manner. I can create a public message queue on a remote machine, but not a PRIVATE one. I can create a message queue (public or private) locally.
I am wondering if anyone knows how to access MSMQ through WMI.
Edit: I don't see anything to do it with using the MSMQ Provider. May have to get tricky and use PSExec to log onto a remote server and execute some code.
Yes, queue creation is simple in .NET, however you cannot create a private queue on a remote machine this way.
I have been thinking about adding queue creation to the MSMQ WMI provider for some time... If you need it for a real product / customer, you can contact me and I will consider giving this feature a priority.
All the best,
Yoel Arnon
A blog post about MSMQ and WMI is here: http://msmq.spaces.live.com/blog/cns!393534E869CE55B7!210.entry
It says there is a provider here: http://www.msmq.biz/Blog/MSMQWmiSetup.msi
It also says there is a reference here: http://www.msmq.biz/Blog/MSMQ%20WMI%20Provider%20Objects.doc
Hope this helps.
WMI can't do this out-of-box. The previous answer has some obsucre WMI provider, but it doesn't even seem to support Queue creation.
This is very simple in .NET however! I wouldn't go so far as PSExec.
MessageQueue.Create
I was wanting to create remote private queues also, but since .NET doesn't support it, we decided we will just use remote public queues instead. If we set Send and Receive permissions on the queues as desired, this should be fine.
One idea for a work around would be to write your own Windows service or web service that runs on the same machine where the queue needs to reside. You could call this service remotely through a socket or over http, and your locally-running code could create the local private queue.
If you use the direct name format to reference the queue, you can Send and Receive from a remote private queue.
set qinfo = CreateObject("MSMQ.MSMQQueueInfo")
qinfo.PathName = ".\Private$\TestQueue"
qinfo.Label = ".\Private$\TestQueue"
qinfo.Journal = "1"
qinfo.Create
Copy the code in a text editor, save the file as .vbs and execute.