Re: array of struct from c++ to c#

Tech-Archive recommends: Speed Up your PC by fixing your registry



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);

.



Relevant Pages

  • Re: nonhomogenous structs (was: lisp performance questions and observations)
    ... We programmers tend to view the instruction level of an architecture ... This is reinforced by the limited hardware courses we ... The conclusion is that in general, vectors of non-homogenous structs ... > naturally represented as an array of structs, ...
    (comp.lang.lisp)
  • Re: C# and C++ (again)
    ... The answer is to use classes, not structs. ... No need to break apart a portion of an array ... >> language where I will be most productive. ... it would be very nice to treat this 27 element array ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: * struct-like list *
    ... Age: 108 Birthday: 061095 SocialSecurity: 476892771999 ... I would like to have an array of "structs." ... I want to go through the file, filling up my list of structs. ... DOB: 061095 ...
    (comp.lang.python)
  • Re: Allocating structs on the stack
    ... > I am preparing for a class this fall that teaches C# programming. ... One big academic issue to me is that use of structs in ... > an array, in which case the rule seems to not be implemented). ... The compiler doesn't know whether an array element has been fully ...
    (microsoft.public.dotnet.languages.csharp)
  • array of struct from c++ to c#
    ... I currently have a single struct being passed from a C++ dll to a C# ... I'm now trying to pass an array of structs back to ... CeDbApi.LoadProcedure(ref proc); ...
    (microsoft.public.dotnet.framework.interop)