473,657 Members | 2,659 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

C# Interop - passing structure from UNmanaged to managed code

1 New Member
Hi,

I have a DLL written in C that creates a structure as a local variable, populates it, and then calls a function in managed c#. here is what im talking about:

//-----unmanged code's header file

typedef struct M
{
int a;
int b;
int c;
} MO;

typedef void (*MyCallBack)(M O m)
void register_callba ck(MyCallBack);

//------unmanged code's .c file

void register_callba ck(MyCallBack mcb)
{
MO co;
co.a = 67;

mcb(co);

}


-------------managed code (in rough, for brevity)

//struct MO
{
int a;
int b;
int c;
}


delegate void MyDelegate(MO m);

MyDelegate mdg;

mdg += delegate (MO m) {
MessageBox.Show (m.a.ToString() );
}

register_callba ck(mdg);
---------------------------------------------

when I run this code, the managed function does get called by the unmanaged dll code, and the message box pops up showing '67', however after that my program crashes. does anyone know why??

thanks!!
Jun 27 '07 #1
0 1861

Sign in to post your reply or Sign up for a free account.

Similar topics

1
947
by: JC | last post by:
How does garbage collection work in C# and VB.NET for data returned from COM object? For example, a COM object written in C++ returns a SAFEARRAY to C# or VB.NET, will this SAFEARRAY (mapped to type in C# or VB.NET) be garbage collected? Similarily, if C# or VB.NET pass SAFEARRAY to the COM object via COM method invocation, will this SAFEARRAY be garbage collected after the call? My guess to answers for both questions is yes. Thanks.
1
2117
by: Eric Twietmeyer | last post by:
Hello, I'm starting to investigate cs, managed c++ and interoperating with a very large unmanaged code base. We are going to use Windows Forms (written in cs) to replace our old fashioned GUI. The guts will remain in unmanaged c++. So I need to write public managed c++ wrapper classes for the couple of interfaces the unmanaged stuff needs to expose for the GUI. Questions:
1
2285
by: Nadav | last post by:
Hi, Introduction: ********************* I am writing a mixed mode application I have a COM module and a .NET module that communicate with each other. The COM exposes a custom sink interface, the .NET module implement the Sink interface ( IUnknown based ) and the COM call the methods of this interface asynchronously.
0
1697
by: nygiantswin2005 | last post by:
I would like to know how do I pass a pointer to a struct from managed code to unmanaged code. For example if I create structure like this in managed code. StructLayout( LayoutKind.Sequential, CharSet=CharSet.Ansi ) ] public struct MYSTRUCT { public String Text;
3
3500
by: zhphust | last post by:
I want to convert a object of a managed class to a unmanaged structure that has the same member with that managed class. Can anybody tell me how i can do it? Thanks in advance. -- zhphust ------------------------------------------------------------------------
3
2160
by: ajtaylor | last post by:
Hello, I wonder if anyone can help lift the cloud of confusion hanging over me at the moment ;-) I have a load of unmanaged code in a project that I am compiling NOT USING /CLR I have some managed code and I want to call some of the functions in the unmanaged code. So I create a file with mixed C++ and C++/CLI code
5
1894
by: Yoavo | last post by:
Hi, In my application I have to use som external COM dll's. In the code: using MyDll when I build the project, I get (in addition to the .exe file) a Dll called Interop.MyDll.dll I want to send the .exe to the users without needing to send the dll. Is it possible to build my project without the accompanying dll ?
5
3269
by: John | last post by:
I would like to ask a question that is obvious to all people porting applications from the "traditional" C\VB6 interop scheme choosing C# vs VB.NET. We have a math library in C which ubiquitously takes a float* which represents an array of floats. If the memory is allocated by the client, in C# or VB.NET, I assume it must be "fixed" so that the GC does not move the pointer during the C algorithm. Given that we are choosing between C#...
0
1157
by: user2008 | last post by:
Hi everybody, I'm working on exposing a .Net (C#) COM Server. Manage Code is exposting an interface with two methods. These methods take an IN/OUT Struct as parameter. Requirement is that the unmanaged client (C++) will initialize the struct fields with some values and these struct values can be modified or reallocated by Managed COM Server. Struct look something like this on the managed side: public struct MyStruct ...
0
8385
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8821
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
8502
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8602
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...
1
6162
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
5632
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
4300
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2726
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
1601
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.