DLLs in C# Application
-
I have noticed that I need not put any DLLImport statements in Windows application if I am using some DLLs. I just need to add those DLLs in the reference and then use it. My question is when is it necessary to have a DLLImport statement in my C# code? Thanks and Regards, Amit
-
I have noticed that I need not put any DLLImport statements in Windows application if I am using some DLLs. I just need to add those DLLs in the reference and then use it. My question is when is it necessary to have a DLLImport statement in my C# code? Thanks and Regards, Amit
The DllImport are required when you want to call into pre-.NET DLL, i.e. C dll. If it's a .NET dll (i.e. an assembly) no problem, if it's a C DLL, you need to do interop. Interop is a vast topic but well documented in the SDK's documentation, I suggest you to refer to it. Basically C# need to know about the function to use it, right? C DLL define no metadata that could be used by other .NET dlls to knwo about its content (they just define C header usable by a C compiler), therefore you have to do these declaration yourself. That's the purpose of DllImpoert. You've got most of win32 API Interop declaration there: http://pinvoke.net/[^]
-
The DllImport are required when you want to call into pre-.NET DLL, i.e. C dll. If it's a .NET dll (i.e. an assembly) no problem, if it's a C DLL, you need to do interop. Interop is a vast topic but well documented in the SDK's documentation, I suggest you to refer to it. Basically C# need to know about the function to use it, right? C DLL define no metadata that could be used by other .NET dlls to knwo about its content (they just define C header usable by a C compiler), therefore you have to do these declaration yourself. That's the purpose of DllImpoert. You've got most of win32 API Interop declaration there: http://pinvoke.net/[^]
Thanks for the answer. It, indeed, answers most of my initial questions. Now, I am trying to write a wrapper for LAPACK [http://www.netlib.org/clapack/faq.html], which is available in C and FORTRAN in C#. It is a highly optimized linear algebra subroutines. Do you have any idea about a place where I can go to learn the secret of the "writing wrappers in C#" business. Thanks and Regards, Amit -- modified at 20:24 Friday 26th August, 2005
-
Thanks for the answer. It, indeed, answers most of my initial questions. Now, I am trying to write a wrapper for LAPACK [http://www.netlib.org/clapack/faq.html], which is available in C and FORTRAN in C#. It is a highly optimized linear algebra subroutines. Do you have any idea about a place where I can go to learn the secret of the "writing wrappers in C#" business. Thanks and Regards, Amit -- modified at 20:24 Friday 26th August, 2005
You should really learn to use the documentation coming with the SDK. In the "index" side panel there is a box named "look for", just type: "interop", "COM interop" or "data marshaling" in it and you will find a wealth of information. Alternatively, if you've got issue with the SDK documentation (strangely many .NET developer seems to ignore about its existence) you could go on MSDN: http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpguide/html/cpconinteroperatingwithunmanagedcode.asp[^] Anyway about array marshaling (let's say float[]) the documentation doesn't straightforwardly explained you wether or not the whole array is copied or not to a temporary buffer every crossing the managed/unmanaged boudary (I think some attributes control that behavior). But always have the solution to use unsafe code & float* in C# or, as I prefer to write my wrappers now, use ManagedC++ (v2.0, meach cleaner, leaner and less ambiguous).