0

I have an application which is installed and run as a Windows service. In a given time, I need to locate the current application path (that is, the installation path) where other, needed files are located.

Unfortunately I'm being unable to find this directory using the traditional methods (QCoreApplication::applicationDirPath(), dir.absolutePath(), QDir::currentPath()). Every time this functions are called, they either return blank or the system32 folder.

I tried to improvise using QStandardPaths::findExecutable, but it didn't solve my problems. My last resort for now is creating a script that creates an entry in the registry telling the service where to look for, but since the installation is supposed to be just an "extract from compressed file", I'd like to avoid this option.

So which other method Qt makes available to make this specific circunstancial task?

Bearded Beaver
  • 646
  • 4
  • 21
Momergil
  • 2,213
  • 5
  • 29
  • 59
  • Possible duplicate of https://stackoverflow.com/questions/7402576/get-current-working-directory-in-a-qt-application – Mohammad Kanan Jan 16 '18 at 15:12
  • Did you make sure the QCoreApplication is initialized before you call applicationDirPath()? – SteakOverflow Jan 16 '18 at 15:15
  • 1
    If the code in question is running inside the service itself, then retrieving the path of the calling process is easy ([`GetModuleFileName(NULL)`](https://msdn.microsoft.com/en-us/library/windows/desktop/ms683197.aspx), then strip off the filename portion), which Qt should have wrappers for. But, if all else fails (and it shouldn't, if you are using Qt correctly), you could use [`QueryServiceConfig()`](https://msdn.microsoft.com/en-us/library/windows/desktop/ms684932.aspx) to ask the SCM where the service is installed. – Remy Lebeau Jan 16 '18 at 15:47
  • @MohammadKanan I'm sorry, but it seems you didn't read my question at all. It's not a duplicate since I'm talking specifically about Qt running as Windows Service and I've already tested the solutions shown in the link you provided (which btw I red before putting this question). – Momergil Jan 17 '18 at 10:00
  • @SteakOverflow yes: before calling it, I start an object of the class 'class ServiceController : public QtService', which I suppose does what you sad. – Momergil Jan 17 '18 at 10:02
  • 1
    @Momergil, Yes sorry I realized that you talk about win service after posting, I was surprised of its different behave ... anyway I left the link for you cause it could help. – Mohammad Kanan Jan 17 '18 at 10:39
  • @RemyLebeau GetModuleFileName(NULL) did the trick, thanks! Could you please post it as an answer so I may mark it as the right one? (And MohammadKanan, OK, no problems =) ) – Momergil Jan 17 '18 at 10:48
  • 1
    @Momergil, hoping to compensation! regarding your workaround " _a script that creates an entry in the registry telling the service where to look for_" if the logic is good for you, then you don't need a script .. you can use the **QSettings** class to do that for yo. have a look https://stackoverflow.com/questions/48168909/qt-session-management-on-android-and-ios/48174537#48174537 – Mohammad Kanan Jan 17 '18 at 11:31
  • @MohammadKanan Curious: I'm aware QSettings works with registry, but I don't know how would it store the installation directory path on it since it needs to grab it in the first place and there is precisely where my problem lies, how to make Qt know the installation path. – Momergil Jan 17 '18 at 12:41
  • @Momergil, have you tried the static : `QString QCoreApplication::applicationDirPath() [static]` .. More infor here: http://doc.qt.io/qt-5/qcoreapplication.html#applicationDirPath – Mohammad Kanan Jan 17 '18 at 12:49
  • 1
    @MohammadKanan according to the OP's question, yes, and it didn't work – Remy Lebeau Jan 17 '18 at 16:44

0 Answers0