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

Home Posts Topics Members FAQ

Passing a struct between managed and unmanged code

2 New Member
I have create a .Net component which exposes an interface with two methods. The first method takes an array of structs as a parameter the other method returns an array of (the same tyoes of) structs.

When I get the array structures from my second method in unmanaged c++, I cannot regonize any of the parameters in the structures. But if I pass the same pointer to the first method in my interface and look at the values in .net then thay are all intact.

Can anyone tell my how to read these values in unmanaged c++?

Code and more explanations:
The regasm of the .net code gives an interface in the .tlh file like this:
HRESULT Input( SAFEARRAY * calibRecords );
SAFEARRAY * Output( );
and my stryct:
MyRecord { double r; };

In c++ i get the dispatch interface to the library and then do a queryInterface for my interface like this:

ITool *p;
HRESULT hr = pDisp->QueryInterface (&p);
SAFEARRAY *psa = p->Output();

I know there are 2 elements in the array, so I do the following:
MyRecord *cc=new MyRecord[2];

hr = SafeArrayAccess Data(psa, reinterpret_cas t<void**>(&cc)) ;

But ther values in cc is NOT the correct ones.
If I then do a hr = p->Input(psa); and looks at the values in the .net application then all looks fine.

Please help me...
Oct 22 '07 #1
3 4426
weaknessforcats
9,208 Recognized Expert Moderator Expert
*p;
HRESULT hr = pDisp->QueryInterface (&p);
SAFEARRAY *psa = p->Output();

I know there are 2 elements in the array, so I do the following:
MyRecord *cc=new MyRecord[2];

hr = SafeArrayAccess Data(psa, reinterpret_cas t<void**>(&cc)) ;
First, SafeArrayAccess Data() requires a SAFEARRAY* obtained from SafeArrayCreate (). I do not know what p->Output() is doing.

Second, SafeArrayAccess Data() needs the address of a pointer since it will allocate the SAFEARRAY and need to return its address to you. So this code:
MyRecord *cc=new MyRecord[2];
probably need to be:
Expand|Select|Wrap|Line Numbers
  1. SAFEARRAY* arr;
  2.  
and the call need to look like:
Expand|Select|Wrap|Line Numbers
  1. hr = SafeArrayAccessData(psa, &arr);
  2.  
Third, you can't have a SAFEARRAY of MyRecord structs. The elements of a SAFEARRAY are limited to specific types. In your case you need a SAFEARRAY of VARIANT where the variant contains the double. You might be able to pass a MyRecord by reference in a VARIANT and use IRecordInfo to parse it.
Oct 22 '07 #2
cskarp
2 New Member
Hi,

Thanks for your answer.

I know that when you creates a SAFEARRAY you have to call SafeArrayCreate () before you can use it. But in this context .net returns a SAFEARRAY in p->Output(), which I suppose is initialized and filled with the data from my .net component. I the .net component I create and fill an array of classes - which contains only data. So in .net the interface is "MyRecord[] Output();"

The AccessData you are describing is also what I have done and afterwards the data is not what I expect.

I really didn't knew that I could not pass a SAFEARRAY of MyRecord between the components. I know that I could create a SAFEARRAY of doubles but I have simplyfied the MyRecord here, normally it contains a lot of other data and therefor I cannot use an array of doubles.
Oct 23 '07 #3
weaknessforcats
9,208 Recognized Expert Moderator Expert
You can have your MyRecord in a VARIANT as IRecordInfo. Then you create your SAFEARRAY as an array of VARIANT.

Check [url=http://msdn2.microsoft .com/en-us/library/ms221627.aspx]this[\url].
And [url=http://msdn2.microsoft .com/en-us/library/ms221482.aspx]this[\url] where is mentions using FADF_VARIANT
Oct 23 '07 #4

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

Similar topics

1
1850
by: codymanix | last post by:
i want to pass a struct by reference to a function in a c++ dll. the problem is that the call causes an ArgumentException in the calling c# code. i already tried to use "out" instead of "ref" there was no exception generated but i saw in the unmanaged code all fields of the struct were uninitialized! this is my c# code:
1
3552
by: lolomgwtf | last post by:
I have a managed C++ method that wraps unmanaged code and creates a managed object holding data retrieved form an unmanged one. I want create an instance of this managed class in C#, pass it to this method and have it set the instance to hold the right data. >From what I've read it seems I should be able to pass C# objects to managed C++ methods and it should just work; however, when I try it, my C# instance comes out null. If I step...
3
1470
by: Germic | last post by:
Hi, I want to create an Hashtable in C# and pass an pointer to the hashtable to a C code. Later, the C code could reference the hashtable by passing the pointer to the hashtable as a ref parameter. what is the correct way to do this? Thanks,
2
4682
by: lolomgwtf | last post by:
I have a managed C++ method that wraps unmanaged code and creates a managed object holding data retrieved form an unmanged one. I want create an instance of this managed class in C#, pass it to this method and have it set the instance to hold the right data. >From what I've read it seems I should be able to pass C# objects to managed C++ methods and it should just work; however, when I try it, my C# instance comes out null. If I step...
2
2728
by: Sandy | last post by:
I am confused about Unmanaged Code, How .Net Framework treate that code, What is the use of that. Thanks in advance Sandeep Chitode
5
3418
by: Maxwell | last post by:
Hello, Newbie question here. I have a VS.NET 2003 MC++ (not C++/cli) project where I have a managed class reference in a unmanaged class...simple enough. To keep things short I am for the most part attempting to do what is this article by Nish: http://www.voidnish.com/articles/ShowArticle.aspx?code=cbwijw I have to hook up a unmanaged callback to a managed method using IJW NOT P\Invoke. So I am employing this "Thunk" or "Bridge" class...
1
2091
by: Russell Mangel | last post by:
Sorry about the Cross-Post, I posted my question in the wrong group. Hello, What is the simplest way to create a dynamic collection (during run-time), using basic C (Struct data types). Since I am doing C++/CLI interop I wish to avoid using vector class. I am using Visual Studio 2005 C++/CLI, and I am writing an Un-Managed class library. The project type will be a static library (possibly a dll). I have
6
1552
by: B. | last post by:
my small project has two files, umg.cpp (unmanaged c++) and mged.cpp (MC++), and unmanged code will call managed code. However, I cannot debug from unmanged code into managed code. Can anyone help me to solve the problem?
0
1868
by: ElysianEagle | last post by:
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;
0
9633
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
10305
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...
0
10137
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
9928
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
8959
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...
0
5503
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4037
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
3632
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2867
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.