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

Tip: Looking for answers? Try searching our database.

passing buffer to unmanaged dll function

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Pascal Cloup - 02 Apr 2004 11:48 GMT
Hello,

How to (in C#)  create and send a buffer (of short) to an unmanaged dll
function declared in C like this:

status = theFunction (..., short *buffer,  ....)

Are the 2 following methods good?
the first:
[DllImport("theDll.dll")]

public static extern short theFunction( ... , Systeem.IntPtr    buffer ,
... );

....

Systeem.IntPtr    buffer = Marshal.AllocHGlobal( buffSize );

theFunction( ... , buffer, ... );

the second:

[DllImport("theDll.dll")]
public static extern short theFunction( ... , [MarshalAs(
UnmanagedType.LPArray)]short[] buffer , ...);
...

short[]    buffer = new short[buffSize ];

theFunction( ... , buffer, ... );

Thanks for helpingg

Pascal Cloup
"Ying-Shen Yu[MSFT]" - 05 Apr 2004 07:43 GMT
Hi Pascal,

Basically for an array of short, I suggest you using the second method,
since in the first method you need allocate and release the unmanaged heap
by your self.
Using the 2nd method, CLR will managed the resource, it can even optimize
the marshalling by pinning the short array object  in the managed heap
instead of copying it to unmanaged heap first. For more information on this
issue, you may take a look at docuement:
<Blittable and Non-Blittable Types>
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpguide/htm
l/cpconBlittableNon-BlittableTypes.asp

Feel free to let me know if you have any thing unclear about this issue.

Have a nice day!

Ying-Shen Yu [MSFT]
Microsoft community Support
Get Secure! - www.microsoft.com/security

This posting is provided "AS IS" with no warranties and confers no rights.
This mail should not be replied directly, please remove the word "online"
before sending mail.
Pascal Cloup - 05 Apr 2004 09:20 GMT
Hi Ying-Shen,

Thanks for your advices.

The different tries which i made show that with the second method all works
fine, this is not the case with the first (?).
It remains one doubt in my mind; all the functions in these dll generally
returns immediatly but continues to use the buffer that was tansmmitted as
argument.
What happens if the .net moves the buffer for whatever reason that it is?
Is it necessary and possible to "lock " the buffer?

Thanks again

Pascal Cloup

> Hi Pascal,
>
[quoted text clipped - 6 lines]
> issue, you may take a look at docuement:
> <Blittable and Non-Blittable Types>

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpguide/htm
> l/cpconBlittableNon-BlittableTypes.asp
>
[quoted text clipped - 9 lines]
> This mail should not be replied directly, please remove the word "online"
> before sending mail.
"Ying-Shen Yu[MSFT]" - 06 Apr 2004 02:58 GMT
Hi Pascal,

In this situation, you need keep a reference to the short array using
GCHandle.Alloc method with Pinned enum. The Pinned enum will prevent the GC
from moving the object. Note, you need use GCHandle.Free method to remove
this reference when you finish using it.

In addition, if there are many pinned arrays in your application, maybe
it's better to use the 1st solution, since many Pinned objects will cause
fragmentation on managed heap which will undermine the GC performance.

For more information you may read the MSDN documentation about the GCHandle
structure and the GCHandleType enumeration.

If you have any further question on this issue, please feel free to reply
this thread.

Have a nice day!

Best regards,

Ying-Shen Yu [MSFT]
Microsoft community Support
Get Secure! - www.microsoft.com/security

This posting is provided "AS IS" with no warranties and confers no rights.
This mail should not be replied directly, please remove the word "online"
before sending mail.

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.