Win32 应用程序在启动时不运行 [英] Win32 application doesn't run on startup

查看:32
本文介绍了Win32 应用程序在启动时不运行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 Visual Studio 2015 中创建了一个 C++ Win32 应用程序.我的目标是让它在启动时运行.我在注册表中添加了一个新条目(HKCU\Software\Microsoft\Windows\CurrentVersion\Run).由于未知原因,它不起作用.

I have created a C++ Win32 application in Visual Studio 2015. My goal is to make it run at startup. I added a new entry in registry (HKCU\Software\Microsoft\Windows\CurrentVersion\Run). For an unknown reason, it doesn't work.

然而,它在任务管理器的启动选项卡中正确显示,但不在进程选项卡中,这意味着它根本不运行.我试图将它包装在一个 bat 文件中.bat 运行,但应用程序似乎在启动时失败(或完全被忽略,我不能确切地说).如果我从 Program Files 目录手动启动,它会正常运行.

However it appears correctly in the Task Manager at the Startup tab but not in the Process tab which means it doesn't run at all. I tried to wrap it in a bat file. The bat runs but the application seems to fail at launch (or totally ignored, I can't tell exactly). If I launch manually from Program Files directory, it runs normally.

这是一个朋友的旧 Visual 项目,并在当前的 Visual (2015) 中进行了转换.如果我从头开始创建一个新项目,它就可以工作.但这种行为没有任何理由.

It's an old Visual project from a friend and it was converted in the current Visual (2015). If I create a new project from scratch, it works. But there is no reason for such behavior.

最后的想法 - 我成功地解决了我的问题.我的程序无法找到 SQLite 文件,因为 Windows 在启动时启动的任何程序都使用c:\Windows\System32"作为当前目录.我刚刚应用了 Marko Popovic 的解决方案.这只是一个简单的当前目录问题.

Final thought - I successully resolved my issue. My program was unable to find the SQLite file because any programs launch by Windows on startup use "c:\Windows\System32" as current directory. I just applied Marko Popovic's solution. It was just a simple current directory problem.

PS - Marko Popovic 的解决方案很好,但 @IInspectable 警告此方法可能发生的传入错误.他建议只使用完全限定的路径名​​而不改变目录.

PS - Marko Popovic's solution is good but @IInspectable warns about incoming bugs that could occured with this method. He recommands to only use fully qualified pathname without changing dir.

推荐答案

当应用程序使用运行"键启动时,它的工作目录不是它的图像文件 (*.exe) 所在的目录.您可以通过在主函数的最开始调用 Win32 函数 SetCurrentDirectory 来更改此设置.这是解决方案的框架:

When the application is started using the "Run" key, its working directory is not the one where its image file (*.exe) resides. You can change this by calling Win32 function SetCurrentDirectory at the very beginning of your main function. Here is a skeleton of a solution:

// Use GetModuleFileName to retrieve path of the executable file
TCHAR pszPathToSelf[MAX_PATH];
DWORD dwPathLength = GetModuleFileName(NULL, pszPathToSelf, MAX_PATH);
if(dwPathLength > 0)
{
    // Code that extracts directory path from pszPathToSelf and places it into variable pszNewWorkingDirectory of type TCHAR
    ...

    BOOL bSuccess = SetCurrentDirectory(pszNewWorkingDirectory);
    if(0 == bSuccess)
    {
    // SetCurrentDirectory failed for some reason, handle how you see fit
    ....
    }
} 

这样,您的函数将更改其工作目录并加载必要的 DLL.

This way, your function will change its working directory and load the necessary DLL.

正如用户 IInspectable 指出的那样,上述方法在默认情况下不起作用,但仅适用于一些特殊情况.更好的方法是在 App Paths 注册表项下注册您的应用程序.如果您的应用程序名为 my_app.exe,请创建以下注册表项:HKEY_LOCAL_MACHINE\Software\Microsoft\Windows\CurrentVersion\App Paths\my_app.exe然后创建一个名为PathREG_SZ 值,该值将包含保存my_app.exe 的目录的路径.

As user IInspectable pointed out, the above approach will not work in the default case, but only in some special ones. A better approach would be to register your application under App Paths registry key. If your application is named my_app.exe, create the following registry key: HKEY_LOCAL_MACHINE\Software\Microsoft\Windows\CurrentVersion\App Paths\my_app.exe and then create a REG_SZ value namedPath, which will contain the path to directory that holds my_app.exe.

有关更多详细信息,请参阅以下 MSDN 页面:

For more details, refer to the following MSDN page:

https://msdn.microsoft.com/en-us/library/windows/desktop/ee872121%28v=vs.85%29.aspx

抱歉最初的错误!

这篇关于Win32 应用程序在启动时不运行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆