Windows test if program is running

How to check if program is running in local console?

In Windows Server 2003, how I can check if my program is running in local console («on the screen of the server machine») instead of remote session?

I’m using Delphi Win32, so any Windows API based method should work..

4 Answers 4

Wouldn’t the session number tell you this ?

You’d have to check the OS version as well, using GetVersionEx: for everything up to XP/Server 2003 session 0 is local (service or interactive console), anything higher is virtual. For Vista/2008 session 0 and 1 are both local (0 is service, 1 is console), everything else is virtual.

I’m guessing your Delphi units would declare the session number as var, so you wouldn’t need the ampersand.

WTSGetActiveConsoleSessionId() should return the ID of the session attached to the console. You can then compare that session id with your application’s current session ID to determine whether you are running on the console or not. Vista (not sure about Windows Server 2008) does not necessarily give the console session the ID of 1 (Fast User Switching, anyone?).

For me, ProcessIdToSessionId returned 0 both when run directly at the physical console and when logged in to the administrative session (mstsc /admin).

However, when you login via RDP, Windows (XP Pro in this case) creates a new session which it shows on the physical console which just has the «this computer is locked» display. WTSGetActiveConsoleSessionId returns the session id for that second session which in my case was 2.

So even though your app is running on the console, there are now two console sessions and your app is not running on the active one. In my code I compare session id against 0 instead.

How can I detect if my app is running on Windows 10

I’m looking for a means to detect if my C# app is running on Windows 10.

I had hoped that Environment.OSVersion would do the trick, but this seems to return a Version of 6.3.9600.0 on Windows 8.1 and Windows 10.

Other solutions such as this don’t seem to distinguish between Windows 8 and Windows 10 either.

Why do I need to do this?

Because I’m using a WinForms WebBrowser control to host an OAuth page that crashes and burns in older IE versions (my app connects to a user’s Nest account. ).

By default, the WebBrowser control emulates IE7. Using a Registry key, you can tell it to emulate the latest version of IE that is installed on the host PC. However, the value that worked up to Windows 8.1 (and pre-releases of Windows 10) does not work in the final version of Windows 10.

7 Answers 7

If you look at registry you will found environment name:

Читайте также:  Аналог puppy для linux

For example my product name is Windows 10 Home :

With this code you get if it Windows 10:

Note: Add using Microsoft.Win32; to your usings.

Answer

Use Environment.OSVersion and add an application manifest file with relevant supportedOS elements uncommented.

e.g. add this under

Reason

I don’t like the answer from @Mitat Koyuncu because is uses the registry unnecessarily and as mentioned in the comments uses unreliable string parsing.

I also don’t like the answer from @sstan because it uses third party code and it needs the application manifest anyway.

Windows 10: VerifyVersionInfo returns false when called by applications that do not have a compatibility manifest for Windows 8.1 or Windows 10 if the lpVersionInfo parameter is set so that it specifies Windows 8.1 or Windows 10, even when the current operating system version is Windows 8.1 or Windows 10. Specifically, VerifyVersionInfo has the following behavior:

• If the application has no manifest, VerifyVersionInfo behaves as if the operation system version is Windows 8 (6.2).

• If the application has a manifest that contains the GUID that corresponds to Windows 8.1, VerifyVersionInfo behaves as if the operation system version is Windows 8.1 (6.3).

• If the application has a manifest that contains the GUID that corresponds to Windows 10, VerifyVersionInfo behaves as if the operation system version is Windows 10 (10.0).

The reason is because VerifyVersionInfo is deprecated in Windows 10.

Checking if my Windows application is running

How do I check if my C# Windows application is running ?

I know that I can check the process name but the name can be changed if the exe changes.

Is there any way to have a hash key or something to make my application unique?

9 Answers 9

The recommended way is to use a Mutex. You can check out a sample here : http://www.codeproject.com/KB/cs/singleinstance.aspx

In specific the code:

For my WPF application i’ve defined global app id and use semaphore to handle it.

you need a way to say that «i am running» from the app,

1) open a WCF ping service 2) write to registry/file on startup and delete on shutdown 3) create a Mutex

. i prefer the WCF part because you may not clean up file/registry correctly and Mutex seems to have its own issues

Mutex and Semaphore didn’t work in my case (I tried them as suggested, but it didn’t do the trick in the application I developed). The answer abramlimpin provided worked for me, after I made a slight modification.

This is how I got it working finally. First, I created some helper functions:

Then, I added the following to the main method:

If you invoke the application a 3rd, 4th . time, it does not show the warning any more and just exits immediately.

How to check if a process is running via a batch script

How can I check if an application is running from a batch (well cmd) file?

I need to not launch another instance if a program is already running. (I can’t change the app to make it single instance only.)

Читайте также:  Android and windows media player

Also the application could be running as any user.

18 Answers 18

Another possibility I came up with, inspired by using grep, is:

It doesn’t need to save an extra file, so I prefer this method.

Here’s how I’ve worked it out:

The above will open Notepad if it is not already running.

Edit: Note that this won’t find applications hidden from the tasklist. This will include any scheduled tasks running as a different user, as these are automatically hidden.

I like Chaosmaster’s solution! But I looked for a solution which does not start another external program (like find.exe or findstr.exe). So I added the idea from Matt Lacey’s solution, which creates an also avoidable temp file. At the end I could find a fairly simple solution, so I share it.

This is working for me nicely.

The suggestion of npocmaka to use QPROCESS instead of TASKLIST is great but, its answer is so big and complex that I feel obligated to post a quite simplified version of it which, I guess, will solve the problem of most non-advanced users:

The code above was tested in Windows 7, with a user with administrator rigths.

Under Windows you can use Windows Management Instrumentation (WMI) to ensure that no apps with the specified command line is launched, for example:

wmic process where (name=»nmake.exe») get commandline | findstr /i /c:»/f load.mak» /c:»/f build.mak» > NUL && (echo THE BUILD HAS BEEN STARTED ALREADY! > %ALREADY_STARTED% & exit /b 1)

I use PV.exe from http://www.teamcti.com/pview/prcview.htm installed in Program Files\PV with a batch file like this:

TrueY’s answer seemed the most elegant solution, however, I had to do some messing around because I didn’t understand what exactly was going on. Let me clear things up to hopefully save some time for the next person.

TrueY’s modified Answer:

Anyway, I hope that helps. I know sometimes reading batch/command-line can be kind of confusing sometimes if you’re kind of a newbie, like me.

The answer provided by Matt Lacey works for Windows XP. However, in Windows Server 2003 the line

INFO: No tasks are running which match the specified criteria.

which is then read as the process is running.

I don’t have a heap of batch scripting experience, so my soulution is to then search for the process name in the search.log file and pump the results into another file and search that for any output.

I hope this helps someone else.

I like the WMIC and TASKLIST tools but they are not available in home/basic editions of windows.Another way is to use QPROCESS command available on almost every windows machine (for the ones that have terminal services — I think only win XP without SP2 , so practialy every windows machine):

QPROCESS command is not so powerful as TASKLIST and is limited in showing only 12 symbols of process name but should be taken into consideration if TASKLIST is not available.

More simple usage where it uses the name if the process as an argument (the .exe suffix is mandatory in this case where you pass the executable name):

Читайте также:  Как изменить заставку загрузки для windows

The difference between two ways of QPROCESS usage is that the QPROCESS * will list all processes while QPROCESS some.exe will filter only the processes for the current user.

Using WMI objects through windows script host exe instead of WMIC is also an option.It should on run also on every windows machine (excluding the ones where the WSH is turned off but this is a rare case).Here bat file that lists all processes through WMI classes and can be used instead of QPROCESS in the script above (it is a jscript/bat hybrid and should be saved as .bat ):

And a modification that will check if a process is running:

The two options could be used on machines that have no TASKLIST .

The ultimate technique is using MSHTA . This will run on every windows machine from XP and above and does not depend on windows script host settings. the call of MSHTA could reduce a little bit the performance though (again should be saved as bat):

Testing if the program is running correctly

I’ve got a program that it basically saying if the users input are inside, outside or on the rectangle edge and now i have written a test for this program that is suppost to tell the user if the test/tests are successful or not and i want to do do a test for every dot in a x-y table and then in the end tell if all test where successful or not.

The problem I have is that i’m not sure how to put it inside the base code so the test will work. Is it suppost to be in the beginning or the end(since the program exits after it tells the user where the dot is located)? Should I do an array for the test or not? Thankful for any help how to write the code inside the base code:

1 Answer 1

First you should check that x1 and y1 and swap the coordinates around as necessary so that point (x1, y1) is bottom left and point (x2, y2) is top right.

Then check if the point is outside the box with

Then check if the point is inside the box with

This leaves the case where

Strangely the function overwrites any arguments it is passed by using them as local variables. So the coordinates are lost when the function returns. It would be better to enter the data before calling the function, or pass pointers to variables that will contain the data.

EDIT

This is one way, although there are many ways the code could be improved. One difficulty of using the double type is the inexactness of the coding of floating point numbers. Note that the code avoids the use of the == equality test. That may not be an issue in this example, but if the point has been computed and should theoretically lie exactly on the box boundary, the testing might fail to detect that.

Оцените статью