pass a c# array to a c++ com lib (newB)

M

madmike

hey,

first thanks for looking. I think this should be easy. I have a com
DLL (in C++) that has this struct:


[export]
struct IdxTimestampedWord {
BSTR word;
__int64 timestamp;
};

and this function:

__interface ISFClass : IUnknown
{
[helpstring("method DoThing")]
HRESULT DoThing( [in, size_is(TranscriptArraySize)] IdxWord
TranscriptWord[], [out,
size_is(TranscriptArraySize)]IdxTimestampedWord AlignedTranscript[]);
};


I added a referance to the DLL in my c# project and I have this
code...

MyNameSpace.IdxTimestampedWord[] obSrc = null;
MyNameSpace.IdxTimestampedWord[] obDst = null;


// fill obSrc

// call the function

MyNameSpace.MyClass myC= new MyNameSpace.MyClass();

myC.doThing(ref obSrc, out obDst)


and I get:

cannot convert from 'ref MyNameSpace.IdxTimestampedWord[]' to 'ref
MyNameSpace.IdxTimestampedWord'



So my question is what to I need to do to send the array I made in C#
to the COM object?

thanks,
mike
 
M

madmike

Ok.

so this is what I did

i ran tlbimp, then ildasm like it told me too, and edited

this:
..method public hidebysig newslot virtual
instance void DoThing( [in] valuetype Alignment.IdxWord& TranscriptWord,
[out] valuetype Alignment.IdxTimestampedWord&
AlignedTranscript) runtime managed internalcall

to this:

..method public hidebysig newslot virtual
instance void DoThing([in] valuetype Alignment.IdxWord[] marshal([])
TranscriptWord,
[out] valuetype Alignment.IdxTimestampedWord[]
marshal ([]) AlignedTranscript
) runtime managed internalcall


** note when I ran with this DLL it failed to load my class, so I also edited
.method public hidebysig newslot virtual abstract to reflect the same as above.

then I ran ilsam with the /DLL option (not in doc) which made a new dll



I call the funciton from c# like this


Alignment.IdxTimestampedWord[] obIN = new Alignment.IdxTimestampedWord[10];
Alignment.IdxTimestampedWord[] obOut = new Alignment.IdxTimestampedWord[10];

DoThing(obIn,obOut);


And I get System.NullReferenceException. Did I do something wrong?
 
M

Mattias Sjögren

** note when I ran with this DLL it failed to load my class, so I also edited
.method public hidebysig newslot virtual abstract to reflect the same as above.

Yep, you have to change all occurances of the method.

And I get System.NullReferenceException. Did I do something wrong?

Not that I can see, it looks correct to me. Sorry, I'm not sure what
causes the exception. I'll do some testing and see if I can reproduce
it.



Mattias
 

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