PageRenderTime 9ms CodeModel.GetById 1ms app.highlight 3ms RepoModel.GetById 1ms app.codeStats 0ms

/Visual Studio 2008/CSPInvokeDll/ReadMe.txt

#
Plain Text | 115 lines | 79 code | 36 blank | 0 comment | 0 complexity | b7682572fe8b2baf4140c49d7a9a6559 MD5 | raw file
  1=============================================================================
  2            CONSOLE APPLICATION : CSPInvokeDll Project Overview
  3=============================================================================
  4
  5/////////////////////////////////////////////////////////////////////////////
  6Summary:
  7
  8Platform Invocation Services (P/Invoke) in .NET allows managed code to call 
  9unmanaged functions that are implemented and exported in unmanaged DLLs. This 
 10VC# code sample demonstrates using P/Invoke to call the functions exported by 
 11the native DLLs: CppDynamicLinkLibrary.dll, user32.dll and msvcrt.dll.
 12
 13
 14/////////////////////////////////////////////////////////////////////////////
 15Sample Relation:
 16
 17CSPInvokeDll -> CppDynamicLinkLibrary
 18CSPInvokeDll P/Ivnoke the functions exported by the native DLL 
 19CppDynamicLinkLibrary.
 20
 21
 22/////////////////////////////////////////////////////////////////////////////
 23Implementation:
 24
 25A. P/Invoke functions exposed from a native C++ DLL module.
 26
 27Step1. Declare the methods as having an implementation from a DLL export.
 28First, declare the method with the static and extern C# keywords. Next, 
 29attach the DllImport attribute to the method. The DllImport attribute allows 
 30us to specify the name of the DLL that contains the method. The general 
 31practice is to name the C# method the same as the exported method, but we can
 32also use a different name for the C# method. Specify custom marshaling 
 33information for the method's parameters and return value, which will override 
 34the .NET Framework default marshaling. 
 35
 36For example, 
 37
 38    [DllImport("CppDynamicLinkLibrary.dll", CharSet = CharSet.Auto,
 39        CallingConvention = CallingConvention.Cdecl)]
 40    internal static extern int GetStringLength1(string str);
 41
 42These tools can help your write the correct P/Invoke declartions.
 43
 44	Dumpbin: View the export table of a DLL
 45
 46	PInvoke.NET: PInvoke.net is primarily a wiki, allowing developers to find, 
 47	edit and add PInvoke* signatures, user-defined types, and any other info 
 48	related to calling Win32 and other unmanaged APIs from managed code such 
 49	as C# or VB.NET.
 50	
 51	PInvoke Interop Assistant: It is a toolkit that helps developers to 
 52	efficiently convert from C to managed P/Invoke signatures or verse visa. 
 53
 54Step2. Call the methods through the PInvoke signatures. For example:
 55
 56	string str = "HelloWorld";
 57    int length;
 58    length = NativeMethod.GetStringLength1(str);
 59
 60B. P/Invoke C++ classes exposed from a native C++ DLL module.
 61
 62There is no easy way to call the classes in a native C++ DLL module through 
 63P/Invoke. Visual C++ Team Blog introduced a solution, but it is complicated: 
 64http://go.microsoft.com/?linkid=9729423.
 65
 66The recommended way of calling native C++ class from .NET are:
 67
 68  1) use a C++/CLI class library to wrap the native C++ module, and your .NET 
 69     code class the C++/CLI wrapper class to indirectly access the native C++ 
 70     class.
 71
 72  2) convert the native C++ module to be a COM server and expose the native 
 73     C++ class through a COM interface. Then, the .NET code can access the 
 74     class through .NET-COM interop.
 75
 76C. Unload the native DLL module.
 77
 78You can unload the DLL by first calling GetModuleHandle to get the handle of 
 79the module and then calling FreeLibrary to unload it. 
 80
 81    [DllImport("kernel32.dll", CharSet = CharSet.Auto, SetLastError = true)]
 82    static extern IntPtr GetModuleHandle(string moduleName);
 83
 84    [DllImport("kernel32.dll", CharSet = CharSet.Auto, SetLastError = true)]
 85    [return: MarshalAs(UnmanagedType.Bool)]
 86    static extern bool FreeLibrary(IntPtr hModule);
 87
 88    // Unload the DLL by calling GetModuleHandle and FreeLibrary. 
 89    if (!FreeLibrary(GetModuleHandle(moduleName)))
 90    {
 91        Console.WriteLine("FreeLibrary failed w/err {0}", 
 92        Marshal.GetLastWin32Error());
 93    }
 94
 95
 96/////////////////////////////////////////////////////////////////////////////
 97References:
 98
 99MSDN: Platform Invoke Tutorial
100http://msdn.microsoft.com/en-us/library/aa288468.aspx
101
102MSDN: Using P/Invoke to Call Unmanaged APIs from Your Managed Classes
103http://msdn.microsoft.com/en-us/library/aa719104.aspx
104
105MSDN: Calling Win32 DLLs in C# with P/Invoke
106http://msdn.microsoft.com/en-us/magazine/cc164123.aspx
107
108PInvoke.NET
109http://www.pinvoke.net/
110
111PInvoke Interop Assistant 
112http://www.codeplex.com/clrinterop
113
114
115/////////////////////////////////////////////////////////////////////////////