[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

login

6/8/2007 12:33:00 PM

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

4 Answers

Ben Voigt [C++ MVP]

6/9/2007 6:14:00 PM

0

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

<login@derimay.com> wrote in message
news:1181305992.082533.171910@p77g2000hsh.googlegroups.com...
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

6/10/2007 4:35:00 PM

0

On Jun 9, 8:14 pm, "Ben Voigt [C++ MVP]" <r...@nospam.nospam> wrote:
> 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;
>
> }
> };
> <l...@derimay.com> wrote in message
>
> news:1181305992.082533.171910@p77g2000hsh.googlegroups.com...
> 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

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

login

6/10/2007 6:29:00 PM

0

On Jun 9, 8:14 pm, "Ben Voigt [C++ MVP]" <r...@nospam.nospam> wrote:
> 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;
>
> }
> };
> <l...@derimay.com> wrote in message
>
> news:1181305992.082533.171910@p77g2000hsh.googlegroups.com...
> 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

I can also not to find the file <msclr.h>...

Ben Voigt [C++ MVP]

6/12/2007 5:23:00 AM

0

> 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.