CreateProcessAsUser icon indicating copy to clipboard operation
CreateProcessAsUser copied to clipboard

Why not handle when WTSQueryUserToken fails?

Open imtrobin opened this issue 5 years ago • 5 comments

Hi, I got the sample working fine under a service, but not when running as a normal app. So I understand there's a few more tokens as described here

https://devblogs.microsoft.com/oldnewthing/20190531-00/?p=102532 https://stackoverflow.com/questions/42717691/wtsqueryusertoken-returning-false

I'm just curious, why not set put this code in when WTSQueryUserToken fails?

imtrobin avatar Apr 30 '20 07:04 imtrobin

Old, I know, but since this looks abandoned yet is still useful, I found it much easier to do this:

if (OperatingSystem.IsWindows())
{
    if (Environment.UserInteractive)
    {
        success = Process.Start(exeFilename) is not null;
    }
    else
    {
        var pathname = 
        Path.Combine(AppDomain.CurrentDomain.BaseDirectory, exeFilename);

        success = ProcessExtensions
        .StartProcessAsCurrentUser(pathname, workDir: AppDomain.CurrentDomain.BaseDirectory);
    }
}

MV10 avatar Dec 05 '23 17:12 MV10

It's not abandoned it's perfect ;)

AndrewSav avatar Dec 13 '23 03:12 AndrewSav

lol... Frank's trivial-to-fix #5 may have something to say about that!

MV10 avatar Dec 13 '23 09:12 MV10

@MV10 I have not developed software for Windows in over 8 years now, so this has been far from my priority. I'd be happy to accept any trivial-to-fix pull requests.

murrayju avatar Dec 13 '23 15:12 murrayju

@murrayju Understandable and my comments weren't meant as a criticism.

I'm happy to PR that fix, and what I'd like to do is convert this to am SDK-style .NET Standard library. I don't personally use .NET Framework any more, but there are unfortunate folks who are stuck with it and I'm sure they'd appreciate not being left behind. I don't see anything here which would prevent that while still supporting modern .NET.

I'd also be willing to add a modern hosting-style demo service project. Services aren't written the way the existing demo shows any more (much easier now -- if you're curious, have a look at this).

Might as well modernize everything right? If you're good with all that, I'll put it on my to-do list and get you a modernization PR followed by a #5 fix PR soon.

Edit: Unexpectedly had a bunch of free time today so I went ahead and PR'd both the modernization changes and the memory leak fix.

MV10 avatar Dec 14 '23 09:12 MV10