Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / .NET Framework / Interop / June 2007

Tip: Looking for answers? Try searching our database.

COM and Marshal Problem

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
login@derimay.com - 08 Jun 2007 13:33 GMT
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
Ben Voigt [C++ MVP] - 09 Jun 2007 19:14 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
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>...

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.