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

Interop with embedded array within C/C++ data structure

Hello all

I am looking for a way to retrieve data from the unmanaged code (legacy) DLL to the .NET code as described below. I could NOT find any articles that would help me to do so successfully. Please help

Unmanaged-Code DLL (LEGACY.DLL)
---------------------------

struct EMBEDDED_ARRAY_DATA_STRUCTUR

short * pValueArray
INT32 nElementCount
}

void WINAPI FillData(EMBEDDED_ARRAY_DATA_STRUCTURE * pData

pData->nElementCount = 5
pData->pValueArray = new short [pData->nElementCount]
for (short i = 0; i < pData->nElementCount; i++
pData->pValueArray[i] = i

Managed-Code
------------------

[StructLayout(LayoutKind.Sequential, CharSet=CharSet.Ansi)
public struct EMBEDDED_ARRAY_DATA_STRUCTUR

public IntPtr pValueArray
public Int32 nElementCount
}

public class MyLegac

...
[DllImport("legacy.dll", CharSet=Charset.Ansi)
public static extern void FillData(ref EMBEDDED_ARRAY_DATA_STRUCTURE pData)
...
}

...

EMBEDDED_ARRAY_DATA_STRUCTURE pData = new EMBEDDED_ARRAY_DATA_STRUCTURE
MyLegacy.FillData(ref pData)
short [] myShortValueArray = new short [pData->nElementCount]
for (int i = 0; i < pData->nElementCount; i++

myShortValueArray[i] = Marshal.ReadInt16(pData->pValueArray, (int) i)
// The problem is that the contents of myShortValueArray is bogus! Please point out what I've done wrong or if there is
// better way to do so....I am desperately needing help!!!!

Peter N

Nov 16 '05 #1
2 1597
Peter,
void WINAPI FillData(EMBEDDED_ARRAY_DATA_STRUCTURE * pData)
{
pData->nElementCount = 5;
pData->pValueArray = new short [pData->nElementCount];
You must provide a function to free the memory you allocate with new
in your C++ code, or you'll leak.

short [] myShortValueArray = new short [pData->nElementCount];
for (int i = 0; i < pData->nElementCount; i++)
{
myShortValueArray[i] = Marshal.ReadInt16(pData->pValueArray, (int) i);


ReadInt16 takes a byte offset from the pointer, not an element sized
offset. So the last parameter would have to be i * 2 (2 being
sizeof(short)).

But I'd recommend reading the entire array at once with Marshal.Copy
instead, it should perform better.

Mattias

--
Mattias Sjögren [MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.
Nov 16 '05 #2
Thank you, Mattias, for your suggestion. I will try it out

Peter N
----- Mattias Sjögren wrote: ----

Peter
void WINAPI FillData(EMBEDDED_ARRAY_DATA_STRUCTURE * pData

pData->nElementCount = 5
pData->pValueArray = new short [pData->nElementCount]
You must provide a function to free the memory you allocate with ne
in your C++ code, or you'll leak

short [] myShortValueArray = new short [pData->nElementCount]
for (int i = 0; i < pData->nElementCount; i++

myShortValueArray[i] = Marshal.ReadInt16(pData->pValueArray, (int) i)


ReadInt16 takes a byte offset from the pointer, not an element size
offset. So the last parameter would have to be i * 2 (2 bein
sizeof(short))

But I'd recommend reading the entire array at once with Marshal.Cop
instead, it should perform better

Mattia

--
Mattias Sjögren [MVP] mattias @ mvps.or
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.co
Please reply only to the newsgroup

Nov 16 '05 #3

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

Similar topics

2
by: Ross Micheals | last post by:
All I have some general .NET questions that I'm looking for some help with. Some of these questions (like the first) are ones that I've seen various conflicting information on, or questions that...
2
by: Jacob | last post by:
I have an interop question: I'm trying to get the ICONDIR Win32 struct which contains an array of ICONDIRENTRY structures to marshall correctly. The problem is that the array isn't a set amount...
4
by: Germic | last post by:
Hi, I need to pass a reference to a string between a C and C# library. I declare the string in C# function as ref String xyz and the corresponding C code would refer to it as BSTR *xyzz....
3
by: Keith M | last post by:
Could some kind soul illustrate how I can call the EnumPrintProcessors Win32 API from c# code? BOOL EnumPrintProcessors( LPTSTR pName, // print server name LPTSTR pEnvironment, ...
17
by: Aaron | last post by:
I've got a doozie of a problem! I and others have been trying to figure this out for too long and I've come to the conclusion that I should probably look for some support.. Ok, I have a COM...
104
by: Leszek | last post by:
Hi. Is it possible in javascript to operate on an array without knowing how mamy elements it has? What i want to do is sending an array to a script, and this script should add all values from...
0
by: kagorami | last post by:
Q: How do you return a LPSTR from unmanaged code? A: Use a StringBuilder. What about this: public struct CSTRUCT { string return_value; } How do you get a LPSTR from unmanaged code when it...
6
by: Nataraj1978 | last post by:
I have a requirement to use a dll written in Visual C++ 6.0 in my C# application. In order to establish the link between my application and the dll, I have written a ATL COM Component in Visual...
2
by: Fredo | last post by:
First of all, I apologize for cross-posting. I posted this on the framework.interop group as well, but haven't received a response yet, so I thought I'd try here... I'm trying to write an app...
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
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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?
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.