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 / December 2004

Tip: Looking for answers? Try searching our database.

passing a reference C# to C++

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Ian Lazarus - 16 Dec 2004 18:06 GMT
Hello,
How do I get a C# file to call a managed C++ class method whose argument is
a reference to a basic type? I get a C# compile time error message saying
that it can't convert a reference to a pointer. The C++ assembly shows the
parameter as a pointer. Why does it do that?
Thanks

// C++ file
public __gc class MyCPPClass
{
public:
   void foo(short& x) { }
};

// C# file
class MyCSClass
{
   public static void Main()
   {
       MyCPPClass x = new MyCPPClass();
       short y = 0;
       x.fref(ref y);
       // error CS1503: Argument '1': cannot convert from 'ref short' to
short*'
   }
}
Mattias Sj?gren - 16 Dec 2004 19:31 GMT
Ian,

>How do I get a C# file to call a managed C++ class method whose argument is
>a reference to a basic type?

You either change the C++ code to make the parameter a ref parameter
as it's known in C#:

void foo(short __gc& x) { }

or

void foo(System::Int16& x) { }

or you keep the C++ code as it is, enable unsafe code in C# and call
it like this

unsafe {
       x.fref(&y);
}

Mattias

Signature

Mattias Sjögren [MVP]  mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.

Ian Lazarus - 17 Dec 2004 03:37 GMT
Thanks for the help. It works, but I don't understand why.

For
   void foo(short __gc& x)
why doesn't that mean that the arg must be on the managed heap, as opposed
to the local heap?

For
   void foo(System::Int16& x)
isn't System::Int16 a structure? How can a reference to a structure be
equivalent to a reference to a basic type? Is it doing some implicit
marshaling going in and out of the foo()? I didn't see any implicit
conversion operator members in System::Int16.
Mattias Sj?gren - 18 Dec 2004 11:57 GMT
Ian,

>For
>    void foo(short __gc& x)
>why doesn't that mean that the arg must be on the managed heap, as opposed
>to the local heap?

__gc means slightly different things in different contexts. Here it
just makes it explicit that you want a managed reference.

>For
>    void foo(System::Int16& x)
>isn't System::Int16 a structure? How can a reference to a structure be
>equivalent to a reference to a basic type?

Int16 is really the same as short int, except that the compiler knows
for sure that you're using it as a managed type, so pointers and
references to it are implicitly __gc.

Chapters 7 and 8 of the MC++ specification cover __gc pointers and
references in detail.

http://msdn.microsoft.com/library/en-us/vcmxspec/html/vcManagedExtensionsSpec_7.asp

The folks in microsoft.public.dotnet.languages.vc can probably explain
all this better than I can.

Mattias

Signature

Mattias Sjögren [MVP]  mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.


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.