You're right, the IDL doesn't have marshalling support. It's designed for
in-process use only, where marshalling isn't an issue... or so everyone
thought, until .NET made it an issue.
You can definitely make a C++/CLI wrapper for it, something like:
#include <msclr.h>
#include <windows.h>
public ref class IaIfWrapper
{
public:
static int GetSomething( IaIf* that, int numRows, [Out] cli::array<double>^
pos, [Out] cli::array<int>^ reflections )
{
pos = gcnew cli::array<double>[numRows];
reflections = gcnew cli::array<int>[numRows];
int result;
pin_ptr<double> pinPos = &pos[0];
pin_ptr<int> pinRefl = &reflections[0];
HRESULT error;
if (FAILED(error = that->getSomething(numRows, pinPos, pinRefl,
&result)))
throw gcnew Exception(); // decode error if you want
return result;
}
};
Hi,
I am certainly not an expert to C# and COM/Marshal.
I cannot manage how to handle this problem:
I want to call the following COM function from a C# Programm:
IDL Description: (Simplified)
interface IaIf : IDispatch
{
...
[id(20), helpstring("")] HRESULT getSomething([in] int numRows, [out]
double* pos, [out] int* reflections, [out, retval] int* result);
...
};
C Definition: (Simplified)
STDMETHODIMP CMyClass::getSomething(int numRows, double *pos, int
*reflections, int *result)
{
*result = IO_OK;
for(int i=0;i<numRows;i++)
{
pos[posIndx] = someDouble1;
pos[posIndx+1] = someDouble2;
pos[posIndx+2] = someDouble3;
reflections[i] = someInt;
}
return S_OK;
}
Basically, this method fills to arrays (double and int) with some
data.
The problem is that the vendor made an error in the idl in my opinion.
If you call this from C#, you get ONE double and ONE integer back
instead of getting an array of doubles and integers or at least a
pointer to them.
(It should have been [in] double*, [in] int* instead maybe??)
Since I cannot ask the vendor to correct this, does anyone could help
me in solving this issue (Marshaling?, C++ Wrapper)
I just cannot manage to solve it myself.
Thanks a lot!
Régis
login@derimay.com - 10 Jun 2007 17:34 GMT
> You're right, the IDL doesn't have marshalling support. It's designed for
> in-process use only, where marshalling isn't an issue... or so everyone
[quoted text clipped - 75 lines]
>
> Régis
Thanks so much! I will try that straight forward!
Can I add this as part of my C# project? or do I have to create a new
project? What's the type of project to create then?
Thanks a lot!
Régis
Ben Voigt [C++ MVP] - 12 Jun 2007 06:22 GMT
> Thanks so much! I will try that straight forward!
> Can I add this as part of my C# project? or do I have to create a new
> project? What's the type of project to create then?
Visual C++ / CLR / Class Library
Then add it to your C# project as a reference, by using the Project tab in
the Add Reference dialog.
login@derimay.com - 10 Jun 2007 19:28 GMT
> You're right, the IDL doesn't have marshalling support. It's designed for
> in-process use only, where marshalling isn't an issue... or so everyone
[quoted text clipped - 75 lines]
>
> Régis
I can also not to find the file <msclr.h>...