Pointers and Declarative Pinning : Pointer Unsafe « Language Basics « C# / C Sharp

Home
C# / C Sharp
1.2D Graphics
2.Class Interface
3.Collections Data Structure
4.Components
5.Data Types
6.Database ADO.net
7.Design Patterns
8.Development Class
9.Event
10.File Stream
11.Generics
12.GUI Windows Form
13.Language Basics
14.LINQ
15.Network
16.Office
17.Reflection
18.Regular Expressions
19.Security
20.Services Event
21.Thread
22.Web Services
23.Windows
24.Windows Presentation Foundation
25.XML
26.XML LINQ
C# / C Sharp by API
C# / CSharp Tutorial
C# / CSharp Open Source
C# / C Sharp » Language Basics » Pointer UnsafeScreenshots 
Pointers and Declarative Pinning

/*
A Programmer's Introduction to C# (Second Edition)
by Eric Gunnerson

Publisher: Apress  L.P.
ISBN: 1-893115-62-3
*/
// 31 - Interop\Calling Native DLL Functions\Pointers and Declarative Pinning
// copyright 2000 Eric Gunnerson
// file=ReadFileUnsafe.cs
// compile with: csc /unsafe ReadFileUnsafe.cs
using System;
using System.Runtime.InteropServices;
using System.Text;

class FileRead
{
    const uint GENERIC_READ = 0x80000000;
    const uint OPEN_EXISTING = 3;
    int handle;
    
    public FileRead(string filename)
    {
        // opens the existing file
        handle = CreateFile(    filename,
        GENERIC_READ,
        0
        0,
        OPEN_EXISTING,
        0,
        0);
    }
    
    [DllImport("kernel32", SetLastError=true)]
    static extern int CreateFile(
    string filename,
    uint desiredAccess,
    uint shareMode,
    uint attributes,        // really SecurityAttributes pointer
    uint creationDisposition,
    uint flagsAndAttributes,
    uint templateFile);
    
    [DllImport("kernel32", SetLastError=true)]
    static extern unsafe bool ReadFile(
    int hFile,
    void* lpBuffer, 
    int nBytesToRead,
    int* nBytesRead,
    int overlapped);
    
    public unsafe int Read(byte[] buffer, int count)
    {
        int n = 0;
        fixed (byte* p = buffer
        {
            ReadFile(handle, p, count, &n, 0);
        }
        return n;
    }
}
public class PointersandDeclarativePinning
{
    public static void Main(string[] args)
    {
        FileRead fr = new FileRead(args[0]);
        
        byte[] buffer = new byte[128];
        ASCIIEncoding e = new ASCIIEncoding();
        
        // loop through, read until done
        Console.WriteLine("Contents");
        while (fr.Read(buffer, 128!= 0)
        {
            Console.Write("{0}", e.GetString(buffer));
        }
    }
}

           
       
Related examples in the same category
1.References and Pointers
2.Demonstrate pointers and unsafeDemonstrate pointers and unsafe
3.Demonstrate fixedDemonstrate fixed
4.Demonstrate the effects of pointer arithmethicDemonstrate the effects of pointer arithmethic
5.Demonstrate pointer comparisonDemonstrate pointer comparison
6.An array name with an index yields a pointer to the start of the arrayAn array name with an index yields a pointer to the     start of the array
7.Index a pointer as if it were an arrayIndex a pointer as if it were an array
8.Use fixed to get a pointer to the start of a stringUse fixed to get a pointer to the start of a string
9.Multiple IndirectMultiple Indirect
10.Unsafe code: copyUnsafe code: copy
11.Unsafe ContextUnsafe Context
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.