Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to use StructLayoutAttribute and MarshalAsAttribute

Status
Not open for further replies.

VenkatRastapuram

Programmer
Oct 24, 2003
46
IN
Hello Sir,

I am sending a sample code which is used to search for a file by importing kernel32.dll. But in the sample code it is unable to comile. It is giving two compilation errors saying that StructLayoutAttribute does not contain a definition for CharSet and another one is the type or namespace name MarshalAs could not be found. How to solve this problem.

using System;
using System.Text;
using System.Runtime.InteropServices;

// Declares a class member for structure element.
[ StructLayout( LayoutKind.Sequential, CharSet=CharSet.Auto )]

public class FindData
{
public int fileAttributes = 0;
// creationTime was an embedded FILETIME structure.
public int creationTime_lowDateTime = 0 ;
public int creationTime_highDateTime = 0;
// lastAccessTime was an embedded FILETIME structure.
public int lastAccessTime_lowDateTime = 0;
public int lastAccessTime_highDateTime = 0;
// lastWriteTime was an embedded FILETIME structure.
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;
}


public class LibWrap

{

// Declares a managed prototype for the unmanaged function.
[DllImport( "Kernel32.dll", CharSet=CharSet.Auto )]
public static extern IntPtr FindFirstFile( String fileName, [ In, Out ]
FindData findFileData );


[ DllImport( "Kernel32.dll", CharSet=CharSet.Auto )]

public static extern int FindNextFile( IntPtr handle, ref FindData

findFileData );

}

Thanking U

Regards,
R.Venatesh
MakeLogic
venkat@makelogicmldb.com
 
I did copy/paste of your code and it is working.
Maybe there is something when you use that code.
I did like here:
Code:
private void btnFindFile_Click(object sender, System.EventArgs e)
{   	string strFileName="C:\\aup.txt";
	FindData fd = new FindData();
	IntPtr h =LibWrap.FindFirstFile (strFileName,fd);
	if (h != IntPtr.Zero)
	{
		LibWrap.FindNextFile(h, ref fd);
		//FindData fd2 = (FindData)Marshal.PtrToStructure(h,fd.GetType());
	}
	if (fd.fileName.Length >0)
	{
		System.Windows.Forms.MessageBox.Show("Found file " +fd.fileName);
	}
	else
	{
		System.Windows.Forms.MessageBox.Show("File doesn't exist");
	}

}
obislavu
 
Hi Obislavu ,

Thank you for your response.

I think u have selecte the application as a windows application. In the windows application it is working properly but when I copy the same code in the Smart device application it is giving comilation errors. So please tell me how to invoke the methods in the smart device application.

Thanking U

Regards,
R.Venkatesh
MakeLogic
venkat@makelogicmldb.com
 
Sorry, I don't know why is not working on the Smart device.
obislavu
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top