1. Back To Blog

.NET interview questions: - Explain the steps to make Windows API calls in .NET?

.NET interview questions: - Explain the steps to make Windows API calls in .NET?

Windows API calls are not COM based and they are invoked through Platform Invoke Services.

Declare StringConversionType (Function | Sub) MethodName Lib "DllName" ([Args]) As Type

StringConversionType is for what type of conversion should take place. Either we can specify Unicode to convert all strings to Unicode values, or Auto to convert strings according to the .NET runtime rules.

MethodName is the name of the API to call.

DllName is the name of the DLL.

Args are any arguments to the API call.

Type is the return type of the API call.

Below is a sample code for VB.NET, which uses Sleep windows API for delaying.

Public Class Form1Declare Auto Sub Sleep Lib "kernel32.dll" (ByVal dwMilliseconds As Long)Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.LoadMessageBox.Show(" start sleeping for 5000 Milli seconds.....")Sleep(5000)MessageBox.Show(" end of sleeping.....")End SubEnd Class#region Using directivesusing System;using System.Collections.Generic;using System.ComponentModel;using System.Data;using System.Drawing;using System.Windows.Forms;using System.Runtime.InteropServices;#endregionnamespace CSharpCodepartial class Form1 : Form[DllImport("Kernel32.dll")]static extern int Sleep(long dwMilliseconds);public Form1()InitializeComponent();}private void Form1_Load(object sender, EventArgs e)MessageBox.Show("Starting of 5000 ms...");Sleep(5000);MessageBox.Show("End of 5000 ms...");}}}


Also view our video on use of VAR keyword: -




Get more tutorials for interview questions and answers for .NET

Regards,

Also view author's blog for Dotnet interview questions and answers
 

Shiv Prasad Koirala

Visit us @ www.questpond.com or call us at 022-66752917... read more

We are on Social