views:

737

answers:

3

I need to add the "Run when Windows starts" option to my program CintaNotes, but do not want to sacrifice the "cleanness" of it: it is a 100% portable freeware and should not leave traces in the system. I've come up with the idea to autodetect running from the Startup shortcut and automatically minimizing to the system tray. Is there a way to do it? I'm using C++ and raw Winapi.

So: - No writing to the registry - No command line parameters

Thanks!

UPD: The question is NOT how to minimize to the system tray! The question is how can a program differentiate between being run normally and being run from a startup-folder shortcut without using registry and command-line parameters.

A: 

Why don't you use an argument to start the application minimized like:

YourProgram.exe -m
Peter
I want the user himself to create this shortcut in the "Startup" folder, and I want to make it as simple as possible, which means: no messing with command-line parameters.
Alex Jenter
+1  A: 

Check the registry for this key. HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Run and add a new String key with the path of your application as value. and use NOTIFYICONDATA structure for minimizing your application to the tray.

GreenShadow
The problem is I don't want to write to the registry.
Alex Jenter
in which OS is this application running.?
GreenShadow
ok then place the shortcut of the exe to the startup folder.
GreenShadow
and? how should the program detect that it should run minimized? as I said, no command-line parameters: the shortcut will be put to the "startup" folder manually
Alex Jenter
You can google NOTIFYICONDATA, a structure used to minimize the application as tray icons.
GreenShadow
I think you misunderstood my question, please re-read.
Alex Jenter
When you want to show your app icon in the system tray, call the win API Shell_NotifyIcon()
GreenShadow
The question is NOT how to minimize to the system tray. The question is how can a program differentiate between being run normally and being run from a startup-folder shortcut without using registry and command-line parameters.
Alex Jenter
Question was poorly asked, and should be edited. You'll know the path you started from, but whether the app was started from one of a dozen different shortcuts all over the user's drive.. not sure you can tell.
Jeff Atwood
I'm sorry for the poor formulation (it was late in the morning;) I've edited the question.
Alex Jenter
This should be in a separate question, but it helped me, thanks
djeidot
+3  A: 

Your "cleanness" appears to be an artificial construct at best. If you're telling the user to create a shortcut in the start-up folder, you're already leaving a footprint (and, to be honest, there's little difference between "myprog.exe" and "myprog.exe -m"). In that case, there are some easier approaches than automagically trying to detect where you're running from.

I would simply provide a menu option in your program ("Install") which would then install the software to a fixed-disk location (as opposed to the flash drive), including the requisite Programs entry (Start, All Programs, CintaNotes).

As part of that process (or even after install), you can let them specify "Start with Windows" and then you create the start-up folder shortcut for the user with a command line option so your program can tell if it's running that way. There's no point in allowing "Start with Windows" unless the program's available (i.e., on the fixed disk, not the flash drive).

Your user need never have to worry about creating shortcuts at all, let alone ones with parameters. And this gives your program the control over how it's run - two modes, "installed" (and start minimized) or "running without installing first" (and start normal).

Something like finding the directory of the executable won't work simply because the start-up folder item that starts your program is likely to be a shortcut to it, so you won't have that path.

I think this is a classic case of asking the wrong question. In answer to your specific question, I say: no, there is no way to tell that you've been launched from a start up folder entry without some command-line parameters. But, I've been wrong before, just ask my wife :-). Some-one else may well know a way.

Adding this an an edit since comments don't allow enough space:

You ask:

What do you think of just disabling the "Start when Windows starts" option when program detects it is being run from the flash drive? I guess there's a way to detect this.

That's a good idea since it doesn't make sense to allow automatic running until it's installed (since the flash drive may not be there). One possibility:

1/ Running from flash, you start with "myprog.exe" since you just double-clicked the executable and you run in a normal window. This presents the option to "Install to fixed disk" but not "Start with Windows". As part of the install process, you may allow them to set up the installed copy to "Start with Windows" but not the current running copy.

2/ Your installed program can be set up to run as "myprog.exe -i", since you create the item in Start/AllPrograms. This would disable "Install to fixed disk" but allow you to toggle "Start with Windows". You can choose whether you want explicit running (by the user) to start normal or minimized (see (3)).

3/ The shortcut in StartUp can be "myprog.exe -s" (again, you control this because you created it). This is identical to (2) but starts minimized (if (2) started minimized anyway, there's no distinction between (2) and (3) and no reason for different command-line options).

That way, each option can have different behavior as you see fit.

paxdiablo
Ok, thanks, you've persuaded me :) What do you think of just disabling the "Start when Windows starts" option when program detects it is being run from the flash drive? I guess there's a way to detect this.
Alex Jenter
Thanks, that's a nice solution.
Alex Jenter