Re: Arrays in structures

Tech-Archive recommends: Fix windows errors by optimizing your registry

From: Willy Denoyette [MVP] (willy.denoyette_at_pandora.be)
Date: 03/01/04


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.



Relevant Pages

  • Re: FTP FtpFindFile
    ... public struct WIN32_FIND_DATA ... public int fileAttributes; ... public FILETIME lastAccessTime; ... public uint fileAttributes; ...
    (microsoft.public.dotnet.framework.compactframework)
  • MAPI problem
    ... MapiMessage message=new MapiMessage; ... public uint ulReserved = 0; ... public string lpszNoteText = string.Empty; ... public IntPtr lpOriginator = IntPtr.Zero; ...
    (microsoft.public.dotnet.framework.interop)
  • Re: FTP FtpFindFile
    ... Also as a struct you should be passing the find data by reference e.g. ... public uint fileAttributes; ... public FILETIME lastAccessTime; ...
    (microsoft.public.dotnet.framework.compactframework)
  • Re: 8 bit image
    ... > How do I instantiate an RGBQUAD? ... public struct BITMAPINFOHEADER ... public uint biCompression; ... BITMAPINFO bmi = new BITMAPINFO; ...
    (microsoft.public.dotnet.framework.drawing)
  • FieldOffset on 64bit OS
    ... public struct MOUSEINPUT ... public int dx; ... public uint mouseData; ... public ushort wScan; ...
    (microsoft.public.dotnet.languages.csharp)