.NET and COM interop - How do I get environment variables to the COM process when using Windows Task Scheduler
-
18 เมษายน 2555 21:26
I have a .NET application that needs to be scheduled to run using Windows Task Scheduler. The task is scheduled to run whether the user is logged in or not and we check the box to run with high priviledges. When we finish with the task, we have to give the login and password to the task scheduler.
The .NET executable uses the Activator.CreateInstance API to create an instance of our COM application using a System.Type variable (via GetTypeFromProgID). However when it starts, the COM application does not have access to the user's environment variables if the user actually logs out before the scheduled task runs.
I have added calls in the .NET application to Enviornment.GetEnvironmentVariable and log the values. The .NET process, whether the user is logged in or not, succeeds in getting the environment variables so I know they are set and available to the .NET process.
I modified my COM app to log the values of the same environment variables. When the user is logged out and the app is started via the Activator call by the scheduled task when it starts, the COM app finds that the environment variables are not set. So the environment for the user is not propogated to the COM process started by the .net process started by the task scheduler.
I suspect that I need to change the security settings of the .NET process so the COM process it creates via the Activator also has access to the user's environment whether the user remains logged in or not.
Does anyone know how I can accomplish this?
R.D. Holland
ตอบทั้งหมด
-
20 เมษายน 2555 9:38
Hi,
I'm confused with the purpose about this application. And I don't think an application can be run ignore the users is a health application.
>>The .NET process, whether the user is logged in or not, succeeds in getting the environment variables so I know they are set and available to the .NET process.
If your application is ignore the users, why you need to get the information about the users' information?
>>I have a .NET application that...... I modified my COM app to log the values of the same environment variables.
So which one do you want to deal with your issue, .NET application and COM applicaition? According to your description, It seems that there is no need about COM object. I'm afraid that you issue about the Creating a the COM object is a little off-topic here.
No code, No fact.
-
20 เมษายน 2555 13:13
cal,
I don't know why the purpose matters. We have a COM based application that supports automation. We have a user that has written a .NET application that drives our application via automation (the user may be creating an assembly line, running finite element analysis on airplane wing and storing the results in a database, or any number of things our application is used to do interactively). There is no "ignoring the user". We need the user's profile and especially access to the registry (HKCU) and system resources, including environment variables.
The user schedules the .NET process, logs out and goes home. When they return they expect that our application was started and it was able to process all the API calls. Our application needs access to the system and especially the user's profile, resources and environment variables.
If the user schedules the task to run in five minutes and just sits and waits, the .net process gets kicked off. It in turn starts our COM app and everything works. But if the user logs out, the .net process is kicked off and it starts our app but our app is not able to proceed very far because it does not have access to the user's environment variables.
The COM object is crucial to the user's workflow. In fact, if the COM app is not available, the .NET code is totally useless. The .net process has no code to perform finite element analysis nor any code to associate the resulting files with the engineering files stored in a database and load them into the database. Our app does that.
So the question remains. Why does the .NET process have access to the user's profile and especially env variables when it starts after the user logs out but the process it starts does not? And more importantly, is there anything I can do to enable the secondary process to have access to the same?
R.D. Holland
-
23 เมษายน 2555 20:40I wrote a windows app that did not use .NET. I have found that it has the same problem. The COM process I start up using the prog ID of our app (after calling OleInitialize) does not have access to the user's environment variables. So this appears to be a generic issue with spawning a process from the process started by the scheduler.
R.D. Holland
-
1 พฤษภาคม 2555 18:55
It turns out that Task scheduler runs the task under the system account when one specifies that the task should be run whether the user is logged in or not. So even though TS asks the user for their password, giving the impression that the task will run under the user account, it does not. For whatever reason, running under the system account will give the task access to the user's env variables, but not any task spawned from the original task.R.D. Holland
- ทำเครื่องหมายเป็นคำตอบโดย RD Holland 1 พฤษภาคม 2555 18:58