Paasing structs to/from C# via MC++ to C++ dll - How?

  • Thread starter Thread starter Keith M
  • Start date Start date
K

Keith M

Hi,

I am a bit of a newcomer to C# but have experience with (unmanaged) C++.

Now, I have a 3rd party dll and headers. This dll is a C++ style dll that
exports classes and structs.
I am attempting to write a Managed C++ wrapper dll for this dll in order
that it can be used from various C# modules in the overall project.
The managed C++ project can 'see' the definitions for the structs that the
lower functions use in the supplied headers.

I cannot reference these headers from the C# project (and have a feeling
that even if I could it would be a 'bad thing' to do so). So how do I
export wrapper structs from the middle tier managed c++ dll to the upper C#
level and how do I convert and pass them up and down the chain?

Hope this query makes sense. Many thanks
 
Keith,

It all depends on the structure actually. Depending on how complex it
is, perhaps you want to create a class representation (in managed code), and
then handle the conversion somewhere in the MC++ code. Can you show the
structure that you want to define/use?
 
Nicholas Paldino said:
It all depends on the structure actually. Depending on how complex it
is, perhaps you want to create a class representation (in managed code), and
then handle the conversion somewhere in the MC++ code. Can you show the
structure that you want to define/use?

Here is a typical one of about 10 that I have:

typedef struct _MYStruct{
LARGE_INTEGER CreationTime;
LARGE_INTEGER LastAccessTime;
LARGE_INTEGER LastWriteTime;
ULONG FileAttributes;
ULONG NumVersions;
ULONG NumFragments;
ULONG Flags;
USHORT ExtraHeaderSize;
USHORT ExtraFooterSize;
} MyStruct;

Any ideas welcome! What a wrapper class would look like and how to convert?
The LARGE_INTEGER member is actually a WIn32 defined structure or union so
how would I incorporate this?
I am thinking I will have to write a series of conversion functions, one for
each struct I want to use and simply do a member wise copy in the function.
As for the large int structure I think I will have to define my own similar
struct and use this in my new wrapper class.

Thanks
 
Keith,
What a wrapper class would look like and how to convert?

Something like

__value struct MyManagedStruct {
Int64 CreationTime;
Int64 LastAccessTime;
Int64 LastWriteTime;
UInt32 FileAttributes;
UInt32 NumVersions;
UInt32 NumFragments;
UInt32 Flags;
UInt16 ExtraHeaderSize;
UInt16 ExtraFooterSize;
};

The LARGE_INTEGER member is actually a WIn32 defined structure or union so
how would I incorporate this?

Work with the QuadPart member of the union, it's just a 64-bit
integer. You should be able to assign it to/from a System::Int64.

I am thinking I will have to write a series of conversion functions, one for
each struct I want to use and simply do a member wise copy in the function.

Yes, that will work. But for simple integer data like the struct
above, you could also just to a memcpy between the native and managed
structs.



Mattias
 
Back
Top