473,387 Members | 1,624 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.

How to return array of structs from unmanaged DLL to C#?

Mates,
the problem is pretty simple, but I'm unable to manage it... Therefore
asking you for help.

Situation:
Have managed code in C# and unamanged code in C++ DLL.
I need to make a call from C# to a function exported by the DLL.
Moreover, I have following struct (in C# and C++, as well):
struct myStruct {
unsigned int val1;
unsigned int val2;
}

What I need:
The C# function declares two empty variables (one for the array itself and
second one for the number of items returned in the array) and passes them
using "ref" keyword to C++ function. The C++ function determines the number
of array elements that will be returned, dynamically allocates memory,
creates the array of structs and passes it (along with the size of the array
itself) to the C# function.

Assumptions:
- C# code (the calling one) doesn't know the number of items in the array
that will be returned
- C# must NOT use unsafe code
- the number of items in the array may vary, but will always be >0

I know that Marshalling/Serialization will be required, but I can't find the
right code. Below, you can see my current C++ and C# code. I'm able to return
and marshal only 1 element. Any ideas?

// C++ code - currently returns only 1 element
struct test {
unsigned int val1;
unsigned int val2;
};

extern "C" __declspec(dllexport) void Test(test** pArray, int* pSize)
{
*pArray = (test*) CoTaskMemAlloc(sizeof(test) * 1);
for (unsigned int i = 0; i < 1; i++)
{
(*pArray + i)->accountID = i;
(*pArray + i)->period = i+1;
(*pArray + i)->cycle = i+2;
(*pArray + i)->cellType = i+3;
}
*pSize = 1;
}

// C# code
[StructLayout(LayoutKind.Sequential)]
public class clTest
{
public uint val1;
public uint val2;
}

[DllImport("somedll.dll")]
public static extern void Test(out IntPtr tst, ref int pSize);

IntPtr buffer;
int size = 0;

Test(out buffer, ref size);
if (size 0)
{
clTest arr = new clTest();
Marshal.PtrToStructure(buffer, arr );
Marshal.FreeCoTaskMem(buffer);
}
Sep 24 '08 #1
1 4839
pls, anyone?

"Darkriser" wrote:
Mates,
the problem is pretty simple, but I'm unable to manage it... Therefore
asking you for help.

Situation:
Have managed code in C# and unamanged code in C++ DLL.
I need to make a call from C# to a function exported by the DLL.
Moreover, I have following struct (in C# and C++, as well):
struct myStruct {
unsigned int val1;
unsigned int val2;
}

What I need:
The C# function declares two empty variables (one for the array itself and
second one for the number of items returned in the array) and passes them
using "ref" keyword to C++ function. The C++ function determines the number
of array elements that will be returned, dynamically allocates memory,
creates the array of structs and passes it (along with the size of the array
itself) to the C# function.

Assumptions:
- C# code (the calling one) doesn't know the number of items in the array
that will be returned
- C# must NOT use unsafe code
- the number of items in the array may vary, but will always be >0

I know that Marshalling/Serialization will be required, but I can't find the
right code. Below, you can see my current C++ and C# code. I'm able to return
and marshal only 1 element. Any ideas?

// C++ code - currently returns only 1 element
struct test {
unsigned int val1;
unsigned int val2;
};

extern "C" __declspec(dllexport) void Test(test** pArray, int* pSize)
{
*pArray = (test*) CoTaskMemAlloc(sizeof(test) * 1);
for (unsigned int i = 0; i < 1; i++)
{
(*pArray + i)->accountID = i;
(*pArray + i)->period = i+1;
(*pArray + i)->cycle = i+2;
(*pArray + i)->cellType = i+3;
}
*pSize = 1;
}

// C# code
[StructLayout(LayoutKind.Sequential)]
public class clTest
{
public uint val1;
public uint val2;
}

[DllImport("somedll.dll")]
public static extern void Test(out IntPtr tst, ref int pSize);

IntPtr buffer;
int size = 0;

Test(out buffer, ref size);
if (size 0)
{
clTest arr = new clTest();
Marshal.PtrToStructure(buffer, arr );
Marshal.FreeCoTaskMem(buffer);
}
Sep 29 '08 #2

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

Similar topics

1
by: Richard A. Lowe | last post by:
I'm successfully using SendInput to emulate mouse events for a legacy app, delcared like so: public extern static int SendInput( int theCount, ref INPUT pInputs, int theSize ); From the...
8
by: Mas L via DotNetMonster.com | last post by:
Hi, I have a c++ source code which I can compile to be a DLL (in VS.NET 2003). And I need to use it in a C# program. After I compiled/build the C++ code to a DLL, I add it as a Reference in my...
4
by: ned | last post by:
I have been having a lot of problems with an array of structures embedded in another structure in C++ .NET and have boiled it down to this very simple example: Try the following steps, it will...
7
by: heddy | last post by:
I have an array of objects. When I use Array.Resize<T>(ref Object,int Newsize); and the newsize is smaller then what the array was previously, are the resources allocated to the objects that are...
6
by: DaTurk | last post by:
Hi, I'm coding a layer into my application using CLI, between a unmanaged and a c# layer. So, I have to marshal some unmanaged c++ structures to structures usable in c#. My solution was to...
13
by: =?Utf-8?B?U2hhcm9u?= | last post by:
I have the following managed C++ function (VC++ 2005 (C++/CLI) System::Array^ ManagedCppClass::GetData() { BYTE* pData; int len; m_pureNativeCPPObj->GetData(pData, len); // Get the data buffer...
6
by: =?Utf-8?B?QWxleGFuZGVyZmU=?= | last post by:
Hi, I have a C# program that uses an unmanaged dll that has a function similar to the signature below : void f(out MyStruct arr, out int num); // num = actual array length returned The array...
2
by: jonpb | last post by:
Using .NET 3.5, I need to pass an array of structs as parameter to a C++ unmanaged function. The C++ dll stores some data in an unmanaged cache, the function writes the values into the array of...
0
by: mjaaland | last post by:
Hi! I've been working with DLLimports passing structs and various other parameters to unmanaged code. I had problems earlier sending pointer to structs to the unmanaged code, and this forum solved...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
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
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
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.