-
Notifications
You must be signed in to change notification settings - Fork 1.7k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Added changing optional config of W32 services #489
Conversation
These most definitely need working tests. Note #258 too. |
* If the function fails, the return value is zero. To get extended | ||
* error information, call GetLastError. | ||
*/ | ||
public boolean ChangeServiceConfig2W(SC_HANDLE hService, int dwInfoLevel, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is the -W suffix necessary? Why?
The tests work, but I commented them out analogous to most other tests in W32ServiceTest.java because they edit real services. How do I proceed concerning #258? Should I merge my changes into #258 and work from there? The W-suffix seems necessary to me because the ChangeServiceConfig2 uses TCHAR strings (LPTSTR) for at least some configuration options (namely in the SERVICE_FAILURE_ACTIONS struct). |
JNA will automatically deal with the Ws, remove that. UNICODE is default. Lets begin with the smallest PR possible, this is good to start, but you do need tests for these bindings. Windows has some default services that are always there, and some functions can be called all the time, and you might find some inspiration in service tests of https://github.com/dblock/msiext. All we want in JNA tests is that each Win32 API added has a test that invokes it. Often invoking it and getting back an expected error is good enough. To be clear, first add a test for |
At akandi@53f0f6c I started writing a better test. Is this along the lines of what you imagined? However I seem to be missing something, maybe the alignment upon conversion back to Structure is off, and the test fails. I have placed a dump of the buffer after a call to QueryServiceConfig2 in contrib/platform/dump.txt and it looks quite right. |
The issue is resolved. I didn't realize I had to call read on the freshly created structure from pointer. Both tests run fine now (provided that the user has sufficient permissions to edit services). |
public SERVICE_FAILURE_ACTIONS getFailureActions() { | ||
Pointer buffer = queryServiceConfig2(Winsvc.SERVICE_CONFIG_FAILURE_ACTIONS); | ||
SERVICE_FAILURE_ACTIONS result = new SERVICE_FAILURE_ACTIONS(buffer); | ||
result.read(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You should make the read()
call part of the SERVICE_FAILURE_ACTIONS(Pointer)
constructor.
I'm not sure if this last commit is necessary. If I comment out the part of the test that reverts the service changes then if I simply use Strings in the struct I end up with Chinese characters in my service configuration. If I use WString all is fine, obviously, but I don't know if that version is compatible with non-Unicode systems. |
* error information, call GetLastError. | ||
*/ | ||
public boolean ChangeServiceConfig2(SC_HANDLE hService, int dwInfoLevel, | ||
Structure lpInfo); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Using the generic Structure is a bit too general - whenever the API expects several variants we "overload" the method - see GetVersionEx as an example. In this case there should be a match between the dwInfoLevel and the actual structure type, but it is up to the user to make sure that the level matches the type. Another alternative would be to define an abstract ServiceConfigStructure that extends Structure and have all the possible variants extend the ServiceConfigStructure - this would allow some degree of type safety...
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, please make one variant per supported structure type. Since I don't believe there is any shared structure between the service config structs, using a ServiceConfigStructure would probably be not much better than Structure.
@twall like so? @lgoldstein @twall Makes sense to me. But I see that in #258 they have gone the route of having an abstract superclass (namely ChangeServiceConfig2Info). |
@akandi yes, that's what I mean. The Is there a test I can run to expose the Chinese character problem? As for the structure base class, it can be useful to communicate that the structure variants are used in the same place, and if there are a lot of them it's easier to define a single method mapping. I don't have a strong opinion there, so I'd say pick what seems to make things clearest to the end user. |
So the SERVICE_FAILURE_ACTIONS Structure - as well as others that depend on the TypeMapper - would have to be moved from the Winsvc to the Advapi32 interface? The Chinese characters appear in my Service configuration (services.msc, right click on Windows Time, Properties, Recoveries tab, in the Run program section the value of the Program text box is Chinese characters) after having the config set through JNA . |
Only the The method for finding a structure's type mapper (if one is not explicitly set) is to check the enclosing class for an instance of I notice there are a few |
This behavior should be more consistent now with recent changes to both the w32 API library hierarchy and fixes to tracking appropriate library options. |
Conflicts resolved and pushed to branch |
@akandi Please re-test any local setup you might have based on the new master. |
Motivation: Let's use the latest maven version to build Modifications: Upgrade to 3.9.0 Result: Use latest maven release
This allows to add failure configurations to Windows services.