Threads accessing a remote machine
-
Sunday, February 12, 2012 2:25 PM
Hi everyone,
I'asking for support because I'm unable to understand where the problem actually is in the following scenario.
My C# application wants to access WMI data on a remote machine.
The application runs on PCLocal under an administrative account (AdmLocal); the remote machine is PCRemote (ip 10.20.1.151) and the account to use is AdmRemote with password "system"; both machines are XP SP3; they are NOT into a domain.
I can easily access PCRemote with AdmRemote credentials (i.e. start \\10.20.1.151\c$).
The code is the following:
public void DoQuery()
{
try
{
ManagementScope theScope = new ManagementScope("\\\\10.20.1.151\\root");
theScope.Options.Username = "AdmRemote";
theScope.Options.Password = "system";
theScope.Options.Authentication = AuthenticationLevel.Default;
theScope.Options.Impersonation = ImpersonationLevel.Impersonate;
ManagementPath thePath = new ManagementPath("__namespace");
ManagementClass theClass = new ManagementClass(theScope, thePath, null);
EnumerationOptions opts = new EnumerationOptions();
opts.ReturnImmediately = false;
foreach (ManagementObject o in theClass.GetInstances(opts))
{
Console.WriteLine(o["name"].ToString());
}
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
}Case 1) the code is run from the main (form) thread
if I execute DoQuery() for example in a buttonQuery_click handler, the connect and query run fine; I get the data etc. but looking at the Security Event Log on PCRemote I see that there are successful logins from AdmRemote AND failed logins from the account logged in on PCLocal (AdmLocal) as if something got confused about which credentials to use - it seems an harmless bug in ManagementScope.Connect.
Case 2) the code is run from a separate thread
suppose that DoQuery is run from a separate thread (buttonQuery_click starts a thread, the thread runs DoQuery and raises an event when all is complete);
a) Connect and GetInstances run fine, they do not report errors;
b) as soon as I try to access/enumerate the returned collection (doing this in the main or worker thread has the same effect) I get an 0x80070005 ACCESS_DENIED exception;
c) the Security Events Log on PCRemote shows the same successful and failed login attemps, only happening in different order;
Of course if I create an account on PCRemote having the same credentials of the user logged on PCLocal, everything's fine.
Am I missing something about threading with C#? Should the worker thread do something specific to manage the remote connection?
Best Regards,
Andrea.
Answers
-
Sunday, February 12, 2012 4:17 PMModerator
Not my area of expertise, but I am trying to decide which forum may be better to ask this question. The Windows Forum, or the Remoting Forum.
Does the sample at this link work for you?
http://msdn.microsoft.com/en-us/library/ms257337(v=VS.80).aspx
I notice that the sample at the link goes about its' business in a rather different way than you do.
http://msdn.microsoft.com/en-us/library/ms143592.aspx local connection with ManagementClass (class allows remoting?)
Rudy =8^D
Mark the best replies as answers. "Fooling computers since 1971."
- Marked As Answer by Bob ShenMicrosoft Contingent Staff, Moderator Thursday, February 23, 2012 9:34 AM
-
Sunday, February 12, 2012 5:09 PM
Hi Andrea,
Rudy gave you some ncie links already. But I hope that I can give some helpfull information, too.
Inour development, we try to not bother around with usernames and passwords. It is always a bad idea in my eyes, because an administrator of one system has a chance to get the next account for another system. So we complely avoid any username / password management where possible. Instead we always use the buildin securit stuff e.g. Windows Integrated Authentication.
So instead of giving a username / password, it should be possible to simply let the administrator enter these details inside the used account. I do not have a windows xp system here to check where the settings are but in Windows 7 you find it inside the control panel / user management a user can setup how different systems should be accessed by default. So if you insert there that 10.20.1.151 should be accessed as AdmRemote with password system, the connection should directly work.
I hope that this information is also usefull for your development. From my perspective it simply keeps stuff where it belongs to and at the same time simplifies the whole development.
With kind regards,
Konrad
- Marked As Answer by Bob ShenMicrosoft Contingent Staff, Moderator Thursday, February 23, 2012 9:34 AM
All Replies
-
Sunday, February 12, 2012 4:17 PMModerator
Not my area of expertise, but I am trying to decide which forum may be better to ask this question. The Windows Forum, or the Remoting Forum.
Does the sample at this link work for you?
http://msdn.microsoft.com/en-us/library/ms257337(v=VS.80).aspx
I notice that the sample at the link goes about its' business in a rather different way than you do.
http://msdn.microsoft.com/en-us/library/ms143592.aspx local connection with ManagementClass (class allows remoting?)
Rudy =8^D
Mark the best replies as answers. "Fooling computers since 1971."
- Marked As Answer by Bob ShenMicrosoft Contingent Staff, Moderator Thursday, February 23, 2012 9:34 AM
-
Sunday, February 12, 2012 5:09 PM
Hi Andrea,
Rudy gave you some ncie links already. But I hope that I can give some helpfull information, too.
Inour development, we try to not bother around with usernames and passwords. It is always a bad idea in my eyes, because an administrator of one system has a chance to get the next account for another system. So we complely avoid any username / password management where possible. Instead we always use the buildin securit stuff e.g. Windows Integrated Authentication.
So instead of giving a username / password, it should be possible to simply let the administrator enter these details inside the used account. I do not have a windows xp system here to check where the settings are but in Windows 7 you find it inside the control panel / user management a user can setup how different systems should be accessed by default. So if you insert there that 10.20.1.151 should be accessed as AdmRemote with password system, the connection should directly work.
I hope that this information is also usefull for your development. From my perspective it simply keeps stuff where it belongs to and at the same time simplifies the whole development.
With kind regards,
Konrad
- Marked As Answer by Bob ShenMicrosoft Contingent Staff, Moderator Thursday, February 23, 2012 9:34 AM
-
Sunday, February 12, 2012 9:09 PM
Hi Rudy,
the samples you linked do different actions on WMI (I'm trying to enumerate the namespaces, the samples enumerate class instances) but the overall concept is the same: set up a ManagementScope with proper credentials, connect to the remote machine, run a query.
However, the experienced behaviour is the same: the samples work correctly if the code is run in the main thread, do not if the code is run by a secondary thread.
Thanks!
-
Sunday, February 12, 2012 9:13 PMModerator
Are the samples producing the same error messages?
Mark the best replies as answers. "Fooling computers since 1971."
http://thesharpercoder.blogspot.com/
- Edited by Rudedog2MVP, Moderator Sunday, February 12, 2012 9:13 PM
-
Sunday, February 12, 2012 9:15 PM
Hi Konrad,
thanks for your feedback; I need to get some background on the subject of WIA and see if and how it fits in the WMI context.
Regards,
Andrea
-
Wednesday, February 15, 2012 9:55 AMModerator
Hi Andrea,
How's it going? Do you have any updates about the issue?
Bob Shen [MSFT]
MSDN Community Support | Feedback to us
-
Monday, March 05, 2012 8:49 PM
Sorry for the delay, I shifted to other activities.
1) I forgot that Windows Forms are single thread by default...
2) It works fine when I change from STAThread to MTAThread into Program.cs
namespace WMINavigator{
static class Program
{
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new Form1());
}
}
}
-
Tuesday, March 06, 2012 10:51 PM
Ok, using MTAThread keeps things working fine as far as I do sync operations in threads created by MY objects/classes.
If I use async operations using the mechanisms provided by System.Management objects, which create their own threads, the "access denied" persists.
a) it's not a problem of permissions, because the remote machine is accessible and returns proper data when queried
b) System.Management relies on COM interop, so (a) the problem is something inside System.Management or (b) I'm either missing some detail or messing things up...

