473,408 Members | 1,876 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,408 software developers and data experts.

Marshalling WIN32_FIND_DATA

Does anyone have a good C# marshalling for the WIN32_FIND_DATA structure used
with API functions FindFirstFile, FindNextFile, FtpFindFirstFile and
InternetFindNextFile?

Have modeled as follows

[StructLayout(LayoutKind.Sequential)]
public class FILETIME
{
public int dwLowDateTime;
public int dwHighDateTime;
};
[StructLayout(LayoutKind.Sequential)]
public class WIN32_FIND_DATA
{
public const int MAX_PATH = 260;
public int dwFileAttributes;
public FILETIME ftCreationTime;
public FILETIME ftLastAccessTime;
public FILETIME ftLastWriteTime;
public int nFileSizeHigh;
public int nFileSizeLow;
public int dwReserved0;
public int dwReserved1;
[MarshalAs(UnmanagedType.LPStr, SizeConst=MAX_PATH)]
public string cFileName;
[MarshalAs(UnmanagedType.LPStr, SizeConst=14)]
public string cAlternateFileName;
};

[DllImport("kernel32.dll", CharSet=CharSet.Auto)]
public static extern int FindFirstFile(string szSearchFile,
[MarshalAs(UnmanagedType.LPStruct)] WIN32_FIND_DATA lpFindFileData)

Calls to FindFirstFile, FindNextFile etc. produce a good handle but no data
in the WIN32_FIND_DATA structure.
Nov 16 '05 #1
3 8022
Does anyone have a good C# marshalling for the WIN32_FIND_DATA structure used
with API functions FindFirstFile, FindNextFile, FtpFindFirstFile and
InternetFindNextFile?
Try www.pinvoke.net

[StructLayout(LayoutKind.Sequential)]
public class FILETIME
{
public int dwLowDateTime;
public int dwHighDateTime;
};
FILETIME is already available as a struct in the
System.Runtime.InteropServices namespace. So no need to redeclare it,
expecially not as a class.

[StructLayout(LayoutKind.Sequential)]
public class WIN32_FIND_DATA
Since the function is set to use CharSet.Auto, so should this type.

[DllImport("kernel32.dll", CharSet=CharSet.Auto)]
public static extern int FindFirstFile(string szSearchFile,
[MarshalAs(UnmanagedType.LPStruct)] WIN32_FIND_DATA lpFindFileData)

Calls to FindFirstFile, FindNextFile etc. produce a good handle but no data
in the WIN32_FIND_DATA structure.


Try getting rid of the MarshalAs(UnmanagedType.LPStruct) attribute,
and add the [In, Out] attributes instead.

Mattias

--
Mattias Sjögren [MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.
Nov 16 '05 #2

[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Auto)]
private struct FILETIME
{
public uint dwLowDateTime;
public uint dwHighDateTime;
}

[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Auto)]
private struct SYSTEMTIME
{
public short wYear;
public short wMonth;
public short wDayOfWeek;
public short wDay;
public short wHour;
public short wMinute;
public short wSecond;
public short wMilliseconds;
}

private const int MAX_PATH = 260;
private const int INVALID_HANDLE_VALUE = -1;

[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Auto)]
private struct WIN32_FIND_DATA
{
public uint dwFileAttributes;
public FILETIME ftCreationTime;
public FILETIME ftLastAccessTime;
public FILETIME ftLastWriteTime;
public uint nFileSizeHigh;
public uint nFileSizeLow;
public uint dwReserved0;
public uint dwReserved1;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = MAX_PATH)]
public string cFileName;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 14)]
public string cAlternateFileName;
}
--
Klaus H. Probst, MVP
http://www.vbbox.com/

"BobT" <Bo**@discussions.microsoft.com> wrote in message
news:CF**********************************@microsof t.com...
Does anyone have a good C# marshalling for the WIN32_FIND_DATA structure used with API functions FindFirstFile, FindNextFile, FtpFindFirstFile and
InternetFindNextFile?

Have modeled as follows

[StructLayout(LayoutKind.Sequential)]
public class FILETIME
{
public int dwLowDateTime;
public int dwHighDateTime;
};
[StructLayout(LayoutKind.Sequential)]
public class WIN32_FIND_DATA
{
public const int MAX_PATH = 260;
public int dwFileAttributes;
public FILETIME ftCreationTime;
public FILETIME ftLastAccessTime;
public FILETIME ftLastWriteTime;
public int nFileSizeHigh;
public int nFileSizeLow;
public int dwReserved0;
public int dwReserved1;
[MarshalAs(UnmanagedType.LPStr, SizeConst=MAX_PATH)]
public string cFileName;
[MarshalAs(UnmanagedType.LPStr, SizeConst=14)]
public string cAlternateFileName;
};

[DllImport("kernel32.dll", CharSet=CharSet.Auto)]
public static extern int FindFirstFile(string szSearchFile,
[MarshalAs(UnmanagedType.LPStruct)] WIN32_FIND_DATA lpFindFileData)

Calls to FindFirstFile, FindNextFile etc. produce a good handle but no data in the WIN32_FIND_DATA structure.

Nov 16 '05 #3
"BobT" <Bo**@discussions.microsoft.com> wrote in message
news:CF**********************************@microsof t.com...
Does anyone have a good C# marshalling for the WIN32_FIND_DATA structure
used
with API functions FindFirstFile, FindNextFile, FtpFindFirstFile and
InternetFindNextFile?

Have modeled as follows
....snip...
Calls to FindFirstFile, FindNextFile etc. produce a good handle but no
data
in the WIN32_FIND_DATA structure.


This works for me...

// Declare a class member for it
[ StructLayout( LayoutKind.Sequential, CharSet=CharSet.Auto )]
private class FindData
{
public int fileAttributes = 0;
// creationTime was a FILETIME struct.
public int creationTime_lowDateTime = 0 ;
public int creationTime_highDateTime = 0;
// lastAccessTime was a FILETIME struct.
public int lastAccessTime_lowDateTime = 0;
public int lastAccessTime_highDateTime = 0;
// lastWriteTime was a FILETIME struct.
public int lastWriteTime_lowDateTime = 0;
public int lastWriteTime_highDateTime = 0;
public int nFileSizeHigh = 0;
public int nFileSizeLow = 0;
public int dwReserved0 = 0;
public int dwReserved1 = 0;
[ MarshalAs( UnmanagedType.ByValTStr, SizeConst=256 )]
public String fileName = null;
[ MarshalAs( UnmanagedType.ByValTStr, SizeConst=14 )]
public String alternateFileName = null;
}

Hope this helps,

-Per
Nov 16 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Animesh | last post by:
Hi All, I don't know whethher this is possible or not. This is the result of a bad design problem. Here I go; I have a structure like this: typedef struct _s_index_entry { char *doc_id;...
2
by: Howard Kaikow | last post by:
I've got the following in a VB 6 project: Private Type PROCESSENTRY32 dwSize As Long cntUsage As Long th32ProcessID As Long th32DefaultHeapID As Long th32ModuleID As Long cntThreads As Long...
0
by: swartzbill2000 | last post by:
I am familiar with the VB6/VC6/ATL way of marshalling an incoming interface via New (VB), and then marshalling an outgoing interface with some form of Advise. To me it looks like .Net has...
2
by: bonk | last post by:
I am currently trying to write a longer article about Marshalling when using . Does anyone know books, articles, or websites that cover Marshalling in Platform Invocation Services ()?
2
by: RJ Lohan | last post by:
Howdy, I have a legacy DLL for which I have a problem marshalling a parameter type of char**. The function header (C++) is as so; extern "C" __declspec(dllexport) int __stdcall...
2
by: calenlas | last post by:
Hi all, I'm taking my first steps into C# <--C++ DLL Interop and unfortunately I've run into (what seems to be) a very complicated case as my first task. Perhaps someone here can help me. I...
2
by: d-42 | last post by:
Hi, I'm pretty sure I've just got a Marshalling problem, but I'm completely stumped. If there is a better newsgroup to post this in, please point me towards it. First I'm trying to use...
1
by: d-42 | last post by:
Hi, I'm pretty sure I've just got a Marshalling problem, but I'm completely stumped. If there is a better newsgroup to post this in, please point me towards it. First I'm trying to use...
0
by: santoshdarekar | last post by:
HI, I have some code which I am reverse engineering. Here is one module which is used as calculation madule and marshalling is used in it. But, still there is performance issue as the CPU time the...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.