PageRenderTime 28ms CodeModel.GetById 6ms RepoModel.GetById 0ms 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. Summary:
  6. Platform Invocation Services (P/Invoke) in .NET allows managed code to call
  7. unmanaged functions that are implemented and exported in unmanaged DLLs. This
  8. VC# code sample demonstrates using P/Invoke to call the functions exported by
  9. the native DLLs: CppDynamicLinkLibrary.dll, user32.dll and msvcrt.dll.
  10. /////////////////////////////////////////////////////////////////////////////
  11. Sample Relation:
  12. CSPInvokeDll -> CppDynamicLinkLibrary
  13. CSPInvokeDll P/Ivnoke the functions exported by the native DLL
  14. CppDynamicLinkLibrary.
  15. /////////////////////////////////////////////////////////////////////////////
  16. Implementation:
  17. A. P/Invoke functions exposed from a native C++ DLL module.
  18. Step1. Declare the methods as having an implementation from a DLL export.
  19. First, declare the method with the static and extern C# keywords. Next,
  20. attach the DllImport attribute to the method. The DllImport attribute allows
  21. us to specify the name of the DLL that contains the method. The general
  22. practice is to name the C# method the same as the exported method, but we can
  23. also use a different name for the C# method. Specify custom marshaling
  24. information for the method's parameters and return value, which will override
  25. the .NET Framework default marshaling.
  26. For example,
  27. [DllImport("CppDynamicLinkLibrary.dll", CharSet = CharSet.Auto,
  28. CallingConvention = CallingConvention.Cdecl)]
  29. internal static extern int GetStringLength1(string str);
  30. These tools can help your write the correct P/Invoke declartions.
  31. Dumpbin: View the export table of a DLL
  32. PInvoke.NET: is primarily a wiki, allowing developers to find,
  33. edit and add PInvoke* signatures, user-defined types, and any other info
  34. related to calling Win32 and other unmanaged APIs from managed code such
  35. as C# or VB.NET.
  36. PInvoke Interop Assistant: It is a toolkit that helps developers to
  37. efficiently convert from C to managed P/Invoke signatures or verse visa.
  38. Step2. Call the methods through the PInvoke signatures. For example:
  39. string str = "HelloWorld";
  40. int length;
  41. length = NativeMethod.GetStringLength1(str);
  42. B. P/Invoke C++ classes exposed from a native C++ DLL module.
  43. There is no easy way to call the classes in a native C++ DLL module through
  44. P/Invoke. Visual C++ Team Blog introduced a solution, but it is complicated:
  46. The recommended way of calling native C++ class from .NET are:
  47. 1) use a C++/CLI class library to wrap the native C++ module, and your .NET
  48. code class the C++/CLI wrapper class to indirectly access the native C++
  49. class.
  50. 2) convert the native C++ module to be a COM server and expose the native
  51. C++ class through a COM interface. Then, the .NET code can access the
  52. class through .NET-COM interop.
  53. C. Unload the native DLL module.
  54. You can unload the DLL by first calling GetModuleHandle to get the handle of
  55. the module and then calling FreeLibrary to unload it.
  56. [DllImport("kernel32.dll", CharSet = CharSet.Auto, SetLastError = true)]
  57. static extern IntPtr GetModuleHandle(string moduleName);
  58. [DllImport("kernel32.dll", CharSet = CharSet.Auto, SetLastError = true)]
  59. [return: MarshalAs(UnmanagedType.Bool)]
  60. static extern bool FreeLibrary(IntPtr hModule);
  61. // Unload the DLL by calling GetModuleHandle and FreeLibrary.
  62. if (!FreeLibrary(GetModuleHandle(moduleName)))
  63. {
  64. Console.WriteLine("FreeLibrary failed w/err {0}",
  65. Marshal.GetLastWin32Error());
  66. }
  67. /////////////////////////////////////////////////////////////////////////////
  68. References:
  69. MSDN: Platform Invoke Tutorial
  71. MSDN: Using P/Invoke to Call Unmanaged APIs from Your Managed Classes
  73. MSDN: Calling Win32 DLLs in C# with P/Invoke
  75. PInvoke.NET
  77. PInvoke Interop Assistant
  79. /////////////////////////////////////////////////////////////////////////////