| Start |
|---|
| Import function from unmanaged C++ DLL Here is an example of how to import a function that is defined in an unmanaged C++ DLL. In the C++ source code for "myDLL.dll", the function Then it can be included into a C# program as follows: See Calling conventions (opens new window) and C++ name mangling (opens new window) for explanations about why Finding the dynamic libraryWhen the extern method is first invoked the C# program will search for and load the appropriate DLL. For more information about where is searched to find the DLL, and how you can influence the search locations see this stackoverflow question (opens new window). |
| Calling conventions There're several conventions of calling functions, specifying who (caller or callee) pops arguments from the stack, how arguments are passed and in what order. C++ uses If you want to use a function with |
| C++ name mangling C++ compilers encode additional information in the names of exported functions, such as argument types, to make overloads with different arguments possible. This process is called name mangling (opens new window). This causes problems with importing functions in C# (and interop with other languages in general), as the name of There're several ways of solving the problem of name mangling: Function name will still be mangled ( myDLL.def module definition file: The function name will be pure |
| Dynamic loading and unloading of unmanaged DLLs When using the The pointer returned by The following code sample demonstrates this with the |
| Dealing with Win32 Errors When using interop methods, you can use GetLastError API to get additional information on you API calls. DllImport Attribute SetLastError Attribute SetLastError=true Indicates that the callee will call SetLastError (Win32 API function). SetLastError=false Indicates that the callee will not call SetLastError (Win32 API function), therefore you will not get an error information. Example: If you trying to open mutex which does not exist, GetLastError will return ERROR_FILE_NOT_FOUND. System Error Codes can be found here: https://msdn.microsoft.com/en-us/library/windows/desktop/ms681382(v=vs.85).aspx (opens new window) GetLastError API There is a native GetLastError API which you can use as well :
Here's why: Between your Win32 call which sets the error (calls SetLastError), the CLR can call other Win32 calls which could call SetLastError as well, this behavior can override your error value. In this scenario, if you call GetLastError you can obtain an invalid error. Setting SetLastError = true, makes sure that the CLR retrieves the error code before it executes other Win32 calls. |
| Reading structures with Marshal Marshal class contains a function named PtrToStructure, this function gives us the ability of reading structures by an unmanaged pointer. PtrToStructure function got many overloads, but they all have the same intention. Generic PtrToStructure: T - structure type. ptr - A pointer to an unmanaged block of memory. Example:
|
Simple code to expose class for com |
| Pinned Object GC (Garbage Collector) is responsible for cleaning our garbage. While GC cleans our garbage, he removes the unused objects from the managed heap which cause heap fragmentation. When GC is done with the removal, it performs a heap compression (defragmintation) which involves moving objects on the heap. Since GC isn't deterministic, when passing managed object reference/pointer to native code, GC can kick in at any time, if it occurs just after Inerop call, there is a very good possibility that object (which reference passed to native) will be moved on the managed heap - as a result, we get an invalid reference on managed side. In this scenario, you should pin the object before passing it to native code. Pinned Object Pinned object is an object that is not allowed to move by GC. Gc Pinned Handle You can create a pin object using Gc.Alloc method
Example: Precautions
RemarksWorking with Win32 API using C# Windows exposes lots of functionality in the form of Win32 API. Using these API you can perform direct operation in windows, which increases performance of your application.Source Click here (opens new window) Windows exposes a broad range of API. To get information about various APIs you can check sites like pinvoke (opens new window). |
Read Article
https://develop.horje.com/learn/1434/reference