First, I know that it makes no sense to compare the dllimport attribute and the getProcAddress function directly. Rather, I'm interested in comparing two pieces of code that basically implement the same thing - calling a function in dll - by importing a function with the dllimport attribute or using the getProcAddress function. In particular, I am writing a C # application that uses some function in a DLL that I wrote. First, I accessed my dll function with the following code snippet:
class DllAccess { [DllImport("kernel32.dll", SetLastError = true)] private extern IntPtr LoadLibrary(String DllName); [UnmanagedFunctionPointer(CallingConvention.StdCall)] private delegate Bool BarType(Byte arg);
However, later I needed to get the lastError value if it was set during the dll call, so I changed the code to this:
class DllAccess { [DllImport("foo.dll", EntryPoint = "bar", CallingConvention = CallingConvention.StdCall, SetLastError = true)] private extern Bool DllBar(Byte arg);
This, of course, is much more neat, and, as already mentioned, it sets the lastError code. Obviously, my first code snippet gives me the ability to change the dll and function call at runtime, but this is not required at the moment. So my question is: is there any reason to use the first wording if I'm sure that I will not use another dll or another function?
Boris source share