files generated in system's %appdata% rather than user's %appdata% when running mpi

StackOverflow https://stackoverflow.com/questions/14397892

  •  16-01-2022
  •  | 
  •  

Pergunta

I am running my application (sample.exe) which uses an external DLL (i.e. I do not have the source of this DLL) on windows. External DLL generates some data in the %appdata% folder. Everything runs fine when the application is run standalone.

Now, when I run the sample.exe using MPI the logs are getting generated in "C:\Windows\System32\config\systemprofile\AppData"

I understand that this is the %appdata% for SYSTEM. Also, MPI uses "smpd" service to run which needs to be installed as Administrator. So, I thought probably the sample.exe is owned by the system and not by the user. But, when checked from the task manager the owner of the application is still the user(%username%).

I dont understand why are the logs generated in the system's appdata folder rather than the user's appdata folder.

Foi útil?

Solução

Since sample.exe is your own application, you can troubleshoot the problem by adding logging code. For example:

  • Call SHGetKnownFolderPath to determine the application data folder.
  • Call GetUserName to check what user account you are running under.
  • Call GetEnvironmentStrings to determine how the environment variables are set, paying particular attention to %APPDATA% and %LOCALAPPDATA%.
  • If this is a C program, use getenv("APPDATA") and getenv("LOCALAPPDATA") as well; IIRC, the C runtime library keeps it's own copy of the environment variables.

If the application data folder is correct but the environment variable isn't (this would be my first guess as to the problem) then you can try using SetEnvironmentVariable and/or _putenv to correct the path before loading the external library.

Outras dicas

The likely explanation is that a service running under the LOCALSYSTEM account is saving the file. Knowing very little about the architecture of your application, I can't say in any detail how this comes about.

This is because your Windows service is running with "Local System Account". Go to the properties of your service and the "LogOn" tab. Change from Local System Account to "This account" and give your credentials. NO, the service should run in User account and should use the user AppData.

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top