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 2006

Tip: Looking for answers? Try searching our database.

Needed advise in the use of IntPtr within PInvoke

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
eduwushu - 11 Apr 2006 08:51 GMT
Hi there,
I have some doubts about the using of PInvoke to import dll functions.
Let's see
I have a function which returns a pinter to a structure to open a network
adapter

LPADAPTER PacketOpenAdapter(LPTSRT AdapterName);

and i have other which is used to close the adapter and free the adapter
structure

VOID PacketCloseAdapter(LPADAPTER lpAdapter);

the first thing i have done is to define  an adapter structure

[StructLayout(LayoutKind.Sequential,CharSet=CharSet.Auto)]
class LPADAPTER
{
     public IntPtr hFile;
     [MarshalAs(UnmanagedType.ByValTStr,SizeConst=64)]
     public string SymbolicLink;
     public int NumWrites;
     public IntPtr ReadEvent;
     public int ReadTimeout;
}

and do this

[DllImport("packet.dll", CharSet = CharSet.Auto)]
static extern LPADAPTER PacketOpenAdapter(string AdapterName);
[DllImport("packet.dll")]
static extern void PacketCloseAdapter(LPADAPTER lpAdapter);

OK but then i have thinked that if the structure has to be freed by
packetclose adapter then i cannot pass a managed structure and i have to use
IntPtr as well in the DllImports:
[DllImport("packet.dll", CharSet = CharSet.Auto)]
static extern IntPtr PacketOpenAdapter(string AdapterName);
[DllImport("packet.dll")]
static extern void PacketCloseAdapter(IntPtr lpAdapter);

But im not very sure if it is necessary.What is the criteria i have to
follow to decide if i have to use IntPtr's or managed structures???
Thanks
Dmytro Lapshyn [MVP] - 11 Apr 2006 11:15 GMT
Hi,

You can use IntPtr-s because you don't own the memory allocated for the
ADAPTER structure, but you can also keep the structure declaration and use
Marshal.PtrToStruct to copy the data from the IntPtr to the structure.

Rate this thread:







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.