15.02.2015 Views

C# 4 and .NET 4

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

Platform invoke ❘ 723<br />

To reuse an unmanaged library that doesn ’ t contain COM objects, just exported functions, platform invoke<br />

(p/invoke) can be used. With p/invoke, the CLR loads the DLL that includes the function that should be<br />

called <strong>and</strong> marshals the parameters.<br />

To use the unmanaged function, fi rst you have to fi nd out the name of the function as it is exported. You<br />

can do this by using the dumpbin tool with the /exports option.<br />

For example, the comm<strong>and</strong><br />

dumpbin /exports c:\windows\system32\kernel32.dll | more<br />

lists all exported functions from the DLL kernel32.dll . In the example, you use the CreateHardLink()<br />

Windows API function to create a hard link to an existing fi le. With this API call, you can have several<br />

fi le n a m e s t h at refere nc e t he s a m e fi le a s long a s t he fi le n a m e s a re on ju s t one h a rd d i sk . T h i s A PI c a l l i s not<br />

available from .<strong>NET</strong> Framework 4, so platform invoke must be used.<br />

To call a native function, you have to defi ne a <strong>C#</strong> external method with the same number of arguments,<br />

<strong>and</strong> the argument types that are defi ned with the unmanaged method must have mapped types with<br />

managed code.<br />

The Windows API call CreateHardLink() has this defi nition in C++:<br />

BOOL CreateHardLink(<br />

LPCTSTR lpFileName,<br />

LPCTSTR lpExistingFileName,<br />

LPSECURITY_ATTRIBUTES lpSecurityAttributes);<br />

Now, this defi nition must be mapped to .<strong>NET</strong> data types. The return type is a BOOL with unmanaged code;<br />

this simply maps to the bool data type. LPCTSTR defi nes a long pointer to a const string. The Windows<br />

API uses the Hungarian naming convention for the data type. LP is a long pointer, C a const, <strong>and</strong> STR is<br />

a null - terminated string. The T marks the type as a generic type, <strong>and</strong> the type is either resolved to LPCSTR<br />

(an ANSI string) or LPWSTR (a wide Unicode string), depending on the compiler ’ s settings. C strings map to<br />

the .<strong>NET</strong> type String. LPSECURITY_ATTRIBUTES , which is a long pointer to a struct of type SECURITY_<br />

ATTRIBUTES . Because you can pass NULL to this argument, mapping this type to IntPtr is okay. The <strong>C#</strong><br />

declaration of this method must be marked with the extern modifi er, because there ’ s no implementation of<br />

this method within the <strong>C#</strong> code. Instead, the implementation of this method is found in the DLL kernel32<br />

.dll , which is referenced with the attribute [DllImport] . Because the return type of the .<strong>NET</strong> declaration<br />

CreateHardLink() is of type bool , <strong>and</strong> the native method CreateHardLink() returns a BOOL , some<br />

additional clarifi cation is useful. Because there are different Boolean data types with C++ (for example<br />

the native bool <strong>and</strong> the Windows - defi ned BOOL , which have different values), the attribute [MarshalAs]<br />

specifi es to what native type the .<strong>NET</strong> type bool should map.<br />

[DllImport("kernel32.dll", SetLastError="true",<br />

EntryPoint="CreateHardLink", CharSet=CharSet.Unicode)]<br />

[return: MarshalAs(UnmanagedType.Bool)]<br />

public static extern bool CreateHardLink(string newFileName,<br />

string existingFilename,<br />

IntPtr securityAttributes);<br />

The web site http://www.pinvoke.net <strong>and</strong> the tool P/Invoke Interop Assistant,<br />

which can be downloaded from http://www.codeplex.com, are very helpful with the<br />

conversion from native to managed code.<br />

The settings that you can specify with the attribute [DllImport] are listed in the following table.<br />

www.it-ebooks.info

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!