Hi all,
I am intrigued to know what this function call does I have used it here an there, but don't really know what it does. Is it to call a specific function from a dll file without having to load the whole dll into memory
I have tried googl'in it, but couldn't find anything.
Thanks
Tryst

DLLImport() - What's it do?
kcabral1
Good job again Michael.
I have used this a few times but reading that it will blow out at runtime if the dll was not found - how would go about making sure it exists
Sure, you could use the File.Exists() method but is there no "rules" on where you can put in the [DllImport()] attribute/command in C#
I usually I always put it at the top of the class, just after the namespace declaration.
SureshJayaraman
The purpose of this attribute is to identify to the compiler/runtime a function that is not defined in the code but is available through a DLL call. The attribute identifies the DLL containing the function and any special considerations that may be necessary in order to marshal data to and from the unmanaged DLL. Without the attribute (and with the exception of another special attribute) all callable functions in your code must have a function body somewhere in managed code (either in the current assembly or in another assembly referenced by the current assembly).
If you are familiar with C++ then the attribute is equivalent to a function definition in a header file. Unlike C++ however no compile time checking is done to ensure that the function (or even the DLL) actually exists. If the function signature doesn't match or doesn't exist then it'll blow up at runtime.
DllImport is actually pretty trivial and uses the same technology that has been available since VC 6.0 to delay load a DLL when a function contained in the DLL is first needed. The following algorithm is probably how it is implemented:
Get the DLL defined in the DllImport attribute
If the DLL is not already loaded
Load the DLL into memory and throw an exception if it fails (LoadLibraryEx)
If the address for the function being called has not been found
Get the address of the function in the DLL (GetProcAddr)
Marshal the data from managed to unmanaged code and call the function at the given address
Unmarshal any data back
In effect DllImport loads the unmanaged DLL only when a function contained within it is called the first time. The DLL will remain in memory thereafter until the application closes. The address for the called function is cached so subsequent calls can do simply do a jump (actually 2 I believe) to the actual code after marshaling. There is no benefit in using DllImport over some other method as there is no other method available short of either migrating the unmanaged code to managed code or converting the unmanaged code to COM code and then using COM interop.
Michael Taylor - 6/1/066
Hetal_tatvasoft
As for determining if it'll blow up at runtime the only true way to know is to try and load it using LoadLibraryEx. If you don't get back a valid handle then the DLL can't be found. Although you could check for such a case before making the function call it will actually make it awkard to use and prone to failure. Instead the general rule (in fact enforced by FxCop) is that you should never publicly expose a private external function. Instead wrap the call in a public method that your clients should call. Within this method you can use a try-catch to trap the case where the DLL couldn't be found and react accordingly.
public class MyClass
{
public void Foo ( )
{
try
{
ExternalFoo();
} catch
{
//Do something reasonable
};
}
[DllImport("...")]
private static extern void ExternalFoo ( );
}
Michael Taylor - 6/1/06
MightyMondie
That is awesome! :)
Tryst