Renamed to pinvoke 719

Info icon This preview shows pages 743–746. Sign up to view the full content.

View Full Document Right Arrow Icon
c:\test\media\newTest\onestop.mid renamed to c:\test\media\newTest\onestop.mid P/Invoke | 719
Image of page 743

Info icon This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
c:\test\media\newTest\recycle.wav deleted. c:\test\media\newTest\ringout.wav renamed to c:\test\media\newTest\ringout.wav c:\test\media\newTest\Speech Disambiguation.wav deleted. Pointers Until now, you’ve seen no code using C/C++-style pointers. Pointers are central to the C family of languages, but in C#, pointers are relegated to unusual and advanced pro- gramming; typically, they are used only with P/Invoke, and occasionally with COM. C# supports the usual C pointer operators, listed in Table 19-1 . Table 19-1. C# pointer operators Operator Meaning & The address-of operator returns a pointer to the address of a value. * The dereference operator returns the value at the address of a pointer. -> The member access operator is used to access the members of a type via a pointer. In theory, you can use pointers anywhere in C#, but in practice, they are almost never required outside of interop scenarios, and their use is nearly always discouraged. When you do use pointers, you must mark your code with the C# unsafe modifier. The code is marked as unsafe because pointers let you manipulate memory locations directly, defeating the usual type safety rules. In unsafe code, you can directly access memory, perform conversions between pointers and integral types, take the address of variables, perform pointer arithmetic, and so forth. In exchange, you give up garbage collection and protection against uninitialized variables, dangling pointers, and accessing mem- ory beyond the bounds of an array. In essence, the unsafe keyword creates an island of code within your otherwise safe C# application that is subject to all the pointer-related bugs C++ programs tend to suffer from. Moreover, your code will not work in partial- trust scenarios. Silverlight does not support unsafe code at all, because it only supports partial trust. Silverlight code running in a web browser is always con- strained, because code downloaded from the Internet is not typically considered trustworthy. Even Silverlight code that runs out of the browser is constrained—the “elevated” permissions such code can re- quest still don’t grant full trust. Silverlight depends on the type safety rules to enforce security, which is why unsafe code is not allowed. As an example of when this might be useful, read a file to the console by invoking two Win32 API calls: CreateFile and ReadFile . ReadFile takes, as its second parameter, a pointer to a buffer. The declaration of the two imported methods is straightforward: [DllImport("kernel32", SetLastError=true)] static extern unsafe int CreateFile( 720 | Chapter 19: Interop with COM and Win32
Image of page 744
string filename, uint desiredAccess, uint shareMode, uint attributes, 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); You will create a new class, APIFileReader , whose constructor will invoke the CreateFile() method. The constructor takes a filename as a parameter, and passes that
Image of page 745

Info icon This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
Image of page 746
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

What students are saying

  • Left Quote Icon

    As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

    Student Picture

    Kiran Temple University Fox School of Business ‘17, Course Hero Intern

  • Left Quote Icon

    I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern