473,701 Members | 2,763 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 4420
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
1846
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
3537
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
1467
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
4677
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
2725
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
3413
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
2090
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
1550
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
1865
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
8738
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
8649
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
9233
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
8978
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
7829
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
6575
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
5905
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
4665
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
2399
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.