How to call Win32 API function from C# (newbie)

D

David Ichilov

How do i call in CSharp to this function:
GetVolumeInformation

The GetVolumeInformation function retrieves information about a file system
and volume whose root directory is specified.


BOOL GetVolumeInformation(
LPCTSTR lpRootPathName,
LPTSTR lpVolumeNameBuffer,
DWORD nVolumeNameSize,
LPDWORD lpVolumeSerialNumber,
LPDWORD lpMaximumComponentLength,
LPDWORD lpFileSystemFlags,
LPTSTR lpFileSystemNameBuffer,
DWORD nFileSystemNameSize
);
Parameters
lpRootPathName
[in] Pointer to a string that contains the root directory of the volume to
be described. If this parameter is NULL, the root of the current directory
is used. A trailing backslash is required. For example, you would specify
\\MyServer\MyShare as \\MyServer\MyShare\, or the C drive as "C:\".
lpVolumeNameBuffer
[out] Pointer to a buffer that receives the name of the specified volume.
nVolumeNameSize
[in] Length of the volume name buffer, in TCHARs. This parameter is
ignored if the volume name buffer is not supplied.
lpVolumeSerialNumber
[out] Pointer to a variable that receives the volume serial number. This
parameter can be NULL if the serial number is not required.

Windows Me/98/95: If the queried volume is a network drive, the serial
number will not be returned.


lpMaximumComponentLength
[out] Pointer to a variable that receives the maximum length, in TCHARs,
of a file name component supported by the specified file system. A file name
component is that portion of a file name between backslashes.
The value stored in variable pointed to by *lpMaximumComponentLength is
used to indicate that long names are supported by the specified file system.
For example, for a FAT file system supporting long names, the function
stores the value 255, rather than the previous 8.3 indicator. Long names can
also be supported on systems that use the NTFS file system.

lpFileSystemFlags
[out] Pointer to a variable that receives flags associated with the
specified file system. This parameter can be one or more of the following
flags; however, FS_FILE_COMPRESSION and FS_VOL_IS_COMPRESSED are mutually
exclusive. Value Meaning
FILE_NAMED_STREAMS The file system supports named streams.
FILE_READ_ONLY_VOLUME The specified volume is read-only.

Windows 2000/NT and Windows Me/98/95: This value is not supported.


FILE_SUPPORTS_OBJECT_IDS The file system supports object
identifiers.
FILE_SUPPORTS_REPARSE_POINTS The file system supports reparse
points.
FILE_SUPPORTS_SPARSE_FILES The file system supports sparse files.
FILE_VOLUME_QUOTAS The file system supports disk quotas.
FS_CASE_IS_PRESERVED The file system preserves the case of file
names when it places a name on disk.
FS_CASE_SENSITIVE The file system supports case-sensitive file
names.
FS_FILE_COMPRESSION The file system supports file-based compression.
FS_FILE_ENCRYPTION The file system supports the Encrypted File
System (EFS).
FS_PERSISTENT_ACLS The file system preserves and enforces ACLs. For
example, NTFS preserves and enforces ACLs, and FAT does not.
FS_UNICODE_STORED_ON_DISK The file system supports Unicode in file
names as they appear on disk.
FS_VOL_IS_COMPRESSED The specified volume is a compressed volume;
for example, a DoubleSpace volume.

lpFileSystemNameBuffer
[out] Pointer to a buffer that receives the name of the file system (such
as FAT or NTFS).
nFileSystemNameSize
[in] Length of the file system name buffer, in TCHARs. This parameter is
ignored if the file system name buffer is not supplied.
Return Values
If all the requested information is retrieved, the return value is nonzero.

If not all the requested information is retrieved, the return value is zero.
To get extended error information, call GetLastError.

Remarks
If you are attempting to obtain information about a floppy drive that does
not have a floppy disk or a CD-ROM drive that does not have a compact disc,
the system displays a message box asking the user to insert a floppy disk or
a compact disc, respectively. To prevent the system from displaying this
message box, call the SetErrorMode function with SEM_FAILCRITICALERRORS.

The FS_VOL_IS_COMPRESSED flag is the only indicator of volume-based
compression. The file system name is not altered to indicate compression.
This flag comes back set on a DoubleSpace volume, for example. With
volume-based compression, an entire volume is either compressed or not
compressed.

The FS_FILE_COMPRESSION flag indicates whether a file system supports
file-based compression. With file-based compression, individual files can be
compressed or not compressed.

The FS_FILE_COMPRESSION and FS_VOL_IS_COMPRESSED flags are mutually
exclusive; both bits cannot come back set.

The maximum component length value, stored in lpMaximumComponentLength, is
the only indicator that a volume supports longer-than-normal FAT (or other
file system) file names. The file system name is not altered to indicate
support for long file names.

The GetCompressedFileSize function obtains the compressed size of a file.
The GetFileAttributes function can determine whether an individual file is
compressed.



Windows Me/98/95: GetVolumeInformationW is supported by the Microsoft Layer
for Unicode. To use this, you must add certain files to your application, as
outlined in Microsoft Layer for Unicode on Windows 95/98/Me Systems.



Example Code
For an example, see Enumerating Mount Points.

Requirements
Client: Included in Windows XP, Windows 2000 Professional, Windows NT
Workstation, Windows Me, Windows 98, and Windows 95.
Server: Included in Windows Server 2003, Windows 2000 Server, and Windows NT
Server.
Unicode: Implemented as Unicode and ANSI versions. Note that Unicode support
on Windows Me/98/95 requires Microsoft Layer for Unicode.
Header: Declared in Winbase.h; include Windows.h.
Library: Use Kernel32.lib.
 
A

Aravind C

HI David,

You would need to use PInvoke intorder to call Win32 APIs.
MSDN contains a few samples and tutorials on PInvoke. With that said, please
find below the PInvoke definition for GetVolumeInformation

[DllImport("kernel32.dll", CharSet=CharSet.Auto)]
public static extern bool GetVolumeInformation(string lpRootPathName,
StringBuilder lpVolumeNameBuffer,
[MarshalAs(UnmanagedType.U4)] int nVolumeNameSize,
[MarshalAs(UnmanagedType.U4)] ref int lpVolumeSerialNumber,
[MarshalAs(UnmanagedType.U4)] ref int lpMaximumComponentLength,
[MarshalAs(UnmanagedType.U4)] ref int lpFileSystemFlags,
StringBuilder lpFileSystemNameBuffer,
[MarshalAs(UnmanagedType.U4)] int nFileSystemNameSize);

Regards,
Aravind C


David Ichilov said:
How do i call in CSharp to this function:
GetVolumeInformation

The GetVolumeInformation function retrieves information about a file system
and volume whose root directory is specified.


BOOL GetVolumeInformation(
LPCTSTR lpRootPathName,
LPTSTR lpVolumeNameBuffer,
DWORD nVolumeNameSize,
LPDWORD lpVolumeSerialNumber,
LPDWORD lpMaximumComponentLength,
LPDWORD lpFileSystemFlags,
LPTSTR lpFileSystemNameBuffer,
DWORD nFileSystemNameSize
);
Parameters
lpRootPathName
[in] Pointer to a string that contains the root directory of the volume to
be described. If this parameter is NULL, the root of the current directory
is used. A trailing backslash is required. For example, you would specify
\\MyServer\MyShare as \\MyServer\MyShare\, or the C drive as "C:\".
lpVolumeNameBuffer
[out] Pointer to a buffer that receives the name of the specified volume.
nVolumeNameSize
[in] Length of the volume name buffer, in TCHARs. This parameter is
ignored if the volume name buffer is not supplied.
lpVolumeSerialNumber
[out] Pointer to a variable that receives the volume serial number. This
parameter can be NULL if the serial number is not required.

Windows Me/98/95: If the queried volume is a network drive, the serial
number will not be returned.


lpMaximumComponentLength
[out] Pointer to a variable that receives the maximum length, in TCHARs,
of a file name component supported by the specified file system. A file name
component is that portion of a file name between backslashes.
The value stored in variable pointed to by *lpMaximumComponentLength is
used to indicate that long names are supported by the specified file system.
For example, for a FAT file system supporting long names, the function
stores the value 255, rather than the previous 8.3 indicator. Long names can
also be supported on systems that use the NTFS file system.

lpFileSystemFlags
[out] Pointer to a variable that receives flags associated with the
specified file system. This parameter can be one or more of the following
flags; however, FS_FILE_COMPRESSION and FS_VOL_IS_COMPRESSED are mutually
exclusive. Value Meaning
FILE_NAMED_STREAMS The file system supports named streams.
FILE_READ_ONLY_VOLUME The specified volume is read-only.

Windows 2000/NT and Windows Me/98/95: This value is not supported.


FILE_SUPPORTS_OBJECT_IDS The file system supports object
identifiers.
FILE_SUPPORTS_REPARSE_POINTS The file system supports reparse
points.
FILE_SUPPORTS_SPARSE_FILES The file system supports sparse files.
FILE_VOLUME_QUOTAS The file system supports disk quotas.
FS_CASE_IS_PRESERVED The file system preserves the case of file
names when it places a name on disk.
FS_CASE_SENSITIVE The file system supports case-sensitive file
names.
FS_FILE_COMPRESSION The file system supports file-based compression.
FS_FILE_ENCRYPTION The file system supports the Encrypted File
System (EFS).
FS_PERSISTENT_ACLS The file system preserves and enforces ACLs. For
example, NTFS preserves and enforces ACLs, and FAT does not.
FS_UNICODE_STORED_ON_DISK The file system supports Unicode in file
names as they appear on disk.
FS_VOL_IS_COMPRESSED The specified volume is a compressed volume;
for example, a DoubleSpace volume.

lpFileSystemNameBuffer
[out] Pointer to a buffer that receives the name of the file system (such
as FAT or NTFS).
nFileSystemNameSize
[in] Length of the file system name buffer, in TCHARs. This parameter is
ignored if the file system name buffer is not supplied.
Return Values
If all the requested information is retrieved, the return value is nonzero.

If not all the requested information is retrieved, the return value is zero.
To get extended error information, call GetLastError.

Remarks
If you are attempting to obtain information about a floppy drive that does
not have a floppy disk or a CD-ROM drive that does not have a compact disc,
the system displays a message box asking the user to insert a floppy disk or
a compact disc, respectively. To prevent the system from displaying this
message box, call the SetErrorMode function with SEM_FAILCRITICALERRORS.

The FS_VOL_IS_COMPRESSED flag is the only indicator of volume-based
compression. The file system name is not altered to indicate compression.
This flag comes back set on a DoubleSpace volume, for example. With
volume-based compression, an entire volume is either compressed or not
compressed.

The FS_FILE_COMPRESSION flag indicates whether a file system supports
file-based compression. With file-based compression, individual files can be
compressed or not compressed.

The FS_FILE_COMPRESSION and FS_VOL_IS_COMPRESSED flags are mutually
exclusive; both bits cannot come back set.

The maximum component length value, stored in lpMaximumComponentLength, is
the only indicator that a volume supports longer-than-normal FAT (or other
file system) file names. The file system name is not altered to indicate
support for long file names.

The GetCompressedFileSize function obtains the compressed size of a file.
The GetFileAttributes function can determine whether an individual file is
compressed.



Windows Me/98/95: GetVolumeInformationW is supported by the Microsoft Layer
for Unicode. To use this, you must add certain files to your application, as
outlined in Microsoft Layer for Unicode on Windows 95/98/Me Systems.



Example Code
For an example, see Enumerating Mount Points.

Requirements
Client: Included in Windows XP, Windows 2000 Professional, Windows NT
Workstation, Windows Me, Windows 98, and Windows 95.
Server: Included in Windows Server 2003, Windows 2000 Server, and Windows NT
Server.
Unicode: Implemented as Unicode and ANSI versions. Note that Unicode support
on Windows Me/98/95 requires Microsoft Layer for Unicode.
Header: Declared in Winbase.h; include Windows.h.
Library: Use Kernel32.lib.
 
N

news

Hmm... What UnmanagedType is HICON???


Aravind C said:
HI David,

You would need to use PInvoke intorder to call Win32 APIs.
MSDN contains a few samples and tutorials on PInvoke. With that said, please
find below the PInvoke definition for GetVolumeInformation

[DllImport("kernel32.dll", CharSet=CharSet.Auto)]
public static extern bool GetVolumeInformation(string lpRootPathName,
StringBuilder lpVolumeNameBuffer,
[MarshalAs(UnmanagedType.U4)] int nVolumeNameSize,
[MarshalAs(UnmanagedType.U4)] ref int lpVolumeSerialNumber,
[MarshalAs(UnmanagedType.U4)] ref int lpMaximumComponentLength,
[MarshalAs(UnmanagedType.U4)] ref int lpFileSystemFlags,
StringBuilder lpFileSystemNameBuffer,
[MarshalAs(UnmanagedType.U4)] int nFileSystemNameSize);

Regards,
Aravind C


David Ichilov said:
How do i call in CSharp to this function:
GetVolumeInformation

The GetVolumeInformation function retrieves information about a file system
and volume whose root directory is specified.


BOOL GetVolumeInformation(
LPCTSTR lpRootPathName,
LPTSTR lpVolumeNameBuffer,
DWORD nVolumeNameSize,
LPDWORD lpVolumeSerialNumber,
LPDWORD lpMaximumComponentLength,
LPDWORD lpFileSystemFlags,
LPTSTR lpFileSystemNameBuffer,
DWORD nFileSystemNameSize
);
Parameters
lpRootPathName
[in] Pointer to a string that contains the root directory of the
volume
to
be described. If this parameter is NULL, the root of the current directory
is used. A trailing backslash is required. For example, you would specify
\\MyServer\MyShare as \\MyServer\MyShare\, or the C drive as "C:\".
lpVolumeNameBuffer
[out] Pointer to a buffer that receives the name of the specified volume.
nVolumeNameSize
[in] Length of the volume name buffer, in TCHARs. This parameter is
ignored if the volume name buffer is not supplied.
lpVolumeSerialNumber
[out] Pointer to a variable that receives the volume serial number. This
parameter can be NULL if the serial number is not required.

Windows Me/98/95: If the queried volume is a network drive, the serial
number will not be returned.


lpMaximumComponentLength
[out] Pointer to a variable that receives the maximum length, in TCHARs,
of a file name component supported by the specified file system. A file name
component is that portion of a file name between backslashes.
The value stored in variable pointed to by *lpMaximumComponentLength is
used to indicate that long names are supported by the specified file system.
For example, for a FAT file system supporting long names, the function
stores the value 255, rather than the previous 8.3 indicator. Long names can
also be supported on systems that use the NTFS file system.

lpFileSystemFlags
[out] Pointer to a variable that receives flags associated with the
specified file system. This parameter can be one or more of the following
flags; however, FS_FILE_COMPRESSION and FS_VOL_IS_COMPRESSED are mutually
exclusive. Value Meaning
FILE_NAMED_STREAMS The file system supports named streams.
FILE_READ_ONLY_VOLUME The specified volume is read-only.

Windows 2000/NT and Windows Me/98/95: This value is not supported.


FILE_SUPPORTS_OBJECT_IDS The file system supports object
identifiers.
FILE_SUPPORTS_REPARSE_POINTS The file system supports reparse
points.
FILE_SUPPORTS_SPARSE_FILES The file system supports sparse files.
FILE_VOLUME_QUOTAS The file system supports disk quotas.
FS_CASE_IS_PRESERVED The file system preserves the case of file
names when it places a name on disk.
FS_CASE_SENSITIVE The file system supports case-sensitive file
names.
FS_FILE_COMPRESSION The file system supports file-based compression.
FS_FILE_ENCRYPTION The file system supports the Encrypted File
System (EFS).
FS_PERSISTENT_ACLS The file system preserves and enforces ACLs. For
example, NTFS preserves and enforces ACLs, and FAT does not.
FS_UNICODE_STORED_ON_DISK The file system supports Unicode in file
names as they appear on disk.
FS_VOL_IS_COMPRESSED The specified volume is a compressed volume;
for example, a DoubleSpace volume.

lpFileSystemNameBuffer
[out] Pointer to a buffer that receives the name of the file system (such
as FAT or NTFS).
nFileSystemNameSize
[in] Length of the file system name buffer, in TCHARs. This parameter is
ignored if the file system name buffer is not supplied.
Return Values
If all the requested information is retrieved, the return value is nonzero.

If not all the requested information is retrieved, the return value is zero.
To get extended error information, call GetLastError.

Remarks
If you are attempting to obtain information about a floppy drive that does
not have a floppy disk or a CD-ROM drive that does not have a compact disc,
the system displays a message box asking the user to insert a floppy
disk
or
a compact disc, respectively. To prevent the system from displaying this
message box, call the SetErrorMode function with SEM_FAILCRITICALERRORS.

The FS_VOL_IS_COMPRESSED flag is the only indicator of volume-based
compression. The file system name is not altered to indicate compression.
This flag comes back set on a DoubleSpace volume, for example. With
volume-based compression, an entire volume is either compressed or not
compressed.

The FS_FILE_COMPRESSION flag indicates whether a file system supports
file-based compression. With file-based compression, individual files
can
be
compressed or not compressed.

The FS_FILE_COMPRESSION and FS_VOL_IS_COMPRESSED flags are mutually
exclusive; both bits cannot come back set.

The maximum component length value, stored in lpMaximumComponentLength, is
the only indicator that a volume supports longer-than-normal FAT (or other
file system) file names. The file system name is not altered to indicate
support for long file names.

The GetCompressedFileSize function obtains the compressed size of a file.
The GetFileAttributes function can determine whether an individual file is
compressed.



Windows Me/98/95: GetVolumeInformationW is supported by the Microsoft Layer
for Unicode. To use this, you must add certain files to your
application,
as
outlined in Microsoft Layer for Unicode on Windows 95/98/Me Systems.



Example Code
For an example, see Enumerating Mount Points.

Requirements
Client: Included in Windows XP, Windows 2000 Professional, Windows NT
Workstation, Windows Me, Windows 98, and Windows 95.
Server: Included in Windows Server 2003, Windows 2000 Server, and
Windows
NT
Server.
Unicode: Implemented as Unicode and ANSI versions. Note that Unicode support
on Windows Me/98/95 requires Microsoft Layer for Unicode.
Header: Declared in Winbase.h; include Windows.h.
Library: Use Kernel32.lib.
 
A

Aravind C

Hi,

The unmanaged HICON datatype generally maps to the 'IntPtr' managed type.

Regards,
Aravind C

Hmm... What UnmanagedType is HICON???


Aravind C said:
HI David,

You would need to use PInvoke intorder to call Win32 APIs.
MSDN contains a few samples and tutorials on PInvoke. With that said, please
find below the PInvoke definition for GetVolumeInformation

[DllImport("kernel32.dll", CharSet=CharSet.Auto)]
public static extern bool GetVolumeInformation(string lpRootPathName,
StringBuilder lpVolumeNameBuffer,
[MarshalAs(UnmanagedType.U4)] int nVolumeNameSize,
[MarshalAs(UnmanagedType.U4)] ref int lpVolumeSerialNumber,
[MarshalAs(UnmanagedType.U4)] ref int lpMaximumComponentLength,
[MarshalAs(UnmanagedType.U4)] ref int lpFileSystemFlags,
StringBuilder lpFileSystemNameBuffer,
[MarshalAs(UnmanagedType.U4)] int nFileSystemNameSize);

Regards,
Aravind C


David Ichilov said:
How do i call in CSharp to this function:
GetVolumeInformation

The GetVolumeInformation function retrieves information about a file system
and volume whose root directory is specified.


BOOL GetVolumeInformation(
LPCTSTR lpRootPathName,
LPTSTR lpVolumeNameBuffer,
DWORD nVolumeNameSize,
LPDWORD lpVolumeSerialNumber,
LPDWORD lpMaximumComponentLength,
LPDWORD lpFileSystemFlags,
LPTSTR lpFileSystemNameBuffer,
DWORD nFileSystemNameSize
);
Parameters
lpRootPathName
[in] Pointer to a string that contains the root directory of the
volume
to
be described. If this parameter is NULL, the root of the current directory
is used. A trailing backslash is required. For example, you would specify
\\MyServer\MyShare as \\MyServer\MyShare\, or the C drive as "C:\".
lpVolumeNameBuffer
[out] Pointer to a buffer that receives the name of the specified volume.
nVolumeNameSize
[in] Length of the volume name buffer, in TCHARs. This parameter is
ignored if the volume name buffer is not supplied.
lpVolumeSerialNumber
[out] Pointer to a variable that receives the volume serial number. This
parameter can be NULL if the serial number is not required.

Windows Me/98/95: If the queried volume is a network drive, the serial
number will not be returned.


lpMaximumComponentLength
[out] Pointer to a variable that receives the maximum length, in TCHARs,
of a file name component supported by the specified file system. A
file
name
component is that portion of a file name between backslashes.
The value stored in variable pointed to by *lpMaximumComponentLength is
used to indicate that long names are supported by the specified file system.
For example, for a FAT file system supporting long names, the function
stores the value 255, rather than the previous 8.3 indicator. Long
names
can
also be supported on systems that use the NTFS file system.

lpFileSystemFlags
[out] Pointer to a variable that receives flags associated with the
specified file system. This parameter can be one or more of the following
flags; however, FS_FILE_COMPRESSION and FS_VOL_IS_COMPRESSED are mutually
exclusive. Value Meaning
FILE_NAMED_STREAMS The file system supports named streams.
FILE_READ_ONLY_VOLUME The specified volume is read-only.

Windows 2000/NT and Windows Me/98/95: This value is not supported.


FILE_SUPPORTS_OBJECT_IDS The file system supports object
identifiers.
FILE_SUPPORTS_REPARSE_POINTS The file system supports reparse
points.
FILE_SUPPORTS_SPARSE_FILES The file system supports sparse files.
FILE_VOLUME_QUOTAS The file system supports disk quotas.
FS_CASE_IS_PRESERVED The file system preserves the case of file
names when it places a name on disk.
FS_CASE_SENSITIVE The file system supports case-sensitive file
names.
FS_FILE_COMPRESSION The file system supports file-based compression.
FS_FILE_ENCRYPTION The file system supports the Encrypted File
System (EFS).
FS_PERSISTENT_ACLS The file system preserves and enforces
ACLs.
For
example, NTFS preserves and enforces ACLs, and FAT does not.
FS_UNICODE_STORED_ON_DISK The file system supports Unicode in file
names as they appear on disk.
FS_VOL_IS_COMPRESSED The specified volume is a compressed volume;
for example, a DoubleSpace volume.

lpFileSystemNameBuffer
[out] Pointer to a buffer that receives the name of the file system (such
as FAT or NTFS).
nFileSystemNameSize
[in] Length of the file system name buffer, in TCHARs. This
parameter
is disk can
lpMaximumComponentLength,
file
is application, Windows
 
N

news

Aravind please, look at this:
IconCount working well, but second calling throws this exception:
'The Win32 handle you passed to Icon is invalid or of the wrong type'
[DllImport("SHELL32.DLL")]

public static extern IntPtr ExtractIconEx(string lpszFile, int nIconIndex, IntPtr phiconLarge, IntPtr phiconSmall, int nIcons);


private void ExtractIconsFromFile(string path, int iconIndex)

{

IntPtr smallIcon = IntPtr.Zero;

IntPtr largeIcon = IntPtr.Zero;

int iconCount = ExtractIconEx(path , -1, smallIcon, largeIcon , -1).ToInt32();

if(iconIndex > iconCount)

{

//TODO: Default Icon

}

else

{

ExtractIconEx(path, iconIndex, smallIcon, largeIcon, 1);

m_SmallIcon = Icon.FromHandle(smallIcon);

m_LargeIcon = Icon.FromHandle(largeIcon);

}


}

Aravind C said:
Hi,

The unmanaged HICON datatype generally maps to the 'IntPtr' managed type.

Regards,
Aravind C

Hmm... What UnmanagedType is HICON???


Aravind C said:
HI David,

You would need to use PInvoke intorder to call Win32 APIs.
MSDN contains a few samples and tutorials on PInvoke. With that said, please
find below the PInvoke definition for GetVolumeInformation

[DllImport("kernel32.dll", CharSet=CharSet.Auto)]
public static extern bool GetVolumeInformation(string lpRootPathName,
StringBuilder lpVolumeNameBuffer,
[MarshalAs(UnmanagedType.U4)] int nVolumeNameSize,
[MarshalAs(UnmanagedType.U4)] ref int lpVolumeSerialNumber,
[MarshalAs(UnmanagedType.U4)] ref int lpMaximumComponentLength,
[MarshalAs(UnmanagedType.U4)] ref int lpFileSystemFlags,
StringBuilder lpFileSystemNameBuffer,
[MarshalAs(UnmanagedType.U4)] int nFileSystemNameSize);

Regards,
Aravind C




How do i call in CSharp to this function:
GetVolumeInformation

The GetVolumeInformation function retrieves information about a file
system
and volume whose root directory is specified.


BOOL GetVolumeInformation(
LPCTSTR lpRootPathName,
LPTSTR lpVolumeNameBuffer,
DWORD nVolumeNameSize,
LPDWORD lpVolumeSerialNumber,
LPDWORD lpMaximumComponentLength,
LPDWORD lpFileSystemFlags,
LPTSTR lpFileSystemNameBuffer,
DWORD nFileSystemNameSize
);
Parameters
lpRootPathName
[in] Pointer to a string that contains the root directory of the volume
to
be described. If this parameter is NULL, the root of the current directory
is used. A trailing backslash is required. For example, you would specify
\\MyServer\MyShare as \\MyServer\MyShare\, or the C drive as "C:\".
lpVolumeNameBuffer
[out] Pointer to a buffer that receives the name of the specified
volume.
nVolumeNameSize
[in] Length of the volume name buffer, in TCHARs. This parameter is
ignored if the volume name buffer is not supplied.
lpVolumeSerialNumber
[out] Pointer to a variable that receives the volume serial number. This
parameter can be NULL if the serial number is not required.

Windows Me/98/95: If the queried volume is a network drive, the serial
number will not be returned.


lpMaximumComponentLength
[out] Pointer to a variable that receives the maximum length, in TCHARs,
of a file name component supported by the specified file system. A file
name
component is that portion of a file name between backslashes.
The value stored in variable pointed to by *lpMaximumComponentLength is
used to indicate that long names are supported by the specified file
system.
For example, for a FAT file system supporting long names, the function
stores the value 255, rather than the previous 8.3 indicator. Long names
can
also be supported on systems that use the NTFS file system.

lpFileSystemFlags
[out] Pointer to a variable that receives flags associated with the
specified file system. This parameter can be one or more of the following
flags; however, FS_FILE_COMPRESSION and FS_VOL_IS_COMPRESSED are mutually
exclusive. Value Meaning
FILE_NAMED_STREAMS The file system supports named streams.
FILE_READ_ONLY_VOLUME The specified volume is read-only.

Windows 2000/NT and Windows Me/98/95: This value is not
supported.


FILE_SUPPORTS_OBJECT_IDS The file system supports object
identifiers.
FILE_SUPPORTS_REPARSE_POINTS The file system supports reparse
points.
FILE_SUPPORTS_SPARSE_FILES The file system supports sparse files.
FILE_VOLUME_QUOTAS The file system supports disk quotas.
FS_CASE_IS_PRESERVED The file system preserves the case of file
names when it places a name on disk.
FS_CASE_SENSITIVE The file system supports case-sensitive file
names.
FS_FILE_COMPRESSION The file system supports file-based
compression.
FS_FILE_ENCRYPTION The file system supports the Encrypted File
System (EFS).
FS_PERSISTENT_ACLS The file system preserves and enforces ACLs.
For
example, NTFS preserves and enforces ACLs, and FAT does not.
FS_UNICODE_STORED_ON_DISK The file system supports Unicode in file
names as they appear on disk.
FS_VOL_IS_COMPRESSED The specified volume is a compressed volume;
for example, a DoubleSpace volume.

lpFileSystemNameBuffer
[out] Pointer to a buffer that receives the name of the file system
(such
as FAT or NTFS).
nFileSystemNameSize
[in] Length of the file system name buffer, in TCHARs. This
parameter
is
ignored if the file system name buffer is not supplied.
Return Values
If all the requested information is retrieved, the return value is
nonzero.

If not all the requested information is retrieved, the return value is
zero.
To get extended error information, call GetLastError.

Remarks
If you are attempting to obtain information about a floppy drive that does
not have a floppy disk or a CD-ROM drive that does not have a compact
disc,
the system displays a message box asking the user to insert a floppy disk
or
a compact disc, respectively. To prevent the system from displaying this
message box, call the SetErrorMode function with SEM_FAILCRITICALERRORS.

The FS_VOL_IS_COMPRESSED flag is the only indicator of volume-based
compression. The file system name is not altered to indicate compression.
This flag comes back set on a DoubleSpace volume, for example. With
volume-based compression, an entire volume is either compressed or not
compressed.

The FS_FILE_COMPRESSION flag indicates whether a file system supports
file-based compression. With file-based compression, individual files can
be
compressed or not compressed.

The FS_FILE_COMPRESSION and FS_VOL_IS_COMPRESSED flags are mutually
exclusive; both bits cannot come back set.

The maximum component length value, stored in
lpMaximumComponentLength,
is
the only indicator that a volume supports longer-than-normal FAT (or other
file system) file names. The file system name is not altered to indicate
support for long file names.

The GetCompressedFileSize function obtains the compressed size of a file.
The GetFileAttributes function can determine whether an individual
file
is
compressed.



Windows Me/98/95: GetVolumeInformationW is supported by the Microsoft
Layer
for Unicode. To use this, you must add certain files to your application,
as
outlined in Microsoft Layer for Unicode on Windows 95/98/Me Systems.



Example Code
For an example, see Enumerating Mount Points.

Requirements
Client: Included in Windows XP, Windows 2000 Professional, Windows NT
Workstation, Windows Me, Windows 98, and Windows 95.
Server: Included in Windows Server 2003, Windows 2000 Server, and Windows
NT
Server.
Unicode: Implemented as Unicode and ANSI versions. Note that Unicode
support
on Windows Me/98/95 requires Microsoft Layer for Unicode.
Header: Declared in Winbase.h; include Windows.h.
Library: Use Kernel32.lib.
 
A

Aravind C

Hi,

Actually, HICON* maps to IntPtr[].

So your modified PInvoke definition would be:

[DllImport("Shell32.Dll", CharSet = CharSet.Auto)]
public static extern uint ExtractIconEx(string lpszFile,
int nIconIndex, IntPtr[] phiconLarge, IntPtr[] phiconSmall, uint nIcons);

Here's how we invoke ExtractIconEx:

int iconCount = ExtractIconEx(path, -1, null, null, 0);
IntPtr[] smallIcon = new IntPtr[iconCount];
IntPtr[] largeIcon = new IntPtr[iconCount];
ExtractIconEx(path, 0, smallIcon, largeIcon, iconCount);


Regards,
Aravind C


Aravind please, look at this:
IconCount working well, but second calling throws this exception:
'The Win32 handle you passed to Icon is invalid or of the wrong type'
[DllImport("SHELL32.DLL")]

public static extern IntPtr ExtractIconEx(string lpszFile, int nIconIndex, IntPtr phiconLarge, IntPtr phiconSmall, int nIcons);


private void ExtractIconsFromFile(string path, int iconIndex)

{

IntPtr smallIcon = IntPtr.Zero;

IntPtr largeIcon = IntPtr.Zero;

int iconCount = ExtractIconEx(path , -1, smallIcon, largeIcon , -1).ToInt32();

if(iconIndex > iconCount)

{

//TODO: Default Icon

}

else

{

ExtractIconEx(path, iconIndex, smallIcon, largeIcon, 1);

m_SmallIcon = Icon.FromHandle(smallIcon);

m_LargeIcon = Icon.FromHandle(largeIcon);

}


}

Aravind C said:
Hi,

The unmanaged HICON datatype generally maps to the 'IntPtr' managed type.

Regards,
Aravind C

Hmm... What UnmanagedType is HICON???


Aravind C said:
HI David,

You would need to use PInvoke intorder to call Win32 APIs.
MSDN contains a few samples and tutorials on PInvoke. With that said, please
find below the PInvoke definition for GetVolumeInformation

[DllImport("kernel32.dll", CharSet=CharSet.Auto)]
public static extern bool GetVolumeInformation(string lpRootPathName,
StringBuilder lpVolumeNameBuffer,
[MarshalAs(UnmanagedType.U4)] int nVolumeNameSize,
[MarshalAs(UnmanagedType.U4)] ref int lpVolumeSerialNumber,
[MarshalAs(UnmanagedType.U4)] ref int lpMaximumComponentLength,
[MarshalAs(UnmanagedType.U4)] ref int lpFileSystemFlags,
StringBuilder lpFileSystemNameBuffer,
[MarshalAs(UnmanagedType.U4)] int nFileSystemNameSize);

Regards,
Aravind C




How do i call in CSharp to this function:
GetVolumeInformation

The GetVolumeInformation function retrieves information about a file
system
and volume whose root directory is specified.


BOOL GetVolumeInformation(
LPCTSTR lpRootPathName,
LPTSTR lpVolumeNameBuffer,
DWORD nVolumeNameSize,
LPDWORD lpVolumeSerialNumber,
LPDWORD lpMaximumComponentLength,
LPDWORD lpFileSystemFlags,
LPTSTR lpFileSystemNameBuffer,
DWORD nFileSystemNameSize
);
Parameters
lpRootPathName
[in] Pointer to a string that contains the root directory of the volume
to
be described. If this parameter is NULL, the root of the current directory
is used. A trailing backslash is required. For example, you would specify
\\MyServer\MyShare as \\MyServer\MyShare\, or the C drive as "C:\".
lpVolumeNameBuffer
[out] Pointer to a buffer that receives the name of the specified
volume.
nVolumeNameSize
[in] Length of the volume name buffer, in TCHARs. This parameter is
ignored if the volume name buffer is not supplied.
lpVolumeSerialNumber
[out] Pointer to a variable that receives the volume serial number. This
parameter can be NULL if the serial number is not required.

Windows Me/98/95: If the queried volume is a network drive, the serial
number will not be returned.


lpMaximumComponentLength
[out] Pointer to a variable that receives the maximum length, in TCHARs,
of a file name component supported by the specified file system. A file
name
component is that portion of a file name between backslashes.
The value stored in variable pointed to by *lpMaximumComponentLength is
used to indicate that long names are supported by the specified file
system.
For example, for a FAT file system supporting long names, the function
stores the value 255, rather than the previous 8.3 indicator. Long names
can
also be supported on systems that use the NTFS file system.

lpFileSystemFlags
[out] Pointer to a variable that receives flags associated with the
specified file system. This parameter can be one or more of the following
flags; however, FS_FILE_COMPRESSION and FS_VOL_IS_COMPRESSED are mutually
exclusive. Value Meaning
FILE_NAMED_STREAMS The file system supports named streams.
FILE_READ_ONLY_VOLUME The specified volume is read-only.

Windows 2000/NT and Windows Me/98/95: This value is not
supported.


FILE_SUPPORTS_OBJECT_IDS The file system supports object
identifiers.
FILE_SUPPORTS_REPARSE_POINTS The file system supports reparse
points.
FILE_SUPPORTS_SPARSE_FILES The file system supports sparse files.
FILE_VOLUME_QUOTAS The file system supports disk quotas.
FS_CASE_IS_PRESERVED The file system preserves the case of file
names when it places a name on disk.
FS_CASE_SENSITIVE The file system supports case-sensitive file
names.
FS_FILE_COMPRESSION The file system supports file-based
compression.
FS_FILE_ENCRYPTION The file system supports the Encrypted File
System (EFS).
FS_PERSISTENT_ACLS The file system preserves and enforces ACLs.
For
example, NTFS preserves and enforces ACLs, and FAT does not.
FS_UNICODE_STORED_ON_DISK The file system supports Unicode in file
names as they appear on disk.
FS_VOL_IS_COMPRESSED The specified volume is a compressed volume;
for example, a DoubleSpace volume.

lpFileSystemNameBuffer
[out] Pointer to a buffer that receives the name of the file system
(such
as FAT or NTFS).
nFileSystemNameSize
[in] Length of the file system name buffer, in TCHARs. This
parameter
is
ignored if the file system name buffer is not supplied.
Return Values
If all the requested information is retrieved, the return value is
nonzero.

If not all the requested information is retrieved, the return value is
zero.
To get extended error information, call GetLastError.

Remarks
If you are attempting to obtain information about a floppy drive that does
not have a floppy disk or a CD-ROM drive that does not have a compact
disc,
the system displays a message box asking the user to insert a floppy disk
or
a compact disc, respectively. To prevent the system from displaying this
message box, call the SetErrorMode function with SEM_FAILCRITICALERRORS.

The FS_VOL_IS_COMPRESSED flag is the only indicator of volume-based
compression. The file system name is not altered to indicate compression.
This flag comes back set on a DoubleSpace volume, for example. With
volume-based compression, an entire volume is either compressed or not
compressed.

The FS_FILE_COMPRESSION flag indicates whether a file system supports
file-based compression. With file-based compression, individual files can
be
compressed or not compressed.

The FS_FILE_COMPRESSION and FS_VOL_IS_COMPRESSED flags are mutually
exclusive; both bits cannot come back set.

The maximum component length value, stored in
lpMaximumComponentLength,
is
the only indicator that a volume supports longer-than-normal FAT (or other
file system) file names. The file system name is not altered to indicate
support for long file names.

The GetCompressedFileSize function obtains the compressed size of a file.
The GetFileAttributes function can determine whether an individual
file
is
compressed.



Windows Me/98/95: GetVolumeInformationW is supported by the Microsoft
Layer
for Unicode. To use this, you must add certain files to your application,
as
outlined in Microsoft Layer for Unicode on Windows 95/98/Me Systems.



Example Code
For an example, see Enumerating Mount Points.

Requirements
Client: Included in Windows XP, Windows 2000 Professional, Windows NT
Workstation, Windows Me, Windows 98, and Windows 95.
Server: Included in Windows Server 2003, Windows 2000 Server, and Windows
NT
Server.
Unicode: Implemented as Unicode and ANSI versions. Note that Unicode
support
on Windows Me/98/95 requires Microsoft Layer for Unicode.
Header: Declared in Winbase.h; include Windows.h.
Library: Use Kernel32.lib.
 
A

Aravind C

Hi,

Actually, HICON* maps to IntPtr[].

So your modified PInvoke definition would be:

[DllImport("Shell32.Dll", CharSet = CharSet.Auto)]
public static extern uint ExtractIconEx(string lpszFile,
int nIconIndex, IntPtr[] phiconLarge, IntPtr[] phiconSmall, uint nIcons);

Here's how we invoke ExtractIconEx:

int iconCount = ExtractIconEx(path, -1, null, null, 0);
IntPtr[] smallIcon = new IntPtr[iconCount];
IntPtr[] largeIcon = new IntPtr[iconCount];
ExtractIconEx(path, 0, smallIcon, largeIcon, iconCount);


Regards,
Aravind C


Aravind please, look at this:
IconCount working well, but second calling throws this exception:
'The Win32 handle you passed to Icon is invalid or of the wrong type'
[DllImport("SHELL32.DLL")]
public static extern IntPtr ExtractIconEx(string lpszFile, int nIconIndex,
IntPtr phiconLarge, IntPtr phiconSmall, int nIcons);
private void ExtractIconsFromFile(string path, int iconIndex)
{
IntPtr smallIcon = IntPtr.Zero;
IntPtr largeIcon = IntPtr.Zero;
int iconCount = ExtractIconEx(path , -1, smallIcon, largeIcon
, -1).ToInt32();
if(iconIndex > iconCount)
{
//TODO: Default Icon
}
else
{
ExtractIconEx(path, iconIndex, smallIcon, largeIcon, 1);
m_SmallIcon = Icon.FromHandle(smallIcon);
m_LargeIcon = Icon.FromHandle(largeIcon);
}
}
Aravind C said:
Hi,

The unmanaged HICON datatype generally maps to the 'IntPtr' managed type.

Regards,
Aravind C

Hmm... What UnmanagedType is HICON???


Aravind C said:
HI David,

You would need to use PInvoke intorder to call Win32 APIs.
MSDN contains a few samples and tutorials on PInvoke. With that said, please
find below the PInvoke definition for GetVolumeInformation

[DllImport("kernel32.dll", CharSet=CharSet.Auto)]
public static extern bool GetVolumeInformation(string lpRootPathName,
StringBuilder lpVolumeNameBuffer,
[MarshalAs(UnmanagedType.U4)] int nVolumeNameSize,
[MarshalAs(UnmanagedType.U4)] ref int lpVolumeSerialNumber,
[MarshalAs(UnmanagedType.U4)] ref int lpMaximumComponentLength,
[MarshalAs(UnmanagedType.U4)] ref int lpFileSystemFlags,
StringBuilder lpFileSystemNameBuffer,
[MarshalAs(UnmanagedType.U4)] int nFileSystemNameSize);

Regards,
Aravind C




How do i call in CSharp to this function:
GetVolumeInformation

The GetVolumeInformation function retrieves information about a file
system
and volume whose root directory is specified.


BOOL GetVolumeInformation(
LPCTSTR lpRootPathName,
LPTSTR lpVolumeNameBuffer,
DWORD nVolumeNameSize,
LPDWORD lpVolumeSerialNumber,
LPDWORD lpMaximumComponentLength,
LPDWORD lpFileSystemFlags,
LPTSTR lpFileSystemNameBuffer,
DWORD nFileSystemNameSize
);
Parameters
lpRootPathName
[in] Pointer to a string that contains the root directory of the volume
to
be described. If this parameter is NULL, the root of the current directory
is used. A trailing backslash is required. For example, you would specify
\\MyServer\MyShare as \\MyServer\MyShare\, or the C drive as "C:\".
lpVolumeNameBuffer
[out] Pointer to a buffer that receives the name of the specified
volume.
nVolumeNameSize
[in] Length of the volume name buffer, in TCHARs. This parameter is
ignored if the volume name buffer is not supplied.
lpVolumeSerialNumber
[out] Pointer to a variable that receives the volume serial
number.
This
parameter can be NULL if the serial number is not required.

Windows Me/98/95: If the queried volume is a network drive, the serial
number will not be returned.


lpMaximumComponentLength
[out] Pointer to a variable that receives the maximum length, in TCHARs,
of a file name component supported by the specified file system. A file
name
component is that portion of a file name between backslashes.
The value stored in variable pointed to by
*lpMaximumComponentLength
is
used to indicate that long names are supported by the specified file
system.
For example, for a FAT file system supporting long names, the function
stores the value 255, rather than the previous 8.3 indicator. Long names
can
also be supported on systems that use the NTFS file system.

lpFileSystemFlags
[out] Pointer to a variable that receives flags associated with the
specified file system. This parameter can be one or more of the following
flags; however, FS_FILE_COMPRESSION and FS_VOL_IS_COMPRESSED are mutually
exclusive. Value Meaning
FILE_NAMED_STREAMS The file system supports named streams.
FILE_READ_ONLY_VOLUME The specified volume is read-only.

Windows 2000/NT and Windows Me/98/95: This value is not
supported.


FILE_SUPPORTS_OBJECT_IDS The file system supports object
identifiers.
FILE_SUPPORTS_REPARSE_POINTS The file system supports reparse
points.
FILE_SUPPORTS_SPARSE_FILES The file system supports sparse files.
FILE_VOLUME_QUOTAS The file system supports disk quotas.
FS_CASE_IS_PRESERVED The file system preserves the case of file
names when it places a name on disk.
FS_CASE_SENSITIVE The file system supports case-sensitive file
names.
FS_FILE_COMPRESSION The file system supports file-based
compression.
FS_FILE_ENCRYPTION The file system supports the Encrypted File
System (EFS).
FS_PERSISTENT_ACLS The file system preserves and enforces ACLs.
For
example, NTFS preserves and enforces ACLs, and FAT does not.
FS_UNICODE_STORED_ON_DISK The file system supports Unicode
in
file
names as they appear on disk.
FS_VOL_IS_COMPRESSED The specified volume is a compressed volume;
for example, a DoubleSpace volume.

lpFileSystemNameBuffer
[out] Pointer to a buffer that receives the name of the file system
(such
as FAT or NTFS).
nFileSystemNameSize
[in] Length of the file system name buffer, in TCHARs. This
parameter
is
ignored if the file system name buffer is not supplied.
Return Values
If all the requested information is retrieved, the return value is
nonzero.

If not all the requested information is retrieved, the return value is
zero.
To get extended error information, call GetLastError.

Remarks
If you are attempting to obtain information about a floppy drive
that
does
not have a floppy disk or a CD-ROM drive that does not have a compact
disc,
the system displays a message box asking the user to insert a floppy disk
or
a compact disc, respectively. To prevent the system from displaying this
message box, call the SetErrorMode function with SEM_FAILCRITICALERRORS.

The FS_VOL_IS_COMPRESSED flag is the only indicator of volume-based
compression. The file system name is not altered to indicate compression.
This flag comes back set on a DoubleSpace volume, for example. With
volume-based compression, an entire volume is either compressed or not
compressed.

The FS_FILE_COMPRESSION flag indicates whether a file system supports
file-based compression. With file-based compression, individual
files
can
be
compressed or not compressed.

The FS_FILE_COMPRESSION and FS_VOL_IS_COMPRESSED flags are mutually
exclusive; both bits cannot come back set.

The maximum component length value, stored in
lpMaximumComponentLength,
is
the only indicator that a volume supports longer-than-normal FAT (or other
file system) file names. The file system name is not altered to indicate
support for long file names.

The GetCompressedFileSize function obtains the compressed size of a file.
The GetFileAttributes function can determine whether an individual
file
is
compressed.



Windows Me/98/95: GetVolumeInformationW is supported by the Microsoft
Layer
for Unicode. To use this, you must add certain files to your application,
as
outlined in Microsoft Layer for Unicode on Windows 95/98/Me Systems.



Example Code
For an example, see Enumerating Mount Points.

Requirements
Client: Included in Windows XP, Windows 2000 Professional, Windows NT
Workstation, Windows Me, Windows 98, and Windows 95.
Server: Included in Windows Server 2003, Windows 2000 Server, and Windows
NT
Server.
Unicode: Implemented as Unicode and ANSI versions. Note that Unicode
support
on Windows Me/98/95 requires Microsoft Layer for Unicode.
Header: Declared in Winbase.h; include Windows.h.
Library: Use Kernel32.lib.
 
N

news

Thanks, thanks, thanks a lot!!!
:)))

Aravind C said:
Hi,

Actually, HICON* maps to IntPtr[].

So your modified PInvoke definition would be:

[DllImport("Shell32.Dll", CharSet = CharSet.Auto)]
public static extern uint ExtractIconEx(string lpszFile,
int nIconIndex, IntPtr[] phiconLarge, IntPtr[] phiconSmall, uint nIcons);

Here's how we invoke ExtractIconEx:

int iconCount = ExtractIconEx(path, -1, null, null, 0);
IntPtr[] smallIcon = new IntPtr[iconCount];
IntPtr[] largeIcon = new IntPtr[iconCount];
ExtractIconEx(path, 0, smallIcon, largeIcon, iconCount);


Regards,
Aravind C


Aravind please, look at this:
IconCount working well, but second calling throws this exception:
'The Win32 handle you passed to Icon is invalid or of the wrong type'
[DllImport("SHELL32.DLL")]
public static extern IntPtr ExtractIconEx(string lpszFile, int nIconIndex,
IntPtr phiconLarge, IntPtr phiconSmall, int nIcons);
private void ExtractIconsFromFile(string path, int iconIndex)
{
IntPtr smallIcon = IntPtr.Zero;
IntPtr largeIcon = IntPtr.Zero;
int iconCount = ExtractIconEx(path , -1, smallIcon, largeIcon
, -1).ToInt32();
if(iconIndex > iconCount)
{
//TODO: Default Icon
}
else
{
ExtractIconEx(path, iconIndex, smallIcon, largeIcon, 1);
m_SmallIcon = Icon.FromHandle(smallIcon);
m_LargeIcon = Icon.FromHandle(largeIcon);
}
}
Aravind C said:
Hi,

The unmanaged HICON datatype generally maps to the 'IntPtr' managed type.

Regards,
Aravind C

Hmm... What UnmanagedType is HICON???


HI David,

You would need to use PInvoke intorder to call Win32 APIs.
MSDN contains a few samples and tutorials on PInvoke. With that said,
please
find below the PInvoke definition for GetVolumeInformation

[DllImport("kernel32.dll", CharSet=CharSet.Auto)]
public static extern bool GetVolumeInformation(string lpRootPathName,
StringBuilder lpVolumeNameBuffer,
[MarshalAs(UnmanagedType.U4)] int nVolumeNameSize,
[MarshalAs(UnmanagedType.U4)] ref int lpVolumeSerialNumber,
[MarshalAs(UnmanagedType.U4)] ref int lpMaximumComponentLength,
[MarshalAs(UnmanagedType.U4)] ref int lpFileSystemFlags,
StringBuilder lpFileSystemNameBuffer,
[MarshalAs(UnmanagedType.U4)] int nFileSystemNameSize);

Regards,
Aravind C




How do i call in CSharp to this function:
GetVolumeInformation

The GetVolumeInformation function retrieves information about a file
system
and volume whose root directory is specified.


BOOL GetVolumeInformation(
LPCTSTR lpRootPathName,
LPTSTR lpVolumeNameBuffer,
DWORD nVolumeNameSize,
LPDWORD lpVolumeSerialNumber,
LPDWORD lpMaximumComponentLength,
LPDWORD lpFileSystemFlags,
LPTSTR lpFileSystemNameBuffer,
DWORD nFileSystemNameSize
);
Parameters
lpRootPathName
[in] Pointer to a string that contains the root directory of the
volume
to
be described. If this parameter is NULL, the root of the current
directory
is used. A trailing backslash is required. For example, you would
specify
\\MyServer\MyShare as \\MyServer\MyShare\, or the C drive as "C:\".
lpVolumeNameBuffer
[out] Pointer to a buffer that receives the name of the specified
volume.
nVolumeNameSize
[in] Length of the volume name buffer, in TCHARs. This parameter is
ignored if the volume name buffer is not supplied.
lpVolumeSerialNumber
[out] Pointer to a variable that receives the volume serial number.
This
parameter can be NULL if the serial number is not required.

Windows Me/98/95: If the queried volume is a network drive, the
serial
number will not be returned.


lpMaximumComponentLength
[out] Pointer to a variable that receives the maximum length, in
TCHARs,
of a file name component supported by the specified file system. A file
name
component is that portion of a file name between backslashes.
The value stored in variable pointed to by *lpMaximumComponentLength
is
used to indicate that long names are supported by the specified file
system.
For example, for a FAT file system supporting long names, the function
stores the value 255, rather than the previous 8.3 indicator. Long names
can
also be supported on systems that use the NTFS file system.

lpFileSystemFlags
[out] Pointer to a variable that receives flags associated with the
specified file system. This parameter can be one or more of the
following
flags; however, FS_FILE_COMPRESSION and FS_VOL_IS_COMPRESSED are
mutually
exclusive. Value Meaning
FILE_NAMED_STREAMS The file system supports named streams.
FILE_READ_ONLY_VOLUME The specified volume is read-only.

Windows 2000/NT and Windows Me/98/95: This value is not
supported.


FILE_SUPPORTS_OBJECT_IDS The file system supports object
identifiers.
FILE_SUPPORTS_REPARSE_POINTS The file system supports reparse
points.
FILE_SUPPORTS_SPARSE_FILES The file system supports sparse
files.
FILE_VOLUME_QUOTAS The file system supports disk quotas.
FS_CASE_IS_PRESERVED The file system preserves the case of file
names when it places a name on disk.
FS_CASE_SENSITIVE The file system supports case-sensitive file
names.
FS_FILE_COMPRESSION The file system supports file-based
compression.
FS_FILE_ENCRYPTION The file system supports the Encrypted File
System (EFS).
FS_PERSISTENT_ACLS The file system preserves and enforces ACLs.
For
example, NTFS preserves and enforces ACLs, and FAT does not.
FS_UNICODE_STORED_ON_DISK The file system supports Unicode in
file
names as they appear on disk.
FS_VOL_IS_COMPRESSED The specified volume is a compressed
volume;
for example, a DoubleSpace volume.

lpFileSystemNameBuffer
[out] Pointer to a buffer that receives the name of the file system
(such
as FAT or NTFS).
nFileSystemNameSize
[in] Length of the file system name buffer, in TCHARs. This parameter
is
ignored if the file system name buffer is not supplied.
Return Values
If all the requested information is retrieved, the return value is
nonzero.

If not all the requested information is retrieved, the return
value
is displaying
this
 
T

Ted Miller

Just curious why you used ref instead of out for a few of these params.
Also, why bother qualifying int's as UnmanagedType.U4? I would have started
with

[DllImport("kernel32.dll", CharSet=CharSet.Auto)]
public static extern bool GetVolumeInformation(
string lpRootPathName,
StringBuilder lpVolumeNameBuffer,
int nVolumeNameSize,
out uint lpVolumeSerialNumber,
out int lpMaximumComponentLength,
out int lpFileSystemFlags, // arguably, uint, or
better yet, create a [Flags] enum for the flags
StringBuilder lpFileSystemNameBuffer,
int nFileSystemNameSize
);

Is there some reason why this is incorrect?

Aravind C said:
HI David,

You would need to use PInvoke intorder to call Win32 APIs.
MSDN contains a few samples and tutorials on PInvoke. With that said, please
find below the PInvoke definition for GetVolumeInformation

[DllImport("kernel32.dll", CharSet=CharSet.Auto)]
public static extern bool GetVolumeInformation(string lpRootPathName,
StringBuilder lpVolumeNameBuffer,
[MarshalAs(UnmanagedType.U4)] int nVolumeNameSize,
[MarshalAs(UnmanagedType.U4)] ref int lpVolumeSerialNumber,
[MarshalAs(UnmanagedType.U4)] ref int lpMaximumComponentLength,
[MarshalAs(UnmanagedType.U4)] ref int lpFileSystemFlags,
StringBuilder lpFileSystemNameBuffer,
[MarshalAs(UnmanagedType.U4)] int nFileSystemNameSize);

Regards,
Aravind C


David Ichilov said:
How do i call in CSharp to this function:
GetVolumeInformation

The GetVolumeInformation function retrieves information about a file system
and volume whose root directory is specified.


BOOL GetVolumeInformation(
LPCTSTR lpRootPathName,
LPTSTR lpVolumeNameBuffer,
DWORD nVolumeNameSize,
LPDWORD lpVolumeSerialNumber,
LPDWORD lpMaximumComponentLength,
LPDWORD lpFileSystemFlags,
LPTSTR lpFileSystemNameBuffer,
DWORD nFileSystemNameSize
);
Parameters
lpRootPathName
[in] Pointer to a string that contains the root directory of the
volume
to
be described. If this parameter is NULL, the root of the current directory
is used. A trailing backslash is required. For example, you would specify
\\MyServer\MyShare as \\MyServer\MyShare\, or the C drive as "C:\".
lpVolumeNameBuffer
[out] Pointer to a buffer that receives the name of the specified volume.
nVolumeNameSize
[in] Length of the volume name buffer, in TCHARs. This parameter is
ignored if the volume name buffer is not supplied.
lpVolumeSerialNumber
[out] Pointer to a variable that receives the volume serial number. This
parameter can be NULL if the serial number is not required.

Windows Me/98/95: If the queried volume is a network drive, the serial
number will not be returned.


lpMaximumComponentLength
[out] Pointer to a variable that receives the maximum length, in TCHARs,
of a file name component supported by the specified file system. A file name
component is that portion of a file name between backslashes.
The value stored in variable pointed to by *lpMaximumComponentLength is
used to indicate that long names are supported by the specified file system.
For example, for a FAT file system supporting long names, the function
stores the value 255, rather than the previous 8.3 indicator. Long names can
also be supported on systems that use the NTFS file system.

lpFileSystemFlags
[out] Pointer to a variable that receives flags associated with the
specified file system. This parameter can be one or more of the following
flags; however, FS_FILE_COMPRESSION and FS_VOL_IS_COMPRESSED are mutually
exclusive. Value Meaning
FILE_NAMED_STREAMS The file system supports named streams.
FILE_READ_ONLY_VOLUME The specified volume is read-only.

Windows 2000/NT and Windows Me/98/95: This value is not supported.


FILE_SUPPORTS_OBJECT_IDS The file system supports object
identifiers.
FILE_SUPPORTS_REPARSE_POINTS The file system supports reparse
points.
FILE_SUPPORTS_SPARSE_FILES The file system supports sparse files.
FILE_VOLUME_QUOTAS The file system supports disk quotas.
FS_CASE_IS_PRESERVED The file system preserves the case of file
names when it places a name on disk.
FS_CASE_SENSITIVE The file system supports case-sensitive file
names.
FS_FILE_COMPRESSION The file system supports file-based compression.
FS_FILE_ENCRYPTION The file system supports the Encrypted File
System (EFS).
FS_PERSISTENT_ACLS The file system preserves and enforces ACLs. For
example, NTFS preserves and enforces ACLs, and FAT does not.
FS_UNICODE_STORED_ON_DISK The file system supports Unicode in file
names as they appear on disk.
FS_VOL_IS_COMPRESSED The specified volume is a compressed volume;
for example, a DoubleSpace volume.

lpFileSystemNameBuffer
[out] Pointer to a buffer that receives the name of the file system (such
as FAT or NTFS).
nFileSystemNameSize
[in] Length of the file system name buffer, in TCHARs. This parameter is
ignored if the file system name buffer is not supplied.
Return Values
If all the requested information is retrieved, the return value is nonzero.

If not all the requested information is retrieved, the return value is zero.
To get extended error information, call GetLastError.

Remarks
If you are attempting to obtain information about a floppy drive that does
not have a floppy disk or a CD-ROM drive that does not have a compact disc,
the system displays a message box asking the user to insert a floppy
disk
or
a compact disc, respectively. To prevent the system from displaying this
message box, call the SetErrorMode function with SEM_FAILCRITICALERRORS.

The FS_VOL_IS_COMPRESSED flag is the only indicator of volume-based
compression. The file system name is not altered to indicate compression.
This flag comes back set on a DoubleSpace volume, for example. With
volume-based compression, an entire volume is either compressed or not
compressed.

The FS_FILE_COMPRESSION flag indicates whether a file system supports
file-based compression. With file-based compression, individual files
can
be
compressed or not compressed.

The FS_FILE_COMPRESSION and FS_VOL_IS_COMPRESSED flags are mutually
exclusive; both bits cannot come back set.

The maximum component length value, stored in lpMaximumComponentLength, is
the only indicator that a volume supports longer-than-normal FAT (or other
file system) file names. The file system name is not altered to indicate
support for long file names.

The GetCompressedFileSize function obtains the compressed size of a file.
The GetFileAttributes function can determine whether an individual file is
compressed.



Windows Me/98/95: GetVolumeInformationW is supported by the Microsoft Layer
for Unicode. To use this, you must add certain files to your
application,
as
outlined in Microsoft Layer for Unicode on Windows 95/98/Me Systems.



Example Code
For an example, see Enumerating Mount Points.

Requirements
Client: Included in Windows XP, Windows 2000 Professional, Windows NT
Workstation, Windows Me, Windows 98, and Windows 95.
Server: Included in Windows Server 2003, Windows 2000 Server, and
Windows
NT
Server.
Unicode: Implemented as Unicode and ANSI versions. Note that Unicode support
on Windows Me/98/95 requires Microsoft Layer for Unicode.
Header: Declared in Winbase.h; include Windows.h.
Library: Use Kernel32.lib.
 
A

Aravind C

Hi Ted,

I just took some code from one of my example code snippets.
I guess the [out] might also work.
Will try that today.

Regards,
Aravind

Ted Miller said:
Just curious why you used ref instead of out for a few of these params.
Also, why bother qualifying int's as UnmanagedType.U4? I would have started
with

[DllImport("kernel32.dll", CharSet=CharSet.Auto)]
public static extern bool GetVolumeInformation(
string lpRootPathName,
StringBuilder lpVolumeNameBuffer,
int nVolumeNameSize,
out uint lpVolumeSerialNumber,
out int lpMaximumComponentLength,
out int lpFileSystemFlags, // arguably, uint, or
better yet, create a [Flags] enum for the flags
StringBuilder lpFileSystemNameBuffer,
int nFileSystemNameSize
);

Is there some reason why this is incorrect?

Aravind C said:
HI David,

You would need to use PInvoke intorder to call Win32 APIs.
MSDN contains a few samples and tutorials on PInvoke. With that said, please
find below the PInvoke definition for GetVolumeInformation

[DllImport("kernel32.dll", CharSet=CharSet.Auto)]
public static extern bool GetVolumeInformation(string lpRootPathName,
StringBuilder lpVolumeNameBuffer,
[MarshalAs(UnmanagedType.U4)] int nVolumeNameSize,
[MarshalAs(UnmanagedType.U4)] ref int lpVolumeSerialNumber,
[MarshalAs(UnmanagedType.U4)] ref int lpMaximumComponentLength,
[MarshalAs(UnmanagedType.U4)] ref int lpFileSystemFlags,
StringBuilder lpFileSystemNameBuffer,
[MarshalAs(UnmanagedType.U4)] int nFileSystemNameSize);

Regards,
Aravind C


David Ichilov said:
How do i call in CSharp to this function:
GetVolumeInformation

The GetVolumeInformation function retrieves information about a file system
and volume whose root directory is specified.


BOOL GetVolumeInformation(
LPCTSTR lpRootPathName,
LPTSTR lpVolumeNameBuffer,
DWORD nVolumeNameSize,
LPDWORD lpVolumeSerialNumber,
LPDWORD lpMaximumComponentLength,
LPDWORD lpFileSystemFlags,
LPTSTR lpFileSystemNameBuffer,
DWORD nFileSystemNameSize
);
Parameters
lpRootPathName
[in] Pointer to a string that contains the root directory of the
volume
to
be described. If this parameter is NULL, the root of the current directory
is used. A trailing backslash is required. For example, you would specify
\\MyServer\MyShare as \\MyServer\MyShare\, or the C drive as "C:\".
lpVolumeNameBuffer
[out] Pointer to a buffer that receives the name of the specified volume.
nVolumeNameSize
[in] Length of the volume name buffer, in TCHARs. This parameter is
ignored if the volume name buffer is not supplied.
lpVolumeSerialNumber
[out] Pointer to a variable that receives the volume serial number. This
parameter can be NULL if the serial number is not required.

Windows Me/98/95: If the queried volume is a network drive, the serial
number will not be returned.


lpMaximumComponentLength
[out] Pointer to a variable that receives the maximum length, in TCHARs,
of a file name component supported by the specified file system. A
file
name
component is that portion of a file name between backslashes.
The value stored in variable pointed to by *lpMaximumComponentLength is
used to indicate that long names are supported by the specified file system.
For example, for a FAT file system supporting long names, the function
stores the value 255, rather than the previous 8.3 indicator. Long
names
can
also be supported on systems that use the NTFS file system.

lpFileSystemFlags
[out] Pointer to a variable that receives flags associated with the
specified file system. This parameter can be one or more of the following
flags; however, FS_FILE_COMPRESSION and FS_VOL_IS_COMPRESSED are mutually
exclusive. Value Meaning
FILE_NAMED_STREAMS The file system supports named streams.
FILE_READ_ONLY_VOLUME The specified volume is read-only.

Windows 2000/NT and Windows Me/98/95: This value is not supported.


FILE_SUPPORTS_OBJECT_IDS The file system supports object
identifiers.
FILE_SUPPORTS_REPARSE_POINTS The file system supports reparse
points.
FILE_SUPPORTS_SPARSE_FILES The file system supports sparse files.
FILE_VOLUME_QUOTAS The file system supports disk quotas.
FS_CASE_IS_PRESERVED The file system preserves the case of file
names when it places a name on disk.
FS_CASE_SENSITIVE The file system supports case-sensitive file
names.
FS_FILE_COMPRESSION The file system supports file-based compression.
FS_FILE_ENCRYPTION The file system supports the Encrypted File
System (EFS).
FS_PERSISTENT_ACLS The file system preserves and enforces
ACLs.
For
example, NTFS preserves and enforces ACLs, and FAT does not.
FS_UNICODE_STORED_ON_DISK The file system supports Unicode in file
names as they appear on disk.
FS_VOL_IS_COMPRESSED The specified volume is a compressed volume;
for example, a DoubleSpace volume.

lpFileSystemNameBuffer
[out] Pointer to a buffer that receives the name of the file system (such
as FAT or NTFS).
nFileSystemNameSize
[in] Length of the file system name buffer, in TCHARs. This
parameter
is disk can
lpMaximumComponentLength,
file
is application, Windows
 

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