How can we call unmanaged code through .NET?

 Posted by Ddd on 2/14/2011 | Category: Others Interview questions | Views: 3356 | Points: 40
Answer:

Unmanaged code can be called through .NET applications in a number of ways:

Example:

1)Pointer code is an unmanaged code:
Mark the pointer code secton with unsafe keyword.

compile the code with /unsafe switch
example:
class abc
{
static void Main()
{
unsafe
{
//Write the pointer code

}
}
}

example:
2)Win32 API Functions:
1)
//add this namespace
using System.Runtime.InteropServices;
(C#)

2)Mark the relevant dll with DllImport attribute
3)Put static and extern modifiers before the function
example:FlashWindow to blink the form's caption bar.

[DllImport("user32.dll")]
static extern void FlashWindow(IntPtr a, bool b);


Asked In: Many Interviews | Alert Moderator 

Comments or Responses

Login to post response