Re: array of struct from c++ to c#
- From: The Real Andy <will_get_back_to_you_on_This@>
- Date: Wed, 22 Mar 2006 21:17:57 +1000
On 21 Mar 2006 12:51:48 -0800, scottelloco@xxxxxxxxx wrote:
Hi,
I currently have a single struct being passed from a C++ dll to a C#
app by reference. I'm now trying to pass an array of structs back to
the C# app using the same technique, but I am receiving an
NotSupportedException when trying to do so. All of the posts I have
seen so far are passing back the array of structs as an IntPtr and then
marshalling each member individually. Is it possible to pass an array
of structs without using an IntPtr and instead passing the entire array
of structs by ref or do I need to start looking into using an IntPtr?
Below I have posted how I am passing a single struct (which works) and
my (failing) attempt to pass an array of structs.
There is an excellent article in MSDN that covers this topic.
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpguide/html/cpconoutarrayofstructssample.asp
One thing that is concerning is that your C++ dll provides no array
size, just a pointer to the array. You will need to modify the dll
call to also give you the length of the array. Without this you have
no way of knowing if you have exceeded the bounds of the array. I am
not sure how the marshaller handles the situation where you have
iterated outside the bounds of the array, but perhaps you could try
this and report back here when you know.
Thanks for any assistance, -Scott
Pass single struct from C++ to C# (works):
C++
------
struct tblProcedure
{
unsigned long ProcedureId;
unsigned long InstitutionId;
wchar_t FName[56];
wchar_t LName[56];
}
CEDBXML_API bool LoadProcedure(tblProcedure* procedure[])
{
procedure->ProcedureId = 810;
procedure->InstitutionId = 2002;
wcscpy(procedure->FName, "Michael");
wcscpy(procedure->LName, "Houser");
}
C#
----
[DllImport("CedbXml", CharSet = CharSet.Auto)]
public static extern bool LoadProcedure(ref
CEDBProcedures.tblProcedure Procs);
CEDBProcedures.tblProcedure proc = new
CEDBProcedures.tblProcedure;
CeDbApi.LoadProcedure(ref proc);
Pass array of structs from C++ to C# (fails with a
NotSupportedException):
C++
------
struct tblProcedure
{
unsigned long ProcedureId;
unsigned long InstitutionId;
wchar_t FName[56];
wchar_t LName[56];
}
CEDBXML_API bool LoadProcedure(tblProcedure* procedure[])
{
procedure[0]->ProcedureId = 810;
procedure[0]->InstitutionId = 2002;
wcscpy(procedure[0]->FName, "Michael");
wcscpy(procedure[0]->LName, "Houser");
procedure[1]->ProcedureId = 811;
procedure[1]->InstitutionId = 1985;
wcscpy(procedure[1]->FName, "John");
wcscpy(procedure[1]->LName, "Bell");
}
C#
----
[DllImport("CedbXml", CharSet = CharSet.Auto)]
public static extern bool LoadProcedure(ref
CEDBProcedures.tblProcedure[] Procs);
CEDBProcedures.tblProcedure[] proc = new
CEDBProcedures.tblProcedure[100];
CeDbApi.LoadProcedure(ref proc);
.
- Follow-Ups:
- Re: array of struct from c++ to c#
- From: scottelloco
- Re: array of struct from c++ to c#
- From: scottelloco
- Re: array of struct from c++ to c#
- References:
- array of struct from c++ to c#
- From: scottelloco
- array of struct from c++ to c#
- Prev by Date: Re: COM interop clarification
- Next by Date: problem converting act.tlb into act.dll using tlbimp - "self-registration failed"
- Previous by thread: array of struct from c++ to c#
- Next by thread: Re: array of struct from c++ to c#
- Index(es):
Relevant Pages
|