如何检查当前登录会话期间程序是否已执行? (视窗)

发布于 2024-08-23 05:23:19 字数 556 浏览 6 评论 0原文

在我正在开发的应用程序(用 C 语言)中,必须首先运行加载器应用程序,然后才能执行主应用程序。我已经做到了,因此主应用程序在启动时将加载程序应用程序作为子应用程序运行,以便自动执行此任务。这一切都运行良好,除了加载器应用程序只需要在用户当前登录会话期间运行一次,以便主应用程序正常工作,并且每次主应用程序执行时都必须运行它是一个真正的问题。疼痛。所以我要问的是:有没有办法检查加载程序是否已经使用某些 Windows 或 C 功能运行? 理想情况下,这样的事情会很棒,尽管我不知道它是否可能:

if(thisapp.exe hasBeenExecuted)
    return;
else
    spawnl(app_path, app_name, args, NULL);

或者可能是让主应用程序创建一个虚拟文件,在加载程序运行时充当标志。然后在后续执行中,检查文件是否存在,如果不存在则运行加载程序。唯一的问题是自动化它,因此当计算机关闭时虚拟文件会被删除(有办法做到这一点吗?)。

一些澄清: 我无法编辑加载程序应用程序或在主应用程序中复制其功能,它不是我编写的。

预先感谢您的任何帮助

In an application I am developing (in C), a loader app must first be run before the main application is executed. I have made it so the main app runs the loader app as a child when it is launched in order to automate this task. This all works well, except for the fact that the loader app only needs to be run one time during the user's current login session in order for the main app to work correctly, and having to run it every time the main app executes is a real pain. So what I'm asking is: is there a way to check if the loader has already been run using some Windows or C functionality?
Ideally something like this would be great, although I have no idea if it is even possible:

if(thisapp.exe hasBeenExecuted)
    return;
else
    spawnl(app_path, app_name, args, NULL);

Or maybe something like having the main app create a dummy file that acts as a flag when the loader is run. Then on subsequent executions, checking if the file exists and running the loader if it does not. The only problem with this is automating it so the dummy file is erased when the computer is turned off (is there a way to do this?).

Some clarification:
I cannot edit the loader app or replicate its functionality in the main app, I did not write it.

Thanks in advance for any help

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

墨落画卷 2024-08-30 05:23:19

有几种方法可以做到这一点。

您的应用程序可以向 Run 键添加一个条目。这使得它保证在用户登录时每个会话至少运行一次。在这种情况下,您可以向它传递一个特殊的命令行参数,以将其与重新启动区分开来。

另一种选择可能是将您的状态存储在当前用户配置单元中,并使用 REG_OPTION_VOLATILE 标志。这会导致密钥仅存储在内存中,并且在卸载配置单元(应该是用户注销时)时不会将密钥刷新到磁盘。

There are a couple ways to do this.

Your application could add an entry to the Run key. This makes it guaranteed to run at least once per session, when the user logs on. You could pass it a special command line arg in this case to distinguish it from relaunches.

Another option could be to store your state in the current user hive, and create the key with the REG_OPTION_VOLATILE flag. This causes the key only to be stored in memory and will not be flushed to disk when the hive is unloaded, which should be when the user logs off.

花开半夏魅人心 2024-08-30 05:23:19

您还可以使用原子函数,而无需读取/写入巨大的文件

http://msdn.microsoft.com/en-us/library/ms649053%28VS.85%29.aspx

You could also use the atom functions without having to read/write an enormous file

http://msdn.microsoft.com/en-us/library/ms649053%28VS.85%29.aspx

深白境迁sunset 2024-08-30 05:23:19

我过去见过的一个建议是在加载时从用户配置单元(即注册表)运行一个应用程序,该应用程序会删除您的文件(或者创建它)。优点是这可以像在启动期间运行的 shell 脚本(批处理文件)一样简单(或用户配置文件加载,取决于您挂钩的位置),并且在大多数情况下应该运行一次(在启动时或用户登录时) )。如果您选择登录,则每次用户注销后登录到计算机时都可以(将)运行它。

您也可以在用户注销时执行此操作,尽管这不太可靠,因为您并不总是知道卸载配置单元时机器的状态(它可能会崩溃、断电等)。

它不是一个完整的证明方法,它只能在 Windows 上工作,但在大多数正常使用场景下它可以工作。

作为替代技巧,您可以将时间戳写入文件,然后在加载时获取时间戳并检查它是否小于当前用户登录的时间。如果是,那么您可以跳过加载,因为它很可能发生在当前用户会话期间。

同样,这也有一些缺点,但它应该也能很好地工作。

One suggestion that I have seen used in the past is to run an application from the user-hive (i.e. registry) on load which deletes your file (or alternately, creates it). The advantage is that this can be as simple as a shell script (batch file) which runs during startup (or user profile load, depending on where you hook) and should under most cases be run once (either at startup, or at user login). If you chose login, it can (will) be run every time the user logs into the machine after having logged out.

You could do it on user log out as well, though this is somewhat less reliable since you do not always know the state of the machine when the hive is unloaded (it could be crashed, out of power, etc).

Its not a full proof way of doing it, and it will only work on windows, but under most normal usage scenarios it would work.

As an alternate trick, you could write a timestamp to a file and when you load up, get the timestamp and check to see if its less than the amount of time the current user has been logged on for. If it is then you can skip the load as it most likely occurred during the current users session.

Again there are some downsides to this, but it should work reasonably well as well.

凶凌 2024-08-30 05:23:19

加载应用程序是做什么的,这意味着它需要首先运行?

在我看来,正确的答案是检查是否已完成,因为这才是您真正关心的。其他任何东西都可能很脆弱并且在极端情况下会损坏。

What does the loader app do, that means it needs to be run first?

It seems to me that the right answer is to check and see if that has been done, since that's what you really care about. Anything else is likely to be fragile and break in corner cases.

不忘初心 2024-08-30 05:23:19

我过去使用互斥锁实现了这一点。

例如:

int main( int argc, char argv[][] )  
{
    // if Mutex exists
    // {
    //     Don't allow a second instance to run:
    //     Notify user, log something, etc.
    //     return 0;
    // }
    // else
    // {
    //     Create Mutex, this is the first time running
    // }

    // Rest of your program runs from here . . .
}

-bn

I have implemented this in the past using a Mutex.

For example:

int main( int argc, char argv[][] )  
{
    // if Mutex exists
    // {
    //     Don't allow a second instance to run:
    //     Notify user, log something, etc.
    //     return 0;
    // }
    // else
    // {
    //     Create Mutex, this is the first time running
    // }

    // Rest of your program runs from here . . .
}

-bn

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文