Gena01.com Forum

Software Development => Win32Pad => Topic started by: element on July 05, 2008, 04:39:50 am



Title: Single instance check
Post by: element on July 05, 2008, 04:39:50 am
I'm on the quest to find the ultimate tiny Notepad replacement (tiny = under 100kB exe). One thing I've noticed is that all tiny Notepad replacements (metapad, SkimEdit, etc) have the same flaw: they all allow multiple instances, so you may end up editing multiple copies of the same file. Win32Pad comes the closes to my ideal, because at least it detects file changes on disk. Still... is it impossible to implement a single-instance check in an "under 100kB" editor?


Title: Re: Single instance check
Post by: Roger on July 05, 2008, 12:14:35 pm
One way you can make sure that you are running only a single instance of Win32Pad.exe is to launch Win32Pad from a batch file, like below. Name the batch file something like «GoW32Pad.bat» or something even shorter like «gownpd.bat». Add the directory to the PATH or even easier locate it in the root of your drive, which means that you'll always run it like this from the command-line: \gownpd [ENT]

@echo off
tasklist|find /i /c "win32pad.exe" > nul
set INSTANCE=%errorlevel%
if "%INSTANCE%"=="0" goto already
start win32pad.exe %1
:: «%1» is the name and path of the file you want to run.
goto end
:already
echo. & echo  Win32Pad is already running somewhere.
echo. & echo  Tap spacebar to conclude this batch file.
pause>nul
:end
set INSTANCE=
exit

Sincerely,

Roger


Title: Re: Single instance check
Post by: Roger on July 15, 2008, 11:40:37 am
Dear fans of Win32Pad!

Here is a more nearly complete answer to the question how to make sure that only one instance of Win32Pad is running. The following batch file supplies the details. Beginners need not do anything of course except copy and paste the batch file text into Win32Pad, or even into NotePad if a person doesn't have Win32Pad up and running yet.

To bring up a copy of NotePad, hold down the WinKey and tap letter R. In the small window that pops up, press Delete, type in «notepad» (without quotes) and press ENTER (unless, of course the word NOTEPAD is already present for you in the run-window: in that case just tap ENTER.

After copying and pasting (and making sure that the copying did pick up the whole contents), save the file to the root directory of your drive (most conveniently a flash- or portable hard drive). Save the batch file (we suggest) under the name «GoW32Pad.bat».

Quit from Win32Pad or NotePad by holding down the ALT key and tapping F4.

Code:
@ echo off
if not exist \win32pad\win32pad.exe goto end
::
::  GoW32Pad.bat is designed to make sure that only one instance is run-
::  ning, anywhere on the operating system, of the program Win32Pad.exe .
::
::  GoW32Pad assumes you've installed Win32Pad.exe to N:\Win32Pad folder.
::  «\» means «root directory» and «N:» is the drive letter of the drive.
::  «Win32Pad» in «N:\Win32Pad» means the «Win32Pad» folder on the drive.
::  Modify if not line and start line to show correct path for YOUR case.
::  Whatever follows the initial «N:\» (if anything) is named the «path».
::  The goto end command will of course terminate this bat-file abruptly
::  if win32pad.exe isn't located in the N:\Win32Pad directory specified.
::
tasklist|find /i /c "win32pad.exe" > nul
::
:: «tasklist» is a function native to Windows (probably from Win2000 on).
::  It doesn't need a prefix for path to it. Type tasklist at any prompt
::  to see how it lists what is currently running on your Windows system.
::
::  Here, we pipe the list into find, where /i and /c mean «case insensi-
::  tive» and «count». If win32pad.exe is already running, find will say
::  so. The «> nul» masks what would otherwise be screen chatter that we
::  don't need. The set INSTANCE line captures the yes|no 0|1 errorlevel
::  («yes» is usually 1, but not in the case of %errorlevel% output with
::  MSoft's cmd.exe = formerly command.com). Goto already in case of «0».
::  %errorlevel% is a hidden feature that shows only if we make it do so
::  as we have here: by setting it = to a temporary environment variable.
::
set INSTANCE=%errorlevel%
if "%INSTANCE%"=="0" goto already
start \Win32Pad\win32pad.exe %1
::
::  «%1» is the name in batch-talk of the file you want to run or create.
::  GoW32Pad.bat WILL run Win32Pad.exe without a somename.ext, but we do
::  not recommend it because File, SaveAs won't be cued to the directory
::  from which you had run GoW32Pad.bat. If, however, you do supply file-
::  name: File, Save As WILL be cued up to exactly the current directory.
::  Current directory means the directory whose prompt you are typing at.
::  If creating a new file, W32Pd will ask if that's what you want to do.
::  The ask window is already cued up to Yes, so all you do is tap ENTER.
::  The typing area inside Win32Pad will of course be empty:  get typing!
::
::  Run this batch file from the directory prompt where an existing file
::  is found OR where you want to create a new file. The syntax reads --
::
::  N:\Some_Directory>\goW32Pad somename.ext [ENT] -- Where GoW32Pad.bat
::  is assumed to dwell in the root directory of your drive named by «N:»
::  Be sure to include the backslash \ in front of goW32Pad somename.ext.
::  Notice how, in the start command above, we likewise supply backslash.
::  We purposely don't supply drive-letter N: since it varies by host PC.
::  On any host where you take your flash-drive a backslash will suffice.
::
goto end
::
:already
echo. & echo  Win32Pad is already running somewhere.
echo. & echo  Tap [spacebar] to end this batch file.
echo.
pause>nul
:end
set INSTANCE=
exit

To summarize: by running Win32Pad by means of the batch file above, only a single instance is allowed of Win32Pad anywhere on the host PC or workstation. Instead of the Win32Pad developers taking the time to build in this single-instance feature, the developers are able to turn their attention to whatever next version they may have time to provide.

mit herzlichem Gruß,

Roger

«Es macht keinen Sinn, daß die Kühe Milch geben —
 Die Bauern nehmen sie ihnen einfach weg.»


Title: Re: Single instance check
Post by: Steve on August 23, 2008, 09:00:22 pm
tasklist.exe does not come on XP Home.  And the download I got from MS did not produce the output expected at the DOS prompt.  So there was nothing to "find".

Where to get the version of tasklist.exe you used to make this BAT file?


Title: Re: Single instance check
Post by: Roger on September 05, 2008, 04:46:02 pm
Hello to all fans of Win32Pad!

Steve reports here (23.August 2008) that «Tasklist.exe does not come on XP Home.»

I didn't know that! (Gott steh' uns bei.) -- So of course we look to simulate tasklist! And, naturally, from the command line, since tasklist is a command-line program. -- I'm happy to report I found a solution. (Had I known that there was a problem with tasklist.exe, I would have written the batch file along the lines I describe below, first testing for whether tasklist.exe exists. Thanks, Steve, for telling us about it.)

Here's how to simulate tasklist.

Go to the NirSoft.net web page for its «CurrProcess v1.13 - Freeware Process Viewer» at www<punkt>nirsoft<punkt>net<forward>utils<forward>cprocess<punkt>html

All of the following assumes we're working at a flash-drive or portable hard-drive whose assigned letter is «N». Substitute for N what your actual letter is.

Open a CMD window by WinKey+R[un] and type «cmd» [ENT]. Type in your equivalent to letter N and tack on a colon, like this -- «N:» and [ENT]. Alternately, you can open a CMD window inside FreeCommander (CTRL+D) or A43 (click icon). Do the same in many other file-managers also.

First create a NirSoft folder (unless you have one already). -- At the prompt which reads «N:\>» (where «>» means «start typing here»), type in the following:

md NirSoft&cd NirSoft [ENT]

Comment: NirSoft offers a lot of freeware. I do keep NirSoft's «Nircmd.exe» directly in the NirSoft folder; but for CProcess.exe (its actual name) I suggest making a subfolder (subdirectory) to house CProcess. Locating all of the various NirSoft programs in just the one single NirSoft folder can lead to confusion. Even if this is your first download from NirSoft, you may well want to add nircmd at some point. To make the subfolder, do this:

N:\NirSoft>md CurrProcess

and then type «cd CurrProcess». The prompt now reads N:\NirSoft\CurrProcess> .

Download CurrProcess into this subdirectory (pick SAVE). As part of downloading, navigate to your drive letter N, then to NirSoft, then to CurrProcess and (finally) OK. UnZip the download. (If working in FreeCommander file manager [or equivalent], scroll to the .zip and click on the UnPack icon.) After unZipping you may delete the .zip file.

For our purposes, we don't need to click on (or otherwise open) CProcess.exe. Like many other NirSoft programs, it will under circumstances run 100% as a command-line program and never show a GUI. In our particular case, a GUI would just get in the way.

We assume as before that win32pad.exe is located in the directory «\Win32Pad» -- which means (in effect) «N:\Win32Pad» -- on i.e. the same drive as the \NirSoft\CurrProcess directory.

«Simulating» tasklist in the batch file (Re: Single instance check, July 15, 2008) amounts to interpolating some lines into the existing batch file, beginning at:
-----------------------------------------
tasklist|find /i /c "win32pad.exe" > nul
::
:: [existing lines of comments]
::
set INSTANCE=%errorlevel%
if "%INSTANCE%"=="0" goto already
start \Win32Pad\win32pad.exe %1
::
:: [more existing comment lines]
::
goto end
::
:already

and so on to the end of the file.
-----------------------------------------

We interpolate an IF NOT line ahead of the existing «tasklist» line; and we interpolate a (new) «:Simulate» subroutine between the existing GOTO END and the existing :ALREADY. %windir% below is an environment variable native to Windows. It usually translates to «C:\WINDOWS».

-----------------------------------------
if not exist %windir%\system32\tasklist.exe goto Simulate
tasklist|find /i /c "win32pad.exe" > nul
::
:: [existing lines of comments]
::
set INSTANCE=%errorlevel%
if "%INSTANCE%"=="0" goto already
start \Win32Pad\win32pad.exe %1
::
:: [more existing comment lines]
::
goto end
::
:: This marks the beginning of interpolated text --
:: i.e. between the original GOTO END and :ALREADY.
::
:Simulate
\NirSoft\CurrProcess\CProcess.exe /stext RunningNow.txt
find /i /c ": win32pad.exe" RunningNow.txt > nul
set INSTANCE=%errorlevel%
del RunningNow.txt > nul
if "%INSTANCE%"=="0" goto already
start \Win32Pad\win32pad.exe %1
goto end
::
:: This marks the end of interpolated text. Note
:: that we have purposely duplicated some lines.
:: --------------------------------------
:already
:: and so on to the end of the file


The ideal solution would be to have a permanent drop-in replacement for tasklist.exe -- or for Redmond to supply it as a free download. I hesitate to write to NirSoft with the suggestion, but if others who like the NirSoft solution were to write and cite this forum, NirSoft just might do it. Or for all I know there already exists a drop-in replacement on the net that is freeware.

«Es macht keinen Sinn daß die Kühe Milch geben --
 Die Bauern nehmen sie ihnen einfach weg.»

Cheers,

Roger


Title: Re: Single instance check
Post by: Roger on September 06, 2008, 11:41:58 am
Hello friends of Win32Pad!

In my last post here (yesterday) I expressed hope that a «drop-in replacement» might be found for tasklist.exe, which as Steve had pointed out (23.August 2008) is not supplied with all versions of XP.

I supplied a somewhat makeshift solution (which works!) via a Nirsoft.net utility, after having attempted, too hastily, to find a copy of tasklist.exe on the 'net and not succeeding.

This morning it occurred to me to Google for «tasklist.exe» and the exact phrase, «command line» -- and to specify the German language. What I came up with is this site:
 www<punkt>hijackthis<hyphen>forum<punkt>de<forward>showthread<punkt>php?t<equals>31716&page<equals>2

About 2/3 of the way down the page, find a post from a «Petra» (Staff) from 03.07.2008,11:32, under the topic «AW: Hilfe VIRUS ALERT», which begins with «Hallo Schumacher76,»

I'll quote the German, then translate: [starting with == Punkt 4 ==, paragraph 2]

«Falls Du WindowsXP Home oder Windows 2000 hast, bitte zusätzlich TASKLIST.ZIP downloaden und nach C:\Windows\system32 respektive C:\WINNT\system32 entpacken [...]»

«In case you have XP Home or Win 2000, please download TASKLIST.ZIP also and [disregard this part for portable-flash or -HD operation] unpack to C:\Windows\system32 or [in some cases] C:\WINNT\system32.»

I've capitalized TASKLIST.ZIP in the quotation, because it is in fact a direct-download link. And so, of course, I downloaded it.

Before doing so, I created a subdirectory for it as follows: N:\NirSoft>md TaskList_EXE & cd TaskList_EXE [ENT] The prompt of course now reads «N:\NirSoft\TaskList_EXE> .

Download tasklist.zip and (as usual) pick SAVE and navigate in the download window to [1] N: drive [2]N:\NirSoft [«OPEN»] and TaskList_EXE [«OPEN»] and (finally) OK.

UnZip tasklist.zip to get a single file «tasklist.exe». -- Delete the .zip since we don't need it anymore.

Now since I already have my own tasklist.exe in C:\...system32, I decided on an experiment to make sure that THIS newly downloaded tasklist is the one that is actually running: so (wait for it), I renamed it to «task_list.exe».

I opened a CMD-window and typed in the following at the N:\NirSoft\TaskList_EXE> prompt:

«start \win32pad\win32pad.exe» [ENT] and in that way got an [untitled]-Win32Pad running. A blank Win32Pad opens, and the CMD-window stays open.

Next, again into the CMD-window, I typed this:

«task_list|find /i /c "win32pad.exe" > nul» -- and then

«set Instance=%errorlevel%»

«echo %Instance%» -- and the display reads «0» -- which, in FIND's manner, means «Yes, an instance of win32pad.exe is running».

Particularly fun (optional!) was typing this: «task_list /? [ENT] -- To find a HELP file displayed -- in German! [Not to worry: I did the same for tasklist, English version, and none of the options needs concern us.]

So there we have it: an drop-in replacement for tasklist.exe.

Now there is no need for YOU to rename it! It is, however, necessary when you do use it to specify its path along with the command. Namely, from any prompt on this same N: drive, type «\NirSoft\TaskList_EXE\tasklist.exe».

[My renaming it was overkill, since specifying the path-to-tasklist would be enough to establish that the newly downloaded tasklist IS the one that's running: the one that Uncle Bill might have given you, IF you'd eaten all your vegetables, has [of course] a different path-to-it (see above).]

Accordingly my original batch file does not need stuff interpolated into it unless you'd like to see how a NirSoft utility works. Instead, change my original batch file simply to include the path: where there is «tasklist», substitute \NirSoft\TaskList_EXE\tasklist.exe.

There is of course no reason to locate the downloaded tasklist to where I did. Decide for yourself where you'd like it to be. It could, for all that, be located right in the \Win32Pad folder.

English speakers can get an idea here of what the web site is about from which we are able to download the (German edition!) of tasklist. Anti-malware solutions are included, und vieles andere mehr [u.v.a.m].

www<punkt>hijackthis<hyphen>forum<punkt>de<forward>forumdisplay<punkt>php<QuesMark>f<equals>10

mit herzlichen Glückwünschen,

Roger

«Es macht keinen Sinn, daß die Kühe Milch geben --
 Die Bauern nehmen sie ihnen einfach weg.»


Title: Re: Single instance check
Post by: Roger on December 01, 2008, 02:59:38 pm
Hello, devoted fans of Win32Pad!

Here is another source from which to download tasklist.exe ---

http://www.computerhope.com/download/winxp.htm (http://www.computerhope.com/download/winxp.htm)

Quoting from that page ---

Description:           Windows XP Tasklist utility

Installation Notes: Execute the file from a MS-DOS prompt. Additional information about
                                the available commands can be seen by typing "tasklist /?"

File Size:               71 KB 

Technical Notes:    Enables Windows XP Home and Windows XP Professional users to view
                                complete information about the services running on their computer.

Cost:                     None.


--- It is, of course, not from an «MS-DOS prompt» that tasklist.exe is run. It is from the «CMD» prompt.
     From Win 2000 onwards, cmd.exe replaces the «command.com» which is at the heart of DOS.

     Get to a CMD-window by holding down the WinKey and tapping letter «r» (for «run»). Type cmd [ENTER]

     Exit from a CMD-window by typing «exit» (without quotes) and press [ENTER].

     In our case we are, naturally, running tasklist.exe from inside of a batch file.

     Please note that tasklist.exe is a stand-alone utility. It can go onto your flash or portable HD.
     At no point, including while it's doing its job, does it write to anywhere on the host OS. Which
     is to say that it simply runs. It may, if you wish, go onto your hard drive, but there is no reason
     to download it to any particular folder, such as where standard (native, supplied) utilities are
     found. If you download it to N:\YouNameIt folder (directory) and if you want to be able to use
     tasklist from any folder, add the N:\YouNameIt folder to your %PATH% --- this way:

     path=N:\YouNameIt;%PATH%

     Where N is the drive letter assigned to the flash, to the portable HD, and so on.



Best wishes,

Roger

«Es macht keinen Sinn, daß die Kühe Milch geben ---
  Die Bauern nehmen sie ihnen einfach weg.»