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

Tip: Looking for answers? Try searching our database.

Declaring a pointer to be passed by reference

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Steve Terepin - 14 Jan 2004 16:13 GMT
I've got a Managed C++ class that's being called from C#, and the C# code
needs to look like this :

byte * p = myManagedCppClassInstance.AllocateDmaBuffer(1000) ;
// Do various things, then ...
myManagedCppClassInstance.FreeDmaBuffer(ref p) ;
// The 'free' routine has set 'p' to zero

The problem is with the declaration of 'FreeDmaBuffer' in the Managed C++
class. In an attempt to make the pointer appear as a 'ref' variable, I've
tried the following :

void FreeDmaBuffer ( unsigned char * & p )
{
 // Free the resource, then ...
 p = 0 ;
}

and also

void FreeDmaBuffer (
 [System::Runtime::InteropServices::InAttribute]
 [System::Runtime::InteropServices::OutAttribute]
 unsigned char * p // Also tried & p here ...
) {
 p = 0 ;
}

But in all cases, the C# compiler complains about a type mismatch.

I'm sure this must be possible - does anyone know how ?

Thanks,

Steve.
Mattias Sj?gren - 15 Jan 2004 11:55 GMT
Steve,

>void FreeDmaBuffer ( unsigned char * & p )

void FreeDmaBuffer ( unsigned char * __gc& p )

or

void FreeDmaBuffer ( System::Byte * & p )

should do it.

Mattias

Signature

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

Steve Terepin - 15 Jan 2004 22:54 GMT
That's just the trick - many thanks !  There's a general lesson for me here
: always use the System::XXX types in the argument lists, instead of the
native C++ types ?  I'd been under the impression that 'unsigned char' was
absolutely equivalent to 'System::Byte', but clearly they aren't the same
...

Best regards,

Steve.

> Steve,
>
[quoted text clipped - 9 lines]
>
> Mattias
Mattias Sj?gren - 17 Jan 2004 12:54 GMT
Steve,

>There's a general lesson for me here
>:always use the System::XXX types in the argument lists, instead of the
>native C++ types ?

That, or remember to use the __gc keyword when needed.

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.