Unicode setlasterrortrue this example is for

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

View Full Document Right Arrow Icon
ExactSpelling=false, CharSet=CharSet.Unicode, SetLastError=true)] * This example is for illustrative purposes—in a real program, you’d just use the FileInfo class’s MoveTo method because it’s more convenient. FileInfo uses P/Invoke internally—it calls the Win32 MoveFile for you when you call MoveTo . 716 | Chapter 19: Interop with COM and Win32
Image of page 740

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

View Full Document Right Arrow Icon
static extern bool MoveFile( string sourceFile, string destinationFile); The DllImport attribute class is used to indicate that an unmanaged method will be invoked through P/Invoke. The parameters are as follows: DLL name This is the name of the DLL that contains the method you are invoking. EntryPoint This indicates the name of the DLL entry point (the method) to call. ExactSpelling The CLR understands certain DLL method naming conventions. For example, there is in fact no MoveFile method—there are two methods, MoveFileA and MoveFileW , designed for the ANSI and Unicode string representations, respectively. Setting ExactSpelling to false lets the CLR select a method based on these rules. CharSet This indicates how the string arguments to the method should be marshaled. SetLastError Setting this to true allows you to call Marshal.GetLastWin32Error , and check whether an error occurred when invoking this method. In fact, all of these are optional except for the DLL name. If you leave out EntryPoint , .NET uses the method name as the entry point name. ExactSpelling is false by default—you set this to true only if you want to disable the use of normal naming conventions. And if you leave out CharSet , the CLR will use Unicode if it’s available. SetLastError is off by default, so although it’s optional, it’s usually a good idea to set it. Therefore, in practice, we would probably just write this: [DllImport("kernel32.dll", SetLastError=true)] static extern bool MoveFile( string sourceFile, string destinationFile); The main reason P/Invoke offers all these optional settings is that some DLLs don’t follow the usual conventions. Most of the time the defaults do the right thing, but just occasionally you need to override them. With this declaration in place, we can now call MoveFile() like any other static method. So if that declaration were inside a class called Tester , we could write: Tester.MoveFile(file.FullName, file.FullName + ".bak"); We pass in the original filename and the new name, and Windows moves the file for us. In this example, there is no advantage—and actually a considerable disadvantage— to using P/Invoke. (Situations where you truly need P/Invoke are increasingly rare and obscure. To illustrate the mechanisms, we’ve picked an example that’s simple enough not to obscure the details of how P/Invoke works, but this means it’s not a scenario in which you’d use P/Invoke in practice.) You have left the world of managed code, and the result is that you’ve abandoned type safety and your code will no longer run in P/Invoke | 717
Image of page 741
“partial-trust” scenarios. Example 19-2 shows the complete source code for using P/ Invoke to move the files.
Image of page 742

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

View Full Document Right Arrow Icon
Image of page 743
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