The Artima Developer Community
Sponsored Link

C# Answers Forum
Access Win32 const DRIVE_FIXED - How to?

8 replies on 1 page. Most recent reply: Mar 23, 2003 4:06 PM by Matt Gerrans

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 8 replies on 1 page
Richard Muller

Posts: 10
Nickname: rlmuller
Registered: Mar, 2003

Access Win32 const DRIVE_FIXED - How to? Posted: Mar 14, 2003 8:09 PM
Reply to this message Reply
Advertisement
Hi,

I downloaded a TreeView C# sample. It included the statement:

if (PlatformInvokeKernel32.GetDriveType(drives) ==
PlatformInvokeKernel32.DRIVE_FIXED) ...

I found a declaration for PlatformInvokeKernel32:

using System.Runtime.InteropServices;
internal class PlatformInvokeKernel32
{
[DllImport("KERNEL32")]
public static extern int GetDriveType(string s);
}

How do I declare PlatformInvokeKernel32.DRIVE_FIXED?

TIA,

Richard


Matt Gerrans

Posts: 1153
Nickname: matt
Registered: Feb, 2002

Re: Access Win32 const DRIVE_FIXED - How to? Posted: Mar 16, 2003 1:17 PM
Reply to this message Reply
Where did you download the sample from? It looks like the class is missing the definitions of the contants.

Anyway, these constants come from WinBase.h:

#define DRIVE_UNKNOWN 0
#define DRIVE_NO_ROOT_DIR 1
#define DRIVE_REMOVABLE 2
#define DRIVE_FIXED 3
#define DRIVE_REMOTE 4
#define DRIVE_CDROM 5
#define DRIVE_RAMDISK 6

For a project I'm working on, I wrote a Python script to create C# files from C++ header files in order to use the contants with COM objects. In this case, an enum could be used. The Python script I wrote just creates a sealed class with a const value for each #define from the C++ header, since I didn't have time to do the necessary grouping to create enums without conflicting values.

It is interesting that the System.IO.Directory class has a GetLogicalDrives() method, but no way of getting drive types (as far as I can see). I looked around a bit in the System.IO, but couldn't find anything for getting drive types, unfortunately...

Matt Gerrans

Posts: 1153
Nickname: matt
Registered: Feb, 2002

Re: Access Win32 const DRIVE_FIXED - How to? Posted: Mar 16, 2003 2:08 PM
Reply to this message Reply
You can use WMI to get the information, avoiding PlatformInvoke. I'm not sure it is that much better, since you still need to define those drive type contants and WMI is pretty slow, but at least you can avoid the unsafe code.

         ManagementClass manager = new ManagementClass("Win32_LogicalDisk");
         ManagementObjectCollection drives = manager.GetInstances();
         foreach( ManagementObject drive in drives )
         {            
            textInfo.Text += drive["Caption"] + " is a " + drive["Description"];
 
            // Do this to get *all* property names and values:
            //foreach( PropertyData prop in drive.Properties )
            //   textInfo.Text += prop.Name + " = " + drive[prop.Name] + " ";
 
            // Do this to get the drive type (you need constants or enums to compare to): 
            // drive["DriveType"];
 
            textInfo.Text += cr;
         }


I'm dumping the info to a multiline TextBox control called textInfo, by the way.

Matt Gerrans

Posts: 1153
Nickname: matt
Registered: Feb, 2002

Re: Access Win32 const DRIVE_FIXED - How to? Posted: Mar 16, 2003 2:11 PM
Reply to this message Reply
Oops, I forgot to include the definition of cr, which was string cr = "\r\n";. By the way, I think it is pretty ridiculous that Microsoft decided to require both carriage and linefeed in EditBoxes and other Form controls. Who in the heck would ever intentionally want to use "\n" alone to display a stupid square box in the middle of some text?!

Richard Muller

Posts: 10
Nickname: rlmuller
Registered: Mar, 2003

Re: Access Win32 const DRIVE_FIXED - How to? Posted: Mar 16, 2003 9:54 PM
Reply to this message Reply
I understood "cr". In response to your aside, I'd suggest that both \r and \n are needed, for example, if the target was an IBM Selectric Typewriter (do they still make them?)

More importantly, thanks for the alternate approach with ManagementClass ....

I made a trivial WinApp project to test it, adding a TextBox with the multiline option and pasting your text following the generated InitializeComponent(); stmt.

I got three error messages, e.g., The type or namespace name 'ManagementClass' could not be found.

I ran the command FindType ManagementClass in a Command window (FindType.exe is a sample C# project in VS.Net), which reported two hits:
class System.Management.ManagementClass
class System.Management.ManagementClassGenerator


I appended using System.Management; to the generated using statements.

That produced the error The type or namespace name 'Management' does not exist in the class or namespace 'System'

I tried a couple of other things. Sorry to bug you with such a seemingly simple question, but my 15+ years experience with C and C++ on several platforms doesn't seem to be of much use with these kind of question in a new language.

Regards,
Richard

Richard Muller

Posts: 10
Nickname: rlmuller
Registered: Mar, 2003

Re: Access Win32 const DRIVE_FIXED - How to? Posted: Mar 16, 2003 10:24 PM
Reply to this message Reply
I replied to your Mar 16, 2003 4:17 PM WinBase.h response, but I don't see it listed here, so I must have made some dumb mistake when posting it. If my original reply shows up, I apologize for this duplication.

First, thank for your response. I confirmed that winbase.h uniquely (among .h and .inl files) defines the constant in question, both in Visual Studion 6.0 and April 2002 Platform SDK.

BTW, I forgot where I found the sample, so I can't direct a question in that direction.

Substituting (int) DriveType.DRIVE_FIXED (where DriveType is an enumeration based on WinBase.h values) in place of PlatformInvokeKernel32.DRIVE_FIXED, worked fine.

But cutting/pasting of portions of C++ headers seems like a hack. Since everything else in the sample compiled/ran perfectly after I added the Win32 API declaration for PlatformInvokeKernel32.GetDriveType and the above substition, there ought to be a way to make the original code work.

So I hope you or some other kind soul comes up with one more suggestion.

Regards,
Richard

Matt Gerrans

Posts: 1153
Nickname: matt
Registered: Feb, 2002

Re: Access Win32 const DRIVE_FIXED - How to? Posted: Mar 17, 2003 12:15 AM
Reply to this message Reply
Oops, sorry I forgot to mention the crucial fact that you need to add a reference to the System.Management assembly to your project. (If you haven't done this before, you right click on the References in the Solution Explorer, and select it from the .Net list of references. If you are doing it from the command line, add WINDOWS\Microsoft.NET\Framework\v1.1.4322\System.Management.dll). This should resolve the problem of not finding the System.Management namespaces and the classes there.

As far as the cr/lf issue, I say we are now in the 21st century and cr/lf is completely meaningless on a windows form. I think it is especially stupid that it is required in .Net windows forms, when it isn't in plain old windows C/C++ apps.

As far as constant names, if MS doesn't "officially" define them somewhere in the .Net libraries, I don't know of any other choice than to get them from the old windows header files. It boils down to applying a meaningful name to a numeric value used in the API. These language-independent interfaces like WMI kind of fly in the face of old "use my header files for the meaningful names of these constants" schemes. I've had similar problems working with other COM controls, where the writer assumed that the clients are C++ programs that use the same headers. The same problems arise in JScript files, which are rife with redefinitions of constants, especially when they use a lot of WMI, but even with simple stuff like showing message boxes and creating files. VB always had this problem too.

Richard Muller

Posts: 10
Nickname: rlmuller
Registered: Mar, 2003

Re: Access Win32 const DRIVE_FIXED - How to? Posted: Mar 21, 2003 9:11 PM
Reply to this message Reply
Hi Matt,

I thought I posted a feply several days ago, but again I don't see it ... so I'm going to re-post the fact that all goes well as a result of your help.

I thought it was stupid that I didn't know where I got this sample code so I searched Google. It came from [/b]http://www.dotnetjunkies.com/quickstart/util/srcview.aspx?path=/quickstart/winforms/Samples/ControlReference/TreeViewCtl/TreeViewCtl.src&file=CS\TreeViewCtl.cs&font=3[/b].

Thanks again for all your advice. My C# education continues!

Regards,
Richard

P.S. Here are the main chunks of my code:

using System.Management;
using System.Text;


and following InitializeComponent();

ManagementClass manager = new ManagementClass("Win32_LogicalDisk");
ManagementObjectCollection drives = manager.GetInstances();
StringBuilder sbDrvDesc = new StringBuilder();
StringBuilder sbDrvAttr = new StringBuilder();
foreach( ManagementObject drive in drives )
{
sbDrvDesc.Append( drive["Caption"] + "\t" + drive["Description"] + "\r\n" );

sbDrvAttr.Remove(0, sbDrvAttr.Length);
foreach( PropertyData prop in drive.Properties )
{
if (drive[prop.Name] != null)
sbDrvAttr.Append( "\t" + prop.Name + " = " + drive[prop.Name] + "\r\n" );
}

// Append drive attributes
sbDrvDesc.Append( sbDrvAttr.ToString() + "\r\n" );
}
textboxDriveTypes.Text += sbDrvDesc.ToString();

Matt Gerrans

Posts: 1153
Nickname: matt
Registered: Feb, 2002

Re: Access Win32 const DRIVE_FIXED - How to? Posted: Mar 23, 2003 4:06 PM
Reply to this message Reply
We should get to the bottom of your posing problem, too. What's are the symptoms? Usually, you should be able to see your post right after you press the "Post Message" button. I don't think that even requires JavaScript or Cookies to be enabled. What do you see right after pressing the button?

Flat View: This topic has 8 replies on 1 page
Topic: vsvars32.bat ineffective - what's up? Previous Topic   Next Topic Topic: datagrid in c#

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use