wrapping...

G

Guest

Hi there.
In c++ Managed, is there a way to seamless wrap an unmanaged struct in a
managed class without having to write access code for all the memebers?

ie..

typedef struct
{
char Name[50];
long int MagicNumber;
} teststruct;

than I want to write a class

class Test
{
....
public teststruct *mystruct;
}


and then in c#

Test myclass;
myclass.mystruct.MagicNumber = 10;


Thanks in advance
 
W

Willy Denoyette [MVP]

"CristianMori"
Hi there.
In c++ Managed, is there a way to seamless wrap an unmanaged struct in a
managed class without having to write access code for all the memebers?

ie..

typedef struct
{
char Name[50];
long int MagicNumber;
} teststruct;

than I want to write a class

class Test
{
....
public teststruct *mystruct;
}


and then in c#

Test myclass;
myclass.mystruct.MagicNumber = 10;


Thanks in advance

Yes, provided Test is a managed class, which it is not in your sample.

Willy.
 
G

Guest

Cristian, The following "struct" is the interface used by the platform invoke
code when dealing with FtpFindFirstFile provided by wininet.dll. I think it
will give you the clues you are looking for...Chuck

/// <summary>
//STRUCTURE to hold the directory information - implemented as a class
/// <summary>
[StructLayout(LayoutKind.Sequential, CharSet=CharSet.Auto)]
public class FileData {
public int fileAttributes = 0;
// creationTime was embedded FILETIME structure
public int creationTime_lowDateTime = 0 ;
public int creationTime_highDateTime = 0;
// lastAccessTime was embedded FILETIME structure
public int lastAccessTime_lowDateTime = 0;
public int lastAccessTime_highDateTime = 0;
// lastWriteTime was 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;
} //end FileData
 
W

Willy Denoyette [MVP]

This doesn't answer the question, your "struct" is a C# declaration, the OP
is talking about C++ and managed C++

Willy.

chuck rudolph said:
Cristian, The following "struct" is the interface used by the platform
invoke
code when dealing with FtpFindFirstFile provided by wininet.dll. I think
it
will give you the clues you are looking for...Chuck

/// <summary>
//STRUCTURE to hold the directory information - implemented as a class
/// <summary>
[StructLayout(LayoutKind.Sequential, CharSet=CharSet.Auto)]
public class FileData {
public int fileAttributes = 0;
// creationTime was embedded FILETIME structure
public int creationTime_lowDateTime = 0 ;
public int creationTime_highDateTime = 0;
// lastAccessTime was embedded FILETIME structure
public int lastAccessTime_lowDateTime = 0;
public int lastAccessTime_highDateTime = 0;
// lastWriteTime was 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;
} //end FileData


CristianMori said:
Hi there.
In c++ Managed, is there a way to seamless wrap an unmanaged struct in a
managed class without having to write access code for all the memebers?

ie..

typedef struct
{
char Name[50];
long int MagicNumber;
} teststruct;

than I want to write a class

class Test
{
....
public teststruct *mystruct;
}


and then in c#

Test myclass;
myclass.mystruct.MagicNumber = 10;


Thanks in advance
 
G

Guest

Indeed!
My struct is in a .h file that must be shared between an unmanaged project
and a managed one.
So I need to wrap it up seamleassy and I don't want to rwrite any access
code, so that if I jange something in the .h file of the struct I don't need
to add any code to the managed ones to make the application work again!

Thanks

Willy Denoyette said:
This doesn't answer the question, your "struct" is a C# declaration, the OP
is talking about C++ and managed C++

Willy.

chuck rudolph said:
Cristian, The following "struct" is the interface used by the platform
invoke
code when dealing with FtpFindFirstFile provided by wininet.dll. I think
it
will give you the clues you are looking for...Chuck

/// <summary>
//STRUCTURE to hold the directory information - implemented as a class
/// <summary>
[StructLayout(LayoutKind.Sequential, CharSet=CharSet.Auto)]
public class FileData {
public int fileAttributes = 0;
// creationTime was embedded FILETIME structure
public int creationTime_lowDateTime = 0 ;
public int creationTime_highDateTime = 0;
// lastAccessTime was embedded FILETIME structure
public int lastAccessTime_lowDateTime = 0;
public int lastAccessTime_highDateTime = 0;
// lastWriteTime was 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;
} //end FileData


CristianMori said:
Hi there.
In c++ Managed, is there a way to seamless wrap an unmanaged struct in a
managed class without having to write access code for all the memebers?

ie..

typedef struct
{
char Name[50];
long int MagicNumber;
} teststruct;

than I want to write a class

class Test
{
....
public teststruct *mystruct;
}


and then in c#

Test myclass;
myclass.mystruct.MagicNumber = 10;


Thanks in advance
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top