Click here to Skip to main content
15,917,608 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i Want to pinvoke a c struct to c#
my c struct is

 typedef struct
{
	u64 Parent;					// index in the folders list
	u64 FileSize;					// file size
	u64 Delta;					// delta offset on drive
	FILETIME Modified;					// modified time
	FILETIME Created;					// created time
	FILETIME Accessed;					// accessed time
	u16 *NameExtStr;					// name of file
	u16 *PartitionStr;				// partition or drive name
	u16 *CommentStr;		// if not null then comment about file state
	size_t OverwrittenIndex;	// index of file that overwritten this file
	u64 LostCluster[LOST_COUNT];		// overwritten clusters #
	u64 LostClusters;					// lost clusters count
	u64 TotalClusters;					// total file clusters
	u8 Exists:1;						// file existence flag
	u8 State:5;						// state of file
	u16 Hidden:1;
	u8 FoundByDeepScan:1;			// tells if file found by deep scan
	u8 Attr;							// attribute
}
FileInfoStruct;

and my c code

 typedef void *myHandle;
// returns file info for one of found files found, Idx - index of file of interest
BOOL GetFileInfoNew(myHandle Undelete, size_t Idx, FileInfoStruct **FileInfo, FileDetailsStruct *FileDetails)
{
	UndeleteStruct *UndeletePtr = (UndeleteStruct *)Undelete;
	
	

	*FileInfo = &UndeletePtr->Search.FilesList.FilesPtr[Idx].fi;						// return file description
	FileDetails[0].FileSize=(*(*FileInfo)).FileSize;

	FileDetails[0].ParentID = (*(*FileInfo)).Parent;

	wcscpy(FileDetails[0].FileName, (*(*FileInfo)).NameExtStr);

	wcscpy(FileDetails[0].PathName,(*(*FileInfo)).PartitionStr);

	
	return TRUE;
}


plz help

What I have tried:

my c# struct is

[StructLayout(LayoutKind.Sequential)]
    public struct FileInfoStruct
    {

        public ulong Parent;
        public ulong FileSize;
        public ulong Delta;
        public FILETIME Modified;
        public FILETIME Created;
        public FILETIME Accessed;
        public IntPtr NameExtStr;                    // name of file
        public IntPtr PartitionStr;                  // partition or drive name
        public IntPtr CommentStr;         // if not null then comment about file state
        public int OverwrittenIndex;
        public ulong[] LostCluster;        // overwritten clusters #
        public ulong TotalClusters;            //u64 LostClusters;                   
        public int Exists;                                                           
        public int State;                                                               					
        public int Hidden;  					
        public int FoundByDeepScan; 
        public int Attr;
        
    }

my c# code

GetFileInfoNew(IntPtr Undelete, uint Idx, ref  FileInfoStruct FileInfo, ref FileDetailsStruct FileDetails)
Posted
Updated 20-Jan-20 3:01am
Comments
Richard MacCutchan 20-Jan-20 8:33am    
What is the question?
Member 14087451 20-Jan-20 8:36am    
sir iam facing problem to pinvoke c code to c#, my struct remain null, it is working properly in c but iam not getting value in c#
Patrice T 20-Jan-20 8:45am    
Use Improve question to update your question.
So that everyone can pay attention to this information.

1 solution

C++
BOOL GetFileInfoNew(myHandle Undelete, size_t Idx, FileInfoStruct **FileInfo, FileDetailsStruct *FileDetails)

I do not think that using a ref keyword in C# leads to double indirection in C.
 
Share this answer
 
Comments
Member 14087451 20-Jan-20 23:49pm    
you are right i changed ref FileInfoStruct to ref Intptr solved my problem thanks :)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900