Re: Arrays in structures
From: Willy Denoyette [MVP] (willy.denoyette_at_pandora.be)
Date: 03/01/04
- Next message: Marco Liedekerken: "Could not stop C# Windows Service with Server Explorer"
- Previous message: Jon Skeet [C# MVP]: "Re: how to creat a single zip file out of two files (zipping and unzipping functionality)"
- In reply to: Peter Seaman: "Re: Arrays in structures"
- Next in thread: Peter Seaman: "Re: Arrays in structures"
- Reply: Peter Seaman: "Re: Arrays in structures"
- Messages sorted by: [ date ] [ thread ]
Date: Mon, 1 Mar 2004 14:54:09 +0100
"Peter Seaman" <Peter MS Seaman at StableSoftware.com> wrote in message
news:uInofV2$DHA.692@TK2MSFTNGP11.phx.gbl...
>> Well, the first thing to do is test whether it is still possible in
>> *managed* C++ - and then run ildasm on the created assembly and see
>> what's in it.
>
> No doubt this is good advice but unfortunately I only have C# Standard.
> Though this product does contain Visual Studio 2003 and compilers for
> JScript .NET and VB .NET, it does not allow me to compile C++ .NET as far
> as
> I can see.
>
> It seems to me that arrays and structures and combinations of such
> aggregates are pretty fundamental to programming, so on the face of this
> inability for structures to contain embedded arrays is a severe
> restriction.
> Is there an easy way round it? How does C# cope with the many such
> structures in the WIN32 API e.g. LPWIN32_FIND_DATA?
>
> Peter Seaman
>
>
>
This is not such a problem in C#, following is a sample using FindNexttFile
and WIN32_FIND_DATA.
using System;
using System.Runtime.InteropServices;
namespace Willys
{
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)]
public struct _WIN32_FIND_DATAW
{
public uint dwFileAttributes;
public FILETIME ftCreationTime;
public FILETIME ftLastAccessTime;
public FILETIME ftLastWriteTime;
public uint nFileSizeHigh;
public uint nFileSizeLow;
public uint dwReserved0;
public uint dwReserved1;
// TCHAR array 260 (MAX_PATH) entries, 520 bytes in unicode
[MarshalAs(UnmanagedType.ByValTStr, SizeConst=520)]
public string cFileName;
// TCHAR array 14 TCHAR's alternate filename 28 byes in unicode
[MarshalAs(UnmanagedType.ByValTStr, SizeConst=28)]
public string cAlternateFileName;
}
[StructLayout(LayoutKind.Sequential)]
public struct FILETIME
{
public uint dwLowDateTime;
public uint dwHighDateTime;
}
class Tester
{
[DllImport("kernel32.dll", EntryPoint="FindFirstFileW", SetLastError=true,
CharSet = CharSet.Unicode)]
public static extern IntPtr FindFirstFileW( string lpFileName, ref
_WIN32_FIND_DATAW lpFindFileData);
static void Main()
{
string file = "*.exe";
_WIN32_FIND_DATAW fndData = new _WIN32_FIND_DATAW();
IntPtr hFile = FindFirstFileW(file, ref fndData);
if(hFile.ToInt32() == -1)
Console.WriteLine("FindFirst: " + Marshal.GetLastWin32Error());
else
Console.WriteLine("Filename: " + fndData.cFileName_260);
}
}
}
Willy.
- Next message: Marco Liedekerken: "Could not stop C# Windows Service with Server Explorer"
- Previous message: Jon Skeet [C# MVP]: "Re: how to creat a single zip file out of two files (zipping and unzipping functionality)"
- In reply to: Peter Seaman: "Re: Arrays in structures"
- Next in thread: Peter Seaman: "Re: Arrays in structures"
- Reply: Peter Seaman: "Re: Arrays in structures"
- Messages sorted by: [ date ] [ thread ]
Relevant Pages
|