473,779 Members | 2,053 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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

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

--
KM

Nov 15 '05 #1
3 1608
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 [.NET/C# MVP]
- mv*@spam.guard. caspershouse.co m

"Keith M" <ke***********@ nospam.amsjv.co m> wrote in message
news:3f******** @baen1673807.gr eenlnk.net...
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

--
KM


Nov 15 '05 #2

"Nicholas Paldino [.NET/C# MVP]" <mv*@spam.guard .caspershouse.c om> wrote in
message

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
Nov 15 '05 #3
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

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

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

Similar topics

26
448
by: codymanix | last post by:
Last night I had several thought about RAII and want to discuss a bit. Why doesn't CSharp support destructors in structs? Wouldn't that make RAII possible like in C++? When the struct goes out of scope, the dtor could be immediately be called (no GC needed). For that, you don't have to declare the whole File class as a struct (which would be not good for performance when File has a lot of data-members). Instead creating a thin wrapper...
19
2127
by: Jasper Kent | last post by:
Can anyone explain the logic behind structs being allowed neither memeber initialisers or default constructors. Doesn't this just encourage developers to create constructors with dummy arguments? Thanks in advance, Jasper Kent.
4
3148
by: MSDousti | last post by:
Hi all, I read some Q&As in the Net, discussing Marshalization of nested structs in C# (or VB.NET). Some guys stated that .NET framework does not support this feature yet. Are they right? BTW, by nested structs I mean structures such as this:
19
1980
by: hamil | last post by:
I have a form with one button, Button1, and a Textbox, Textbox1 I have a class, class1 as follows. Public Class Class1 Public DeForm As Object Sub doit() DeForm.Textbox1.text = "It works" End Sub End Class
7
19257
by: James Johnson | last post by:
Are there structs in JavaScript? If not, what's the closest thing, or do I just use parallel arrays? I'm populating a JavaScript array from ColdFusion query, but I don't think I can do this: var location = new Array() <cfloop query="loc"> location.name = '#loc.name#' location.address1 = '#loc.address1#'
36
2667
by: Roman Mashak | last post by:
Hello, All! I implemented simple program to eliminate entry from the file having the following structure (actually it's config file of 'named' DNS package for those who care and know): options { directory "/var/named"; listen-on { 192.168.11.22; 127.0.0.1; }; forwarders { 168.126.63.1; };
61
3782
by: Marty | last post by:
I am new to C# and to structs so this could be easy or just not possible. I have a struct defined called Branch If I use Branch myBranch = new Branch(i); // everything works If I use Branch (myBranch + x) = new Branch(i); // it doesn't x is a loop iterator, i is an int for the constructor to define an array. What am I doing wrong here.
2
2926
by: dachrist28 | last post by:
I am trying to populate a listbox with data that is pulled from a sql query. Here is my C# code behind the page: sqlConn1.Open(); SqlCommand sqlComm2 = new SqlCommand("SElECT replace((mc.MAINTENT.id + space(20-len(mc.maintent.id))+ left(mc.MAINTENT.aenm,50) + space(50-len(left(mc.maintent.aenm,50)))+mc.mtntype.id),' ','&nbsp') as Description, mc.maintent.mtnoi FROM mc.MAINTENT INNER JOIN mc.MTNTYPE ON mc.MAINTENT.mtctyp_oi =...
5
1785
by: mc | last post by:
I have a function (shown below) that I call to check if a server is present. when a server is present, everything is ok. however when it fails. I get an SocketException fired. On my development machine all seems to work and the exception is fired and caught. however on my production server the exception fires, it's not caught by the try catch and it's also not caught in my application_error handler in Global.asax. Can anyone shed any light...
0
9471
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10136
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9925
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8958
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7478
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6723
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5501
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4036
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3631
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.