WMI

T

trinitypete

Hi All,

I have an explorer type app that I am having a little
trouble with. I use WMI to obtain a list of drives on the
system. The first time through it works fine but if I try
and execute the same WMI Query again I get an error:

An unhandled exception of
type 'System.Management.ManagementException' occurred in
system.management.dll

Additional information: COM object that has been separated
from its underlying RCW can not be used.


The code to access the WMI consists of:

ConnectionOptions opts = new ConnectionOptions();
ManagementScope scope = new
ManagementScope(@"\\.\root\cimv2",opts);
scope.Connect();
//Use WMI to get lists of all
logical drives and their type
ManagementObjectSearcher wmisearch
= new ManagementObjectSearcher(scope,new ObjectQuery
("select Name, DriveType, MediaType,ProviderName from
Win32_LogicalDisk"));
ManagementObjectCollection results
= wmisearch.Get();
MessageBox.Show(results.Count.ToString())

As I say - first time through all works OK, then a refresh
button on the explorer app runs the method containing the
above again, then I get the exception.

Thanks in advance Pete.
 
J

Jeffrey Tan[MSFT]

Hi Pete,

I think just this code snippet will not generate problem(At least on my
machine).
You can setup a WinForm, in the Button's click event paste this code
snippet, how many times you click the button, it will not throw exception.

ConnectionOptions opts = new ConnectionOptions();
ManagementScope scope = new ManagementScope(@"\\.\root\cimv2",opts);
scope.Connect();
ManagementObjectSearcher wmisearch = new ManagementObjectSearcher(scope,new
ObjectQuery("select Name, DriveType, MediaType,ProviderName from
Win32_LogicalDisk"));
ManagementObjectCollection results = wmisearch.Get();
MessageBox.Show(results.Count.ToString());

But there is a known issue that if you Enumerate the
ManagementObjectCollection after calling the Count property.

Such like this:
ConnectionOptions opts = new ConnectionOptions();
ManagementScope scope = new ManagementScope(@"\\.\root\cimv2",opts);
scope.Connect();
//Use WMI to get lists of all logical drives and their type
ManagementObjectSearcher wmisearch = new ManagementObjectSearcher(scope,new
ObjectQuery("select Name, DriveType, MediaType,ProviderName from
Win32_LogicalDisk"));
ManagementObjectCollection results = wmisearch.Get();
MessageBox.Show(results.Count.ToString());

foreach(ManagementObject mo in results)
{
MessageBox.Show(mo["Name"].ToString());
}

I think you may be this problem.
If it is, you should call the count property after Enumerating the
ManagementObjectCollection.

Hope this helps,

Best regards,
Jeffrey Tan
Microsoft Online Partner Support
Get Secure! - www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.

--------------------
| Content-Class: urn:content-classes:message
| From: "trinitypete" <[email protected]>
| Sender: "trinitypete" <[email protected]>
| Subject: WMI
| Date: Fri, 10 Oct 2003 09:06:15 -0700
| Lines: 36
| Message-ID: <[email protected]>
| MIME-Version: 1.0
| Content-Type: text/plain;
| charset="iso-8859-1"
| Content-Transfer-Encoding: 7bit
| X-Newsreader: Microsoft CDO for Windows 2000
| X-MimeOLE: Produced By Microsoft MimeOLE V5.50.4910.0300
| Thread-Index: AcOPSG6xJD4XDBCQTniUaGEaux1O1g==
| Newsgroups: microsoft.public.dotnet.framework
| Path: cpmsftngxa06.phx.gbl
| Xref: cpmsftngxa06.phx.gbl microsoft.public.dotnet.framework:55892
| NNTP-Posting-Host: TK2MSFTNGXA14 10.40.1.166
| X-Tomcat-NG: microsoft.public.dotnet.framework
|
| Hi All,
|
| I have an explorer type app that I am having a little
| trouble with. I use WMI to obtain a list of drives on the
| system. The first time through it works fine but if I try
| and execute the same WMI Query again I get an error:
|
| An unhandled exception of
| type 'System.Management.ManagementException' occurred in
| system.management.dll
|
| Additional information: COM object that has been separated
| from its underlying RCW can not be used.
|
|
| The code to access the WMI consists of:
|
| ConnectionOptions opts = new ConnectionOptions();
| ManagementScope scope = new
| ManagementScope(@"\\.\root\cimv2",opts);
| scope.Connect();
| //Use WMI to get lists of all
| logical drives and their type
| ManagementObjectSearcher wmisearch
| = new ManagementObjectSearcher(scope,new ObjectQuery
| ("select Name, DriveType, MediaType,ProviderName from
| Win32_LogicalDisk"));
| ManagementObjectCollection results
| = wmisearch.Get();
| MessageBox.Show(results.Count.ToString())
|
| As I say - first time through all works OK, then a refresh
| button on the explorer app runs the method containing the
| above again, then I get the exception.
|
| Thanks in advance Pete.
|
 
T

trinitypete

Jeffrey,

Thanks for the reply, for some reason when I try and return to the
newsgroups via http://msdn.microsoft.com/newsgroups/managed/, quite a
few of the newsgroups display newsgroup unavailable, yet I can see
them through google groups? Any idea why?

BTW as I cant see the Newsgroups can you mail me at:

(e-mail address removed) - obviously removing the
'dontspam'

Anyhow getting back to the WMI problem, If I put the code in a button
it does seem to work correctly. I have a simple explorer type user
control where it consistantly gives this error, yet I do not reference
the count property of the managementobjectcollection. May I send you a
code simple example user control and wrapper?

If so do I just drop the 'online' from the EM address?

Thanks for your time - Pete.

Hi Pete,

I think just this code snippet will not generate problem(At least on my
machine).
You can setup a WinForm, in the Button's click event paste this code
snippet, how many times you click the button, it will not throw exception.

ConnectionOptions opts = new ConnectionOptions();
ManagementScope scope = new ManagementScope(@"\\.\root\cimv2",opts);
scope.Connect();
ManagementObjectSearcher wmisearch = new ManagementObjectSearcher(scope,new
ObjectQuery("select Name, DriveType, MediaType,ProviderName from
Win32_LogicalDisk"));
ManagementObjectCollection results = wmisearch.Get();
MessageBox.Show(results.Count.ToString());

But there is a known issue that if you Enumerate the
ManagementObjectCollection after calling the Count property.

Such like this:
ConnectionOptions opts = new ConnectionOptions();
ManagementScope scope = new ManagementScope(@"\\.\root\cimv2",opts);
scope.Connect();
//Use WMI to get lists of all logical drives and their type
ManagementObjectSearcher wmisearch = new ManagementObjectSearcher(scope,new
ObjectQuery("select Name, DriveType, MediaType,ProviderName from
Win32_LogicalDisk"));
ManagementObjectCollection results = wmisearch.Get();
MessageBox.Show(results.Count.ToString());

foreach(ManagementObject mo in results)
{
MessageBox.Show(mo["Name"].ToString());
}

I think you may be this problem.
If it is, you should call the count property after Enumerating the
ManagementObjectCollection.

Hope this helps,

Best regards,
Jeffrey Tan
Microsoft Online Partner Support
Get Secure! - www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.

--------------------
| Content-Class: urn:content-classes:message
| From: "trinitypete" <[email protected]>
| Sender: "trinitypete" <[email protected]>
| Subject: WMI
| Date: Fri, 10 Oct 2003 09:06:15 -0700
| Lines: 36
| Message-ID: <[email protected]>
| MIME-Version: 1.0
| Content-Type: text/plain;
| charset="iso-8859-1"
| Content-Transfer-Encoding: 7bit
| X-Newsreader: Microsoft CDO for Windows 2000
| X-MimeOLE: Produced By Microsoft MimeOLE V5.50.4910.0300
| Thread-Index: AcOPSG6xJD4XDBCQTniUaGEaux1O1g==
| Newsgroups: microsoft.public.dotnet.framework
| Path: cpmsftngxa06.phx.gbl
| Xref: cpmsftngxa06.phx.gbl microsoft.public.dotnet.framework:55892
| NNTP-Posting-Host: TK2MSFTNGXA14 10.40.1.166
| X-Tomcat-NG: microsoft.public.dotnet.framework
|
| Hi All,
|
| I have an explorer type app that I am having a little
| trouble with. I use WMI to obtain a list of drives on the
| system. The first time through it works fine but if I try
| and execute the same WMI Query again I get an error:
|
| An unhandled exception of
| type 'System.Management.ManagementException' occurred in
| system.management.dll
|
| Additional information: COM object that has been separated
| from its underlying RCW can not be used.
|
|
| The code to access the WMI consists of:
|
| ConnectionOptions opts = new ConnectionOptions();
| ManagementScope scope = new
| ManagementScope(@"\\.\root\cimv2",opts);
| scope.Connect();
| //Use WMI to get lists of all
| logical drives and their type
| ManagementObjectSearcher wmisearch
| = new ManagementObjectSearcher(scope,new ObjectQuery
| ("select Name, DriveType, MediaType,ProviderName from
| Win32_LogicalDisk"));
| ManagementObjectCollection results
| = wmisearch.Get();
| MessageBox.Show(results.Count.ToString())
|
| As I say - first time through all works OK, then a refresh
| button on the explorer app runs the method containing the
| above again, then I get the exception.
|
| Thanks in advance Pete.
|
 
J

Jeffrey Tan[MSFT]

Hi Pete,

Thanks for your information of can not access from IE. I will report this
issue.
I suggest you use Outlook Express to visit our group.
For more information, please visit:
http://support.microsoft.com/default.aspx?scid=/support/news/howto/default.a
sp&SD=MSDN

For your problem, I do not quite understand your "simple explorer type user
control", does it contain the code that use the WMI?
I think you should paste more code to us, so that we can see where the
problem is.
If you did not use the count property, I think your problem did not relate
to the known issue.

I think you should trace into your code to find which sentence generates
this exception.

As a workaround, you can try to get the disk information like this:
ManagementClass mc = new ManagementClass("Win32_LogicalDisk");
ManagementObjectCollection mco = mc.GetInstances();
foreach (ManagementObject mo in mco)
{
if(mo["DriveType"].ToString().Equals("3"))
{
MessageBox.Show( mo["FreeSpace"].ToString());
}
}

Also, you can try to use FileSystemObject to get the disk information. This
may work around the problem.
To use FileSystemObject, you should add "Microsoft Scripting Runtime"
reference from the COM tabpage.
Then you can get information something like this:
FileSystemObject fso=new FileSystemObjectClass();
Drive disk=fso.GetDrive("D:");
MessageBox.Show(disk.FreeSpace.ToString());

If these still can not resolve your problem, please feel free to let me
know. But please provde me more information feedback.

Thanks

Best regards,
Jeffrey Tan
Microsoft Online Partner Support
Get Secure! - www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.

--------------------
| From: (e-mail address removed)-online.co.uk (trinitypete)
| Newsgroups: microsoft.public.dotnet.framework
| Subject: Re: WMI
| Date: 13 Oct 2003 05:34:29 -0700
| Organization: http://groups.google.com
| Lines: 128
| Message-ID: <[email protected]>
| References: <[email protected]>
<[email protected]>
| NNTP-Posting-Host: 217.45.203.57
| Content-Type: text/plain; charset=ISO-8859-1
| Content-Transfer-Encoding: 8bit
| X-Trace: posting.google.com 1066048470 2318 127.0.0.1 (13 Oct 2003
12:34:30 GMT)
| X-Complaints-To: (e-mail address removed)
| NNTP-Posting-Date: Mon, 13 Oct 2003 12:34:30 +0000 (UTC)
| Path:
cpmsftngxa06.phx.gbl!TK2MSFTNGP08.phx.gbl!newsfeed00.sul.t-online.de!t-onlin
e.de!news-spur1.maxwell.syr.edu!news.maxwell.syr.edu!postnews1.google.com!no
t-for-mail
| Xref: cpmsftngxa06.phx.gbl microsoft.public.dotnet.framework:56003
| X-Tomcat-NG: microsoft.public.dotnet.framework
|
| Jeffrey,
|
| Thanks for the reply, for some reason when I try and return to the
| newsgroups via http://msdn.microsoft.com/newsgroups/managed/, quite a
| few of the newsgroups display newsgroup unavailable, yet I can see
| them through google groups? Any idea why?
|
| BTW as I cant see the Newsgroups can you mail me at:
|
| (e-mail address removed) - obviously removing the
| 'dontspam'
|
| Anyhow getting back to the WMI problem, If I put the code in a button
| it does seem to work correctly. I have a simple explorer type user
| control where it consistantly gives this error, yet I do not reference
| the count property of the managementobjectcollection. May I send you a
| code simple example user control and wrapper?
|
| If so do I just drop the 'online' from the EM address?
|
| Thanks for your time - Pete.
|
| (e-mail address removed) (Jeffrey Tan[MSFT]) wrote in message
| > Hi Pete,
| >
| > I think just this code snippet will not generate problem(At least on my
| > machine).
| > You can setup a WinForm, in the Button's click event paste this code
| > snippet, how many times you click the button, it will not throw
exception.
| >
| > ConnectionOptions opts = new ConnectionOptions();
| > ManagementScope scope = new ManagementScope(@"\\.\root\cimv2",opts);
| > scope.Connect();
| > ManagementObjectSearcher wmisearch = new
ManagementObjectSearcher(scope,new
| > ObjectQuery("select Name, DriveType, MediaType,ProviderName from
| > Win32_LogicalDisk"));
| > ManagementObjectCollection results = wmisearch.Get();
| > MessageBox.Show(results.Count.ToString());
| >
| > But there is a known issue that if you Enumerate the
| > ManagementObjectCollection after calling the Count property.
| >
| > Such like this:
| > ConnectionOptions opts = new ConnectionOptions();
| > ManagementScope scope = new ManagementScope(@"\\.\root\cimv2",opts);
| > scope.Connect();
| > //Use WMI to get lists of all logical drives and their type
| > ManagementObjectSearcher wmisearch = new
ManagementObjectSearcher(scope,new
| > ObjectQuery("select Name, DriveType, MediaType,ProviderName from
| > Win32_LogicalDisk"));
| > ManagementObjectCollection results = wmisearch.Get();
| > MessageBox.Show(results.Count.ToString());
| >
| > foreach(ManagementObject mo in results)
| > {
| > MessageBox.Show(mo["Name"].ToString());
| > }
| >
| > I think you may be this problem.
| > If it is, you should call the count property after Enumerating the
| > ManagementObjectCollection.
| >
| > Hope this helps,
| >
| > Best regards,
| > Jeffrey Tan
| > Microsoft Online Partner Support
| > Get Secure! - www.microsoft.com/security
| > This posting is provided "as is" with no warranties and confers no
rights.
| >
| > --------------------
| > | Content-Class: urn:content-classes:message
| > | From: "trinitypete" <[email protected]>
| > | Sender: "trinitypete" <[email protected]>
| > | Subject: WMI
| > | Date: Fri, 10 Oct 2003 09:06:15 -0700
| > | Lines: 36
| > | Message-ID: <[email protected]>
| > | MIME-Version: 1.0
| > | Content-Type: text/plain;
| > | charset="iso-8859-1"
| > | Content-Transfer-Encoding: 7bit
| > | X-Newsreader: Microsoft CDO for Windows 2000
| > | X-MimeOLE: Produced By Microsoft MimeOLE V5.50.4910.0300
| > | Thread-Index: AcOPSG6xJD4XDBCQTniUaGEaux1O1g==
| > | Newsgroups: microsoft.public.dotnet.framework
| > | Path: cpmsftngxa06.phx.gbl
| > | Xref: cpmsftngxa06.phx.gbl microsoft.public.dotnet.framework:55892
| > | NNTP-Posting-Host: TK2MSFTNGXA14 10.40.1.166
| > | X-Tomcat-NG: microsoft.public.dotnet.framework
| > |
| > | Hi All,
| > |
| > | I have an explorer type app that I am having a little
| > | trouble with. I use WMI to obtain a list of drives on the
| > | system. The first time through it works fine but if I try
| > | and execute the same WMI Query again I get an error:
| > |
| > | An unhandled exception of
| > | type 'System.Management.ManagementException' occurred in
| > | system.management.dll
| > |
| > | Additional information: COM object that has been separated
| > | from its underlying RCW can not be used.
| > |
| > |
| > | The code to access the WMI consists of:
| > |
| > | ConnectionOptions opts = new ConnectionOptions();
| > | ManagementScope scope = new
| > | ManagementScope(@"\\.\root\cimv2",opts);
| > | scope.Connect();
| > | //Use WMI to get lists of all
| > | logical drives and their type
| > | ManagementObjectSearcher wmisearch
| > | = new ManagementObjectSearcher(scope,new ObjectQuery
| > | ("select Name, DriveType, MediaType,ProviderName from
| > | Win32_LogicalDisk"));
| > | ManagementObjectCollection results
| > | = wmisearch.Get();
| > | MessageBox.Show(results.Count.ToString())
| > |
| > | As I say - first time through all works OK, then a refresh
| > | button on the explorer app runs the method containing the
| > | above again, then I get the exception.
| > |
| > | Thanks in advance Pete.
| > |
|
 
J

Jeffrey Tan[MSFT]

Hi ,

After you add the reference of FileSystemObject, you can use object
"browser window" to watch the members of this assembly.

Jeffrey Tan
Microsoft Online Partner Support
Get Secure! - www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.

--------------------
| Newsgroups: microsoft.public.dotnet.framework
| From: (e-mail address removed) ("Jeffrey Tan[MSFT]")
| Organization: Microsoft
| Date: Tue, 14 Oct 2003 03:38:32 GMT
| Subject: Re: WMI
| X-Tomcat-NG: microsoft.public.dotnet.framework
| MIME-Version: 1.0
| Content-Type: text/plain
| Content-Transfer-Encoding: 7bit
|
|
| Hi Pete,
|
| Thanks for your information of can not access from IE. I will report this
| issue.
| I suggest you use Outlook Express to visit our group.
| For more information, please visit:
|
http://support.microsoft.com/default.aspx?scid=/support/news/howto/default.a
| sp&SD=MSDN
|
| For your problem, I do not quite understand your "simple explorer type
user
| control", does it contain the code that use the WMI?
| I think you should paste more code to us, so that we can see where the
| problem is.
| If you did not use the count property, I think your problem did not
relate
| to the known issue.
|
| I think you should trace into your code to find which sentence generates
| this exception.
|
| As a workaround, you can try to get the disk information like this:
| ManagementClass mc = new ManagementClass("Win32_LogicalDisk");
| ManagementObjectCollection mco = mc.GetInstances();
| foreach (ManagementObject mo in mco)
| {
| if(mo["DriveType"].ToString().Equals("3"))
| {
| MessageBox.Show( mo["FreeSpace"].ToString());
| }
| }
|
| Also, you can try to use FileSystemObject to get the disk information.
This
| may work around the problem.
| To use FileSystemObject, you should add "Microsoft Scripting Runtime"
| reference from the COM tabpage.
| Then you can get information something like this:
| FileSystemObject fso=new FileSystemObjectClass();
| Drive disk=fso.GetDrive("D:");
| MessageBox.Show(disk.FreeSpace.ToString());
|
| If these still can not resolve your problem, please feel free to let me
| know. But please provde me more information feedback.
|
| Thanks
|
| Best regards,
| Jeffrey Tan
| Microsoft Online Partner Support
| Get Secure! - www.microsoft.com/security
| This posting is provided "as is" with no warranties and confers no rights.
|
| --------------------
| | From: (e-mail address removed)-online.co.uk (trinitypete)
| | Newsgroups: microsoft.public.dotnet.framework
| | Subject: Re: WMI
| | Date: 13 Oct 2003 05:34:29 -0700
| | Organization: http://groups.google.com
| | Lines: 128
| | Message-ID: <[email protected]>
| | References: <[email protected]>
| <[email protected]>
| | NNTP-Posting-Host: 217.45.203.57
| | Content-Type: text/plain; charset=ISO-8859-1
| | Content-Transfer-Encoding: 8bit
| | X-Trace: posting.google.com 1066048470 2318 127.0.0.1 (13 Oct 2003
| 12:34:30 GMT)
| | X-Complaints-To: (e-mail address removed)
| | NNTP-Posting-Date: Mon, 13 Oct 2003 12:34:30 +0000 (UTC)
| | Path:
|
cpmsftngxa06.phx.gbl!TK2MSFTNGP08.phx.gbl!newsfeed00.sul.t-online.de!t-onlin
|
e.de!news-spur1.maxwell.syr.edu!news.maxwell.syr.edu!postnews1.google.com!no
| t-for-mail
| | Xref: cpmsftngxa06.phx.gbl microsoft.public.dotnet.framework:56003
| | X-Tomcat-NG: microsoft.public.dotnet.framework
| |
| | Jeffrey,
| |
| | Thanks for the reply, for some reason when I try and return to the
| | newsgroups via http://msdn.microsoft.com/newsgroups/managed/, quite a
| | few of the newsgroups display newsgroup unavailable, yet I can see
| | them through google groups? Any idea why?
| |
| | BTW as I cant see the Newsgroups can you mail me at:
| |
| | (e-mail address removed) - obviously removing the
| | 'dontspam'
| |
| | Anyhow getting back to the WMI problem, If I put the code in a button
| | it does seem to work correctly. I have a simple explorer type user
| | control where it consistantly gives this error, yet I do not reference
| | the count property of the managementobjectcollection. May I send you a
| | code simple example user control and wrapper?
| |
| | If so do I just drop the 'online' from the EM address?
| |
| | Thanks for your time - Pete.
| |
| | (e-mail address removed) (Jeffrey Tan[MSFT]) wrote in message
| | | > Hi Pete,
| | >
| | > I think just this code snippet will not generate problem(At least on
my
| | > machine).
| | > You can setup a WinForm, in the Button's click event paste this code
| | > snippet, how many times you click the button, it will not throw
| exception.
| | >
| | > ConnectionOptions opts = new ConnectionOptions();
| | > ManagementScope scope = new ManagementScope(@"\\.\root\cimv2",opts);
| | > scope.Connect();
| | > ManagementObjectSearcher wmisearch = new
| ManagementObjectSearcher(scope,new
| | > ObjectQuery("select Name, DriveType, MediaType,ProviderName from
| | > Win32_LogicalDisk"));
| | > ManagementObjectCollection results = wmisearch.Get();
| | > MessageBox.Show(results.Count.ToString());
| | >
| | > But there is a known issue that if you Enumerate the
| | > ManagementObjectCollection after calling the Count property.
| | >
| | > Such like this:
| | > ConnectionOptions opts = new ConnectionOptions();
| | > ManagementScope scope = new ManagementScope(@"\\.\root\cimv2",opts);
| | > scope.Connect();
| | > //Use WMI to get lists of all logical drives and their type
| | > ManagementObjectSearcher wmisearch = new
| ManagementObjectSearcher(scope,new
| | > ObjectQuery("select Name, DriveType, MediaType,ProviderName from
| | > Win32_LogicalDisk"));
| | > ManagementObjectCollection results = wmisearch.Get();
| | > MessageBox.Show(results.Count.ToString());
| | >
| | > foreach(ManagementObject mo in results)
| | > {
| | > MessageBox.Show(mo["Name"].ToString());
| | > }
| | >
| | > I think you may be this problem.
| | > If it is, you should call the count property after Enumerating the
| | > ManagementObjectCollection.
| | >
| | > Hope this helps,
| | >
| | > Best regards,
| | > Jeffrey Tan
| | > Microsoft Online Partner Support
| | > Get Secure! - www.microsoft.com/security
| | > This posting is provided "as is" with no warranties and confers no
| rights.
| | >
| | > --------------------
| | > | Content-Class: urn:content-classes:message
| | > | From: "trinitypete" <[email protected]>
| | > | Sender: "trinitypete" <[email protected]>
| | > | Subject: WMI
| | > | Date: Fri, 10 Oct 2003 09:06:15 -0700
| | > | Lines: 36
| | > | Message-ID: <[email protected]>
| | > | MIME-Version: 1.0
| | > | Content-Type: text/plain;
| | > | charset="iso-8859-1"
| | > | Content-Transfer-Encoding: 7bit
| | > | X-Newsreader: Microsoft CDO for Windows 2000
| | > | X-MimeOLE: Produced By Microsoft MimeOLE V5.50.4910.0300
| | > | Thread-Index: AcOPSG6xJD4XDBCQTniUaGEaux1O1g==
| | > | Newsgroups: microsoft.public.dotnet.framework
| | > | Path: cpmsftngxa06.phx.gbl
| | > | Xref: cpmsftngxa06.phx.gbl microsoft.public.dotnet.framework:55892
| | > | NNTP-Posting-Host: TK2MSFTNGXA14 10.40.1.166
| | > | X-Tomcat-NG: microsoft.public.dotnet.framework
| | > |
| | > | Hi All,
| | > |
| | > | I have an explorer type app that I am having a little
| | > | trouble with. I use WMI to obtain a list of drives on the
| | > | system. The first time through it works fine but if I try
| | > | and execute the same WMI Query again I get an error:
| | > |
| | > | An unhandled exception of
| | > | type 'System.Management.ManagementException' occurred in
| | > | system.management.dll
| | > |
| | > | Additional information: COM object that has been separated
| | > | from its underlying RCW can not be used.
| | > |
| | > |
| | > | The code to access the WMI consists of:
| | > |
| | > | ConnectionOptions opts = new ConnectionOptions();
| | > | ManagementScope scope = new
| | > | ManagementScope(@"\\.\root\cimv2",opts);
| | > | scope.Connect();
| | > | //Use WMI to get lists of all
| | > | logical drives and their type
| | > | ManagementObjectSearcher wmisearch
| | > | = new ManagementObjectSearcher(scope,new ObjectQuery
| | > | ("select Name, DriveType, MediaType,ProviderName from
| | > | Win32_LogicalDisk"));
| | > | ManagementObjectCollection results
| | > | = wmisearch.Get();
| | > | MessageBox.Show(results.Count.ToString())
| | > |
| | > | As I say - first time through all works OK, then a refresh
| | > | button on the explorer app runs the method containing the
| | > | above again, then I get the exception.
| | > |
| | > | Thanks in advance Pete.
| | > |
| |
|
 
M

MSDN Managed

Jeffrey,

Thanks for the reply. I have now got access to the newsgroups via Outlook
Express. Thank God (oh and of course you). I was lost without Managed
Newsgroups.

I have resolved the problem but don't quite understand the issue. Here is
what was happening.

1. Method to Obtain ManamentCollection information
2. Enumerate collection extracting various information.
3. Do some processing
4. Enumerate the collection again.
5. Dispose of any disposable objects.

The whole thing works fine the first time through but when the process is
executed again, any access to the collection after the Get() is issued
results in the exception.

In my work around (which works fine), when I first enumerate the collection,
I move the required information into an array. At step 4, I use the array.
Subsequent calls to the process now work fine?

Once again thanks for your time - Pete.


"Jeffrey Tan[MSFT]" said:
Hi ,

After you add the reference of FileSystemObject, you can use object
"browser window" to watch the members of this assembly.

Jeffrey Tan
Microsoft Online Partner Support
Get Secure! - www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.

--------------------
| Newsgroups: microsoft.public.dotnet.framework
| From: (e-mail address removed) ("Jeffrey Tan[MSFT]")
| Organization: Microsoft
| Date: Tue, 14 Oct 2003 03:38:32 GMT
| Subject: Re: WMI
| X-Tomcat-NG: microsoft.public.dotnet.framework
| MIME-Version: 1.0
| Content-Type: text/plain
| Content-Transfer-Encoding: 7bit
|
|
| Hi Pete,
|
| Thanks for your information of can not access from IE. I will report this
| issue.
| I suggest you use Outlook Express to visit our group.
| For more information, please visit:
|
http://support.microsoft.com/default.aspx?scid=/support/news/howto/default.a
| sp&SD=MSDN
|
| For your problem, I do not quite understand your "simple explorer type
user
| control", does it contain the code that use the WMI?
| I think you should paste more code to us, so that we can see where the
| problem is.
| If you did not use the count property, I think your problem did not
relate
| to the known issue.
|
| I think you should trace into your code to find which sentence generates
| this exception.
|
| As a workaround, you can try to get the disk information like this:
| ManagementClass mc = new ManagementClass("Win32_LogicalDisk");
| ManagementObjectCollection mco = mc.GetInstances();
| foreach (ManagementObject mo in mco)
| {
| if(mo["DriveType"].ToString().Equals("3"))
| {
| MessageBox.Show( mo["FreeSpace"].ToString());
| }
| }
|
| Also, you can try to use FileSystemObject to get the disk information.
This
| may work around the problem.
| To use FileSystemObject, you should add "Microsoft Scripting Runtime"
| reference from the COM tabpage.
| Then you can get information something like this:
| FileSystemObject fso=new FileSystemObjectClass();
| Drive disk=fso.GetDrive("D:");
| MessageBox.Show(disk.FreeSpace.ToString());
|
| If these still can not resolve your problem, please feel free to let me
| know. But please provde me more information feedback.
|
| Thanks
|
| Best regards,
| Jeffrey Tan
| Microsoft Online Partner Support
| Get Secure! - www.microsoft.com/security
| This posting is provided "as is" with no warranties and confers no rights.
|
| --------------------
| | From: (e-mail address removed)-online.co.uk (trinitypete)
| | Newsgroups: microsoft.public.dotnet.framework
| | Subject: Re: WMI
| | Date: 13 Oct 2003 05:34:29 -0700
| | Organization: http://groups.google.com
| | Lines: 128
| | Message-ID: <[email protected]>
| | References: <[email protected]>
| <[email protected]>
| | NNTP-Posting-Host: 217.45.203.57
| | Content-Type: text/plain; charset=ISO-8859-1
| | Content-Transfer-Encoding: 8bit
| | X-Trace: posting.google.com 1066048470 2318 127.0.0.1 (13 Oct 2003
| 12:34:30 GMT)
| | X-Complaints-To: (e-mail address removed)
| | NNTP-Posting-Date: Mon, 13 Oct 2003 12:34:30 +0000 (UTC)
| | Path:
|
cpmsftngxa06.phx.gbl!TK2MSFTNGP08.phx.gbl!newsfeed00.sul.t-online.de!t-onlin
e.de!news-spur1.maxwell.syr.edu!news.maxwell.syr.edu!postnews1.google.com!no
| t-for-mail
| | Xref: cpmsftngxa06.phx.gbl microsoft.public.dotnet.framework:56003
| | X-Tomcat-NG: microsoft.public.dotnet.framework
| |
| | Jeffrey,
| |
| | Thanks for the reply, for some reason when I try and return to the
| | newsgroups via http://msdn.microsoft.com/newsgroups/managed/, quite a
| | few of the newsgroups display newsgroup unavailable, yet I can see
| | them through google groups? Any idea why?
| |
| | BTW as I cant see the Newsgroups can you mail me at:
| |
| | (e-mail address removed) - obviously removing the
| | 'dontspam'
| |
| | Anyhow getting back to the WMI problem, If I put the code in a button
| | it does seem to work correctly. I have a simple explorer type user
| | control where it consistantly gives this error, yet I do not reference
| | the count property of the managementobjectcollection. May I send you a
| | code simple example user control and wrapper?
| |
| | If so do I just drop the 'online' from the EM address?
| |
| | Thanks for your time - Pete.
| |
| | (e-mail address removed) (Jeffrey Tan[MSFT]) wrote in message
| | | > Hi Pete,
| | >
| | > I think just this code snippet will not generate problem(At least on
my
| | > machine).
| | > You can setup a WinForm, in the Button's click event paste this code
| | > snippet, how many times you click the button, it will not throw
| exception.
| | >
| | > ConnectionOptions opts = new ConnectionOptions();
| | > ManagementScope scope = new ManagementScope(@"\\.\root\cimv2",opts);
| | > scope.Connect();
| | > ManagementObjectSearcher wmisearch = new
| ManagementObjectSearcher(scope,new
| | > ObjectQuery("select Name, DriveType, MediaType,ProviderName from
| | > Win32_LogicalDisk"));
| | > ManagementObjectCollection results = wmisearch.Get();
| | > MessageBox.Show(results.Count.ToString());
| | >
| | > But there is a known issue that if you Enumerate the
| | > ManagementObjectCollection after calling the Count property.
| | >
| | > Such like this:
| | > ConnectionOptions opts = new ConnectionOptions();
| | > ManagementScope scope = new ManagementScope(@"\\.\root\cimv2",opts);
| | > scope.Connect();
| | > //Use WMI to get lists of all logical drives and their type
| | > ManagementObjectSearcher wmisearch = new
| ManagementObjectSearcher(scope,new
| | > ObjectQuery("select Name, DriveType, MediaType,ProviderName from
| | > Win32_LogicalDisk"));
| | > ManagementObjectCollection results = wmisearch.Get();
| | > MessageBox.Show(results.Count.ToString());
| | >
| | > foreach(ManagementObject mo in results)
| | > {
| | > MessageBox.Show(mo["Name"].ToString());
| | > }
| | >
| | > I think you may be this problem.
| | > If it is, you should call the count property after Enumerating the
| | > ManagementObjectCollection.
| | >
| | > Hope this helps,
| | >
| | > Best regards,
| | > Jeffrey Tan
| | > Microsoft Online Partner Support
| | > Get Secure! - www.microsoft.com/security
| | > This posting is provided "as is" with no warranties and confers no
| rights.
| | >
| | > --------------------
| | > | Content-Class: urn:content-classes:message
| | > | From: "trinitypete" <[email protected]>
| | > | Sender: "trinitypete" <[email protected]>
| | > | Subject: WMI
| | > | Date: Fri, 10 Oct 2003 09:06:15 -0700
| | > | Lines: 36
| | > | Message-ID: <[email protected]>
| | > | MIME-Version: 1.0
| | > | Content-Type: text/plain;
| | > | charset="iso-8859-1"
| | > | Content-Transfer-Encoding: 7bit
| | > | X-Newsreader: Microsoft CDO for Windows 2000
| | > | X-MimeOLE: Produced By Microsoft MimeOLE V5.50.4910.0300
| | > | Thread-Index: AcOPSG6xJD4XDBCQTniUaGEaux1O1g==
| | > | Newsgroups: microsoft.public.dotnet.framework
| | > | Path: cpmsftngxa06.phx.gbl
| | > | Xref: cpmsftngxa06.phx.gbl microsoft.public.dotnet.framework:55892
| | > | NNTP-Posting-Host: TK2MSFTNGXA14 10.40.1.166
| | > | X-Tomcat-NG: microsoft.public.dotnet.framework
| | > |
| | > | Hi All,
| | > |
| | > | I have an explorer type app that I am having a little
| | > | trouble with. I use WMI to obtain a list of drives on the
| | > | system. The first time through it works fine but if I try
| | > | and execute the same WMI Query again I get an error:
| | > |
| | > | An unhandled exception of
| | > | type 'System.Management.ManagementException' occurred in
| | > | system.management.dll
| | > |
| | > | Additional information: COM object that has been separated
| | > | from its underlying RCW can not be used.
| | > |
| | > |
| | > | The code to access the WMI consists of:
| | > |
| | > | ConnectionOptions opts = new ConnectionOptions();
| | > | ManagementScope scope = new
| | > | ManagementScope(@"\\.\root\cimv2",opts);
| | > | scope.Connect();
| | > | //Use WMI to get lists of all
| | > | logical drives and their type
| | > | ManagementObjectSearcher wmisearch
| | > | = new ManagementObjectSearcher(scope,new ObjectQuery
| | > | ("select Name, DriveType, MediaType,ProviderName from
| | > | Win32_LogicalDisk"));
| | > | ManagementObjectCollection results
| | > | = wmisearch.Get();
| | > | MessageBox.Show(results.Count.ToString())
| | > |
| | > | As I say - first time through all works OK, then a refresh
| | > | button on the explorer app runs the method containing the
| | > | above again, then I get the exception.
| | > |
| | > | Thanks in advance Pete.
| | > |
| |
|
 
J

Jeffrey Tan[MSFT]

Hi ,

I am glad that you finally work around your problem.
I think the problem is ManagementObjectCollection implements IDisposable
interface, and the foreach statement will always call the Dispose method on
the enumerator when the foreach loop is terminated.
Once the ManagementObjectCollection object is disposed, the binding to the
underlying WMI objects is lost.

You also can try to enumerate the collection using
ManagementObjectCollection.ManagementObjectEnumerator instead of using
foreach.
You'd use foreach statement only if you don't need to access the
ManagementObjectCollection object after the statement.

If you still have any questions, please feel free to let me know. I am glad
to work with you.
Have a nice day.

Best regards,
Jeffrey Tan
Microsoft Online Partner Support
Get Secure! - www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.

--------------------
| From: "MSDN Managed" <[email protected]>
| References: <[email protected]>
<[email protected]>
<[email protected]>
<[email protected]>
<[email protected]>
| Subject: Re: WMI
| Date: Tue, 14 Oct 2003 11:08:34 +0100
| Lines: 275
| X-Priority: 3
| X-MSMail-Priority: Normal
| X-Newsreader: Microsoft Outlook Express 6.00.2800.1158
| X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2800.1165
| Message-ID: <[email protected]>
| Newsgroups: microsoft.public.dotnet.framework
| NNTP-Posting-Host: host217-45-203-57.in-addr.btopenworld.com 217.45.203.57
| Path: cpmsftngxa06.phx.gbl!TK2MSFTNGP08.phx.gbl!tk2msftngp13.phx.gbl
| Xref: cpmsftngxa06.phx.gbl microsoft.public.dotnet.framework:56089
| X-Tomcat-NG: microsoft.public.dotnet.framework
|
| Jeffrey,
|
| Thanks for the reply. I have now got access to the newsgroups via Outlook
| Express. Thank God (oh and of course you). I was lost without Managed
| Newsgroups.
|
| I have resolved the problem but don't quite understand the issue. Here is
| what was happening.
|
| 1. Method to Obtain ManamentCollection information
| 2. Enumerate collection extracting various information.
| 3. Do some processing
| 4. Enumerate the collection again.
| 5. Dispose of any disposable objects.
|
| The whole thing works fine the first time through but when the process is
| executed again, any access to the collection after the Get() is issued
| results in the exception.
|
| In my work around (which works fine), when I first enumerate the
collection,
| I move the required information into an array. At step 4, I use the array.
| Subsequent calls to the process now work fine?
|
| Once again thanks for your time - Pete.
|
|
| | >
| > Hi ,
| >
| > After you add the reference of FileSystemObject, you can use object
| > "browser window" to watch the members of this assembly.
| >
| > Jeffrey Tan
| > Microsoft Online Partner Support
| > Get Secure! - www.microsoft.com/security
| > This posting is provided "as is" with no warranties and confers no
rights.
| >
| > --------------------
| > | Newsgroups: microsoft.public.dotnet.framework
| > | From: (e-mail address removed) ("Jeffrey Tan[MSFT]")
| > | Organization: Microsoft
| > | Date: Tue, 14 Oct 2003 03:38:32 GMT
| > | Subject: Re: WMI
| > | X-Tomcat-NG: microsoft.public.dotnet.framework
| > | MIME-Version: 1.0
| > | Content-Type: text/plain
| > | Content-Transfer-Encoding: 7bit
| > |
| > |
| > | Hi Pete,
| > |
| > | Thanks for your information of can not access from IE. I will report
| this
| > | issue.
| > | I suggest you use Outlook Express to visit our group.
| > | For more information, please visit:
| > |
| >
|
http://support.microsoft.com/default.aspx?scid=/support/news/howto/default.a
| > | sp&SD=MSDN
| > |
| > | For your problem, I do not quite understand your "simple explorer type
| > user
| > | control", does it contain the code that use the WMI?
| > | I think you should paste more code to us, so that we can see where the
| > | problem is.
| > | If you did not use the count property, I think your problem did not
| > relate
| > | to the known issue.
| > |
| > | I think you should trace into your code to find which sentence
generates
| > | this exception.
| > |
| > | As a workaround, you can try to get the disk information like this:
| > | ManagementClass mc = new ManagementClass("Win32_LogicalDisk");
| > | ManagementObjectCollection mco = mc.GetInstances();
| > | foreach (ManagementObject mo in mco)
| > | {
| > | if(mo["DriveType"].ToString().Equals("3"))
| > | {
| > | MessageBox.Show( mo["FreeSpace"].ToString());
| > | }
| > | }
| > |
| > | Also, you can try to use FileSystemObject to get the disk information.
| > This
| > | may work around the problem.
| > | To use FileSystemObject, you should add "Microsoft Scripting Runtime"
| > | reference from the COM tabpage.
| > | Then you can get information something like this:
| > | FileSystemObject fso=new FileSystemObjectClass();
| > | Drive disk=fso.GetDrive("D:");
| > | MessageBox.Show(disk.FreeSpace.ToString());
| > |
| > | If these still can not resolve your problem, please feel free to let
me
| > | know. But please provde me more information feedback.
| > |
| > | Thanks
| > |
| > | Best regards,
| > | Jeffrey Tan
| > | Microsoft Online Partner Support
| > | Get Secure! - www.microsoft.com/security
| > | This posting is provided "as is" with no warranties and confers no
| rights.
| > |
| > | --------------------
| > | | From: (e-mail address removed)-online.co.uk (trinitypete)
| > | | Newsgroups: microsoft.public.dotnet.framework
| > | | Subject: Re: WMI
| > | | Date: 13 Oct 2003 05:34:29 -0700
| > | | Organization: http://groups.google.com
| > | | Lines: 128
| > | | Message-ID: <[email protected]>
| > | | References: <[email protected]>
| > | <[email protected]>
| > | | NNTP-Posting-Host: 217.45.203.57
| > | | Content-Type: text/plain; charset=ISO-8859-1
| > | | Content-Transfer-Encoding: 8bit
| > | | X-Trace: posting.google.com 1066048470 2318 127.0.0.1 (13 Oct 2003
| > | 12:34:30 GMT)
| > | | X-Complaints-To: (e-mail address removed)
| > | | NNTP-Posting-Date: Mon, 13 Oct 2003 12:34:30 +0000 (UTC)
| > | | Path:
| > |
| >
|
cpmsftngxa06.phx.gbl!TK2MSFTNGP08.phx.gbl!newsfeed00.sul.t-online.de!t-onlin
| > |
| >
|
e.de!news-spur1.maxwell.syr.edu!news.maxwell.syr.edu!postnews1.google.com!no
| > | t-for-mail
| > | | Xref: cpmsftngxa06.phx.gbl microsoft.public.dotnet.framework:56003
| > | | X-Tomcat-NG: microsoft.public.dotnet.framework
| > | |
| > | | Jeffrey,
| > | |
| > | | Thanks for the reply, for some reason when I try and return to the
| > | | newsgroups via http://msdn.microsoft.com/newsgroups/managed/, quite
a
| > | | few of the newsgroups display newsgroup unavailable, yet I can see
| > | | them through google groups? Any idea why?
| > | |
| > | | BTW as I cant see the Newsgroups can you mail me at:
| > | |
| > | | (e-mail address removed) - obviously removing the
| > | | 'dontspam'
| > | |
| > | | Anyhow getting back to the WMI problem, If I put the code in a
button
| > | | it does seem to work correctly. I have a simple explorer type user
| > | | control where it consistantly gives this error, yet I do not
reference
| > | | the count property of the managementobjectcollection. May I send
you a
| > | | code simple example user control and wrapper?
| > | |
| > | | If so do I just drop the 'online' from the EM address?
| > | |
| > | | Thanks for your time - Pete.
| > | |
| > | | (e-mail address removed) (Jeffrey Tan[MSFT]) wrote in message
| > | | > | | > Hi Pete,
| > | | >
| > | | > I think just this code snippet will not generate problem(At least
on
| > my
| > | | > machine).
| > | | > You can setup a WinForm, in the Button's click event paste this
code
| > | | > snippet, how many times you click the button, it will not throw
| > | exception.
| > | | >
| > | | > ConnectionOptions opts = new ConnectionOptions();
| > | | > ManagementScope scope = new
ManagementScope(@"\\.\root\cimv2",opts);
| > | | > scope.Connect();
| > | | > ManagementObjectSearcher wmisearch = new
| > | ManagementObjectSearcher(scope,new
| > | | > ObjectQuery("select Name, DriveType, MediaType,ProviderName from
| > | | > Win32_LogicalDisk"));
| > | | > ManagementObjectCollection results = wmisearch.Get();
| > | | > MessageBox.Show(results.Count.ToString());
| > | | >
| > | | > But there is a known issue that if you Enumerate the
| > | | > ManagementObjectCollection after calling the Count property.
| > | | >
| > | | > Such like this:
| > | | > ConnectionOptions opts = new ConnectionOptions();
| > | | > ManagementScope scope = new
ManagementScope(@"\\.\root\cimv2",opts);
| > | | > scope.Connect();
| > | | > //Use WMI to get lists of all logical drives and their type
| > | | > ManagementObjectSearcher wmisearch = new
| > | ManagementObjectSearcher(scope,new
| > | | > ObjectQuery("select Name, DriveType, MediaType,ProviderName from
| > | | > Win32_LogicalDisk"));
| > | | > ManagementObjectCollection results = wmisearch.Get();
| > | | > MessageBox.Show(results.Count.ToString());
| > | | >
| > | | > foreach(ManagementObject mo in results)
| > | | > {
| > | | > MessageBox.Show(mo["Name"].ToString());
| > | | > }
| > | | >
| > | | > I think you may be this problem.
| > | | > If it is, you should call the count property after Enumerating the
| > | | > ManagementObjectCollection.
| > | | >
| > | | > Hope this helps,
| > | | >
| > | | > Best regards,
| > | | > Jeffrey Tan
| > | | > Microsoft Online Partner Support
| > | | > Get Secure! - www.microsoft.com/security
| > | | > This posting is provided "as is" with no warranties and confers no
| > | rights.
| > | | >
| > | | > --------------------
| > | | > | Content-Class: urn:content-classes:message
| > | | > | From: "trinitypete" <[email protected]>
| > | | > | Sender: "trinitypete" <[email protected]>
| > | | > | Subject: WMI
| > | | > | Date: Fri, 10 Oct 2003 09:06:15 -0700
| > | | > | Lines: 36
| > | | > | Message-ID: <[email protected]>
| > | | > | MIME-Version: 1.0
| > | | > | Content-Type: text/plain;
| > | | > | charset="iso-8859-1"
| > | | > | Content-Transfer-Encoding: 7bit
| > | | > | X-Newsreader: Microsoft CDO for Windows 2000
| > | | > | X-MimeOLE: Produced By Microsoft MimeOLE V5.50.4910.0300
| > | | > | Thread-Index: AcOPSG6xJD4XDBCQTniUaGEaux1O1g==
| > | | > | Newsgroups: microsoft.public.dotnet.framework
| > | | > | Path: cpmsftngxa06.phx.gbl
| > | | > | Xref: cpmsftngxa06.phx.gbl
microsoft.public.dotnet.framework:55892
| > | | > | NNTP-Posting-Host: TK2MSFTNGXA14 10.40.1.166
| > | | > | X-Tomcat-NG: microsoft.public.dotnet.framework
| > | | > |
| > | | > | Hi All,
| > | | > |
| > | | > | I have an explorer type app that I am having a little
| > | | > | trouble with. I use WMI to obtain a list of drives on the
| > | | > | system. The first time through it works fine but if I try
| > | | > | and execute the same WMI Query again I get an error:
| > | | > |
| > | | > | An unhandled exception of
| > | | > | type 'System.Management.ManagementException' occurred in
| > | | > | system.management.dll
| > | | > |
| > | | > | Additional information: COM object that has been separated
| > | | > | from its underlying RCW can not be used.
| > | | > |
| > | | > |
| > | | > | The code to access the WMI consists of:
| > | | > |
| > | | > | ConnectionOptions opts = new ConnectionOptions();
| > | | > | ManagementScope scope = new
| > | | > | ManagementScope(@"\\.\root\cimv2",opts);
| > | | > | scope.Connect();
| > | | > | //Use WMI to get lists of all
| > | | > | logical drives and their type
| > | | > | ManagementObjectSearcher wmisearch
| > | | > | = new ManagementObjectSearcher(scope,new ObjectQuery
| > | | > | ("select Name, DriveType, MediaType,ProviderName from
| > | | > | Win32_LogicalDisk"));
| > | | > | ManagementObjectCollection results
| > | | > | = wmisearch.Get();
| > | | > | MessageBox.Show(results.Count.ToString())
| > | | > |
| > | | > | As I say - first time through all works OK, then a refresh
| > | | > | button on the explorer app runs the method containing the
| > | | > | above again, then I get the exception.
| > | | > |
| > | | > | Thanks in advance Pete.
| > | | > |
| > | |
| > |
| >
|
|
|
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top