473,387 Members | 1,711 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Passing a struct between managed and unmanged code

2
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 = SafeArrayAccessData(psa, reinterpret_cast<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 4404
weaknessforcats
9,208 Expert Mod 8TB
*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 = SafeArrayAccessData(psa, reinterpret_cast<void**>(&cc));
First, SafeArrayAccessData() requires a SAFEARRAY* obtained from SafeArrayCreate(). I do not know what p->Output() is doing.

Second, SafeArrayAccessData() 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
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 Expert Mod 8TB
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
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"...
1
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...
3
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...
2
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...
2
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
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...
1
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...
6
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...
0
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...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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,...
0
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...
0
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,...

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.