Can you call a native function that is exported from a dll, if so how?

 Posted by Kmandapalli on 1/22/2014 | Category: ASP.NET Interview questions | Views: 2156 | Points: 40
Answer:

Here’s a quick example of the DllImport attribute in action:

using System.Runtime.InteropServices; \
class C
{
[DllImport(\"user32.dll\")]
public static extern int MessageBoxA
(int h, string m, string c, int type);
public static int Main()
{
return MessageBoxA(0, \"Hello World!\", \"Caption\", 0);
}
}

This example shows the minimum requirements for declaring a C# method that is implemented in a native DLL. The method C.MessageBoxA() is declared with the static and external modifiers, and has the DllImport attribute, which tells the compiler that the implementation comes from the user32.dll, using the default name of MessageBoxA.


Asked In: Many Interviews | Alert Moderator 

Comments or Responses

Login to post response