Do HMONITORs need to be released? If so, how? - winapi

Several multiple-monitor API functions (eg, MonitorFromRect) return a HMONITOR handle to the monitor in question. I have been unable to find the function to free this handle - does it need to be freed, and if so, how should I free it?

The SDK docs always explicitly mention how a handle needs to be released. Nothing for HMONITOR, you are just getting a handle to an internal object that's around anyway.

Microsoft's Example program doesn't try to free the handle. While their examples might not be perfect, they are a good indication of the way a function is meant to be used.

Related

Way to get IShellExtInit to work out-of-process

I'd like to pass an IShellItemArray to an out-of-process COM object residing within an exe. I'd prefer to use existing interfaces before creating my own, so I thought I'd try having the out-of-process object implement IShellExtInit. Seemed like a good fit.
Anyway, it appears that attempting to create/query interface for IShellExtInit fails for out-of-process COM servers. I found some additional evidence indicating that it's not possible because that particular interface lacks marshaling support.
Is there anything that can be done to fix that, short of defining my own similar interface (with the oleautomation attribute)? Alternatively, are there any other existing generic interfaces, that work out of process, for passing an IShellItemArray? Thanks for any info.
IExecuteCommand is supported out-of-proc where IObjectWithSelection is the way the array is passed. Might only work on Win7 and later.
IDropTarget is also supported out-of-proc but it is more work (fake dropping a data object). It works back to at least WinXP if you care about that.

Custom software driver communication with user client on Windows

EDIT: through another question on the forum, I learned that DeviceIoControl can be async, so question 4 is now just question 2
The extensive Windows driver documentation says little, that I've found, about how a client user-mode app can communicate directly to a specific device. I understand that normally such operations are managed by the Win32 api, but in the case of specific devices or (what I'm interested in) software drivers, I don't know many ways in which it can be done
The docs say that one can use CreateFile, ReadFile, WriteFile etc. to "open" the driver as a "file" and then r/w from/to it, maybe asynchronously if you want. That sounds good but it feels like that can't be the best option for everything, nor is it the only option. DeviceIoControl can have specific control codes and you can command a driver like that, but I can't see any async capabilities in the docs there.
In the driver docs, it's clear a driver must write its callbacks routines for dispatch calls which are sent to it, but I don't understand where these dispatch calls come from, or how a user-mode client might interact with that directly.
Using Valorant's Vanguard as an example software driver, I highly doubt it just r/w'd from a "file" in operation - it seems too abstract to be fast, or not specific enough for a complex system, as all you can do in fileapi.h is read, and write, without any real parametrisation - right?
My questions are:
Must a software driver write routines for all dispatch calls that the docs recommend even though they have nothing to do with hardware?
Are there other techniques than the R/W file api and the DeviceIoControl function to communicate with a specific (software) driver?
Are there efficient, "lean and mean" solutions, when our software driver is entirely custom to the targeted user app, as Vanguard was?
(ignore) Are the async R/W file operations the only way to get this done in a multi-threaded async manner, where the client submits many possibly overlapping calls, or can DeviceIoControl leverage threading and asynchronicity?
To answer your questions
No. Not all dispatch calls needs to be implemented for a software driver. I think only CREATE/CLOSE/DEVICE CONTROL needs to be implemented. You dont even need to implement unload but then you will not be able to unload the driver, which is required for testing. If there are any other required dispatch methods, you can simply return not implemented from those implementation.
Yes. You can use named pipe between driver and application as another way to communicate.
Not sure how much more lean can you get than just implementing the minimum dispatch methods.
You can use multiple threads and synchronous operations OR you can use single thread and asynchronous operation. Depends on what model is best for you.

On windows, how to prevent registry entries from being modified?

I saw some anti-virus software notice me that some other process was trying to modify my registry entry and ask me whether to allow it. How can this anti-virus software hang that process up and stop if from modifying my registry until I make a decision? I fail to find any API that could do such things, which is crucial for my project.
Thanks,
Feng
Consider properly securing your applications registry keys using RegSetKeySecurity (MSDN).
Alternately, you could also construct a registry filter kernel mode driver. Such a driver can utilize the CmRegisterCallback/Ex() service to filter registry events. This is a big job (tm), but is the only documented/supported method to accomplish this that I am aware of.
This is done with games too. They hijack function calls which render objects in a 3D scene, they then tell the game engine to render all characters on top of every other object, which results in a wallhack.
An API you could use for this (not anymore since anti-cheat software blocks it now) is the detours API. You can still use it for your own purposes ofcourse.
There's alot of information about this API on the web.
http://research.microsoft.com/en-us/projects/detours/
The antivirus is most likely hooking the registry functions so it's own code gets called first before passing the requests to the real functions.
Note that doing this in anything but a few very specific circumstances can be suspicious behaviour.
I guess you could have a look at RegNotifyChangeKeyValue, but I think a antivirus won't use this approach. This does not block changes made to the registry, but could be used to get notified when a key is modified.

How to tell, using Perl, if a windows service is stalled, but still running

I have a problem that probably is not unique, but certainly does not seem to be widely reported.
I need a method to ping a windows service that will tell me if it has stalled out, even if windows is reporting that it is running.
I would prefer to use have a Perl solution but would accept any solution that someone can come up with.
Thanks,
Jeremy
What service are you dealing with? I don't think there is any generic solution to your question if the Services control panel doesn't indicate a problem. In order to detect a running-but-no-longer-functioning-properly process, you have to know what "functioning properly" looks like.
You could use the Win32::Service module
use Win32::Service;
GetStatus(hostName, serviceName, status);
From http://metacpan.org/pod/Win32::Service:
Get the status of a service. The third argument must be a hash reference that will be populated with entries corresponding to the SERVICE_STATUS structure of the Win32 API. See the Win32 Platform SDK documentation for details of this structure. You can even grab the service with GetServices(hostName, hashref).

Accessing a webserver from a cocoa application

I am writing a cocoa application in which I want to download a file from a webserver. What will be the most convenient method to go about doing this? Should I go in for NSSockets or a NSUrlRequest? Or is there any other easier way to achieve this?
If you want to load the contents of the file into memory, many of the Cocoa data classes such as NSString, NSData and even NSDictionary have initWithURL: methods, which initialize directly with the contents of a web request. They're very easy to use, but they're not very flexible or provide for good error handling. NSURLConnection provides a more flexible way to load data if you need it.
If you want to download the file directly to disk, then NSURLDownload would be the best bet.
A word of warning: The initWithURL: methods are blocking, which is a big problem if the file is large, the server is slow, the user's internet connection is slow, etc. Don't call them from the main thread.
You also don't get any progress reporting, so when the download is slow, you have no way to tell the user how far along it is or how much longer it will take.
In almost all cases, you should use NSURLDownload or NSURLConnection instead.
The simplest thing to do is probably use NSURLDownload with NSURLRequest.
NSURLConnection is good if you want to get data from the web service into an NSString or NSData. Make sure you make asynchronous calls and handle errors and data in the NSURLConnection methods
Here's a good example for REST-style calls
http://kosmaczewski.net/2008/03/26/playing-with-http-libraries/
NSURLConnection does give you the most granularity, but be careful with NSURLConnection's sendSynchronousRequest() method. It leaks memory each time (have attached the XCode Leak Instrumentation tool and run it to prove it to myself) and gives weird HTTP 204 responses for no reason at all on occasion. I've blogged about this here
And another way is using libcurl, which comes preinstalled on any OS X system. You'd better make sure System Settings like proxies etc. are respected though if you use this approach.

Resources