Click here to Skip to main content
15,891,657 members
Home / Discussions / C#
   

C#

 
GeneralRe: Using variable in Values for SQL insert Pin
Anonymous7-Jun-04 10:06
Anonymous7-Jun-04 10:06 
GeneralRe: Using variable in Values for SQL insert Pin
Dave Kreskowiak7-Jun-04 10:13
mveDave Kreskowiak7-Jun-04 10:13 
GeneralRe: Using variable in Values for SQL insert Pin
Anonymous7-Jun-04 10:36
Anonymous7-Jun-04 10:36 
GeneralRe: Using variable in Values for SQL insert Pin
Heath Stewart7-Jun-04 11:23
protectorHeath Stewart7-Jun-04 11:23 
GeneralReadFileEx OVERLAPPED LocalAlloc wows Pin
gdwinslow7-Jun-04 8:03
gdwinslow7-Jun-04 8:03 
GeneralRe: ReadFileEx OVERLAPPED LocalAlloc wows Pin
Heath Stewart7-Jun-04 11:19
protectorHeath Stewart7-Jun-04 11:19 
GeneralRe: ReadFileEx OVERLAPPED LocalAlloc wows Pin
gdwinslow7-Jun-04 16:23
gdwinslow7-Jun-04 16:23 
GeneralRe: ReadFileEx OVERLAPPED LocalAlloc wows Pin
Heath Stewart8-Jun-04 2:55
protectorHeath Stewart8-Jun-04 2:55 
The Socket class wouldn't work even if it wasn't pinned - it doesn't have a representation in unmanaged code. Instead, you typically declare handles as IntPtrs, and use the Marshal class methods to help with marshaling values. You rarely ever need an unsafe context, either.

In this case, however, you must pin the Socket in memory. There is a chance that the garbage collector (GC) will move the object in managed memory while the unmanaged code is using it. Unmanaged code is just that - unmanaged - so it can't track such changes. Pinning the object in memory will also give you the IntPtr you need. Try something like this, for example:
[StructLayout(LayoutKind.Sequential)]
// The name doesn't matter, so go with .NET naming conventions
public struct OverlappedSocket : IDisposable
{
  public OverlappedSocket(long internal, long internalHigh, int offset,
    int offsetHigh, Socket socket)
  {
    this.Internal = new IntPtr(internal);
    this.InternalHigh = new IntPtr(internalHigh);
    this.Offset = offset;
    this.OffsetHigh = offsetHigh;
 
    // Pin the Socket in memory.
    hEvent = GCHandle.Alloc(socket, GCHandleType.Pinned);
  }
 
  // A ULONG_PTR is processor-dependent - not always 32-bits
  [MarshalAs(UnmanagedType.SysUInt)] public IntPtr Internal;
  [MarshalAs(UnmanagedType.SysUInt)] public IntPtr InternalHigh;
  [MarshalAs(UnmanagedType.U4)] public int Offset;
  [MarshalAs(UnmanagedType.U4)] public int OffsetHigh;
  [MarshalAs(UnmanagedType.SysUInt)] public GCHandle hEvent;
 
  void IDisposable.Dispose()
  {
    // Make sure we free unmanaged resources and let the GC move the Socket
    if (hEvent.IsAllocated) hEvent.Free();
  }
}


 

Microsoft MVP, Visual C#
My Articles
GeneralRe: ReadFileEx OVERLAPPED LocalAlloc wows Pin
Guy Winslow8-Jun-04 11:17
Guy Winslow8-Jun-04 11:17 
GeneralRe: ReadFileEx OVERLAPPED LocalAlloc wows Pin
Heath Stewart8-Jun-04 11:26
protectorHeath Stewart8-Jun-04 11:26 
GeneralRe: ReadFileEx OVERLAPPED LocalAlloc wows Pin
Guy Winslow8-Jun-04 12:42
Guy Winslow8-Jun-04 12:42 
GeneralRe: ReadFileEx OVERLAPPED LocalAlloc wows Pin
Heath Stewart8-Jun-04 17:15
protectorHeath Stewart8-Jun-04 17:15 
Generalsql server ce 2.0, image data type Pin
khchan7-Jun-04 6:45
khchan7-Jun-04 6:45 
GeneralRe: sql server ce 2.0, image data type Pin
Heath Stewart7-Jun-04 6:51
protectorHeath Stewart7-Jun-04 6:51 
GeneralHardcoded filepaths c# Pin
frank217-Jun-04 6:34
frank217-Jun-04 6:34 
GeneralRe: Hardcoded filepaths c# Pin
Heath Stewart7-Jun-04 6:44
protectorHeath Stewart7-Jun-04 6:44 
Generaldataset update on form closing Pin
C#Coder677-Jun-04 6:07
C#Coder677-Jun-04 6:07 
GeneralRe: dataset update on form closing Pin
Heath Stewart7-Jun-04 6:16
protectorHeath Stewart7-Jun-04 6:16 
GeneralVS2K3 doesn't copy file reference Pin
CBoland7-Jun-04 5:58
CBoland7-Jun-04 5:58 
GeneralRe: VS2K3 doesn't copy file reference Pin
Heath Stewart7-Jun-04 6:09
protectorHeath Stewart7-Jun-04 6:09 
GeneralRe: VS2K3 doesn't copy file reference Pin
CBoland7-Jun-04 8:45
CBoland7-Jun-04 8:45 
GeneralRe: VS2K3 doesn't copy file reference Pin
Heath Stewart7-Jun-04 8:47
protectorHeath Stewart7-Jun-04 8:47 
GeneralThreading and errors Pin
Russell Jones7-Jun-04 5:44
Russell Jones7-Jun-04 5:44 
GeneralRe: Threading and errors Pin
Heath Stewart7-Jun-04 6:20
protectorHeath Stewart7-Jun-04 6:20 
Generaldatasets, databinding and dataviews Pin
HappyPaws7-Jun-04 4:42
HappyPaws7-Jun-04 4:42 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.