How to access std::vector in C Sharp

D

DhaneshNair

I have created a COM server in which a collection of vectors need to
be exposed to C# sharp client .

Each vector contains a array of structures and this vector need to be
embedded inside a container, say another vector or list.

Code:
typedef struct
{
std::string name;
std::string type;

} STAGE_INFO;

std::vector<STAGE_INFO> m_stages;

m_stages.push_back(current_stage)



Since STL classes are not accessible in C#, I used a SAFEARRAY and
VARIANT for exposing them. But I am not successful because of the
following reason .

1) I am not able to create a VARIANT array of structures.
2) I created a SAFEARRAY of structures and included it in a VARIANT,
but when accessed in C# as Object, I am not able to get the structure.
Though I am able to typecast it into a Object array , but if accessed
in separte gives exception.
3) I am able to access the members of the structure as they are string
but not a STRUCTURE as a whole.
4) I tried containing a VARIANT of SAFEARRAY'S in another VARIANT, but
it gives a casting error.
5) CComVariant cannot have USD's and VARIANT if used has some problem
of Memory Leaks.

Please let me know if you have expertise in COM and C#.Net
Interoperability.
or
Even if you have some alternate solution for this problem?

Thanks In Advance .. expecting your earliest reply.
_________________
Life Rocks,

$dhanu's$
 
N

Nicholas Paldino [.NET/C# MVP]

See inline:

I have created a COM server in which a collection of vectors need to
be exposed to C# sharp client .

Each vector contains a array of structures and this vector need to be
embedded inside a container, say another vector or list.

Code:
typedef struct
{
std::string name;
std::string type;

} STAGE_INFO;

std::vector<STAGE_INFO> m_stages;

m_stages.push_back(current_stage)



Since STL classes are not accessible in C#, I used a SAFEARRAY and
VARIANT for exposing them. But I am not successful because of the
following reason .

1) I am not able to create a VARIANT array of structures.

Well, you are, but you have to export the structure through COM. For
this, you are better off defining the structure in your type library with
the following IDL:

[uuid(81FE33F8-1F84-47e4-97BF-24B0C29278B4)]
struct PriceFeedSheetInstrumentKeyColumn
{
BSTR Name;
BSTR Type;
};

You will want to change the GUID in the uuid attribute. When you change
the IDL this way, the compiler should pick up on it and generate the header
files/structure declarations that you can place into a SAFEARRAY and marshal
through COM.

You will then want to use these structures when passing them outside of
your COM object.
2) I created a SAFEARRAY of structures and included it in a VARIANT,
but when accessed in C# as Object, I am not able to get the structure.
Though I am able to typecast it into a Object array , but if accessed
in separte gives exception.

Did you define the structure in C#? If so, how?
3) I am able to access the members of the structure as they are string
but not a STRUCTURE as a whole.

How are you doing this? You indicate that you are not able to get the
structure above, if you can't get the structure, then how are you getting
the members of it.
4) I tried containing a VARIANT of SAFEARRAY'S in another VARIANT, but
it gives a casting error.
5) CComVariant cannot have USD's and VARIANT if used has some problem
of Memory Leaks.

Using variants is just going to give you one layer of indirection that
you don't need.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top