Click here to Skip to main content
15,891,745 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
hello all

I searched everywhere but i didnt find the use of [DllImport("user32.dll")] and i dont know how to use , where to use ?

i saw lots of codes having this attribute . I am a beginner in programming c# , so please help
Posted
Updated 18-Jun-20 10:25am

It means that the method declared below it is not in .NET - it is in a external (native) DLL file.
In this case, it is in the User32.dll file, which is a standard Windows component.

For example:
C#
[DllImport("User32")]
private static extern int SetForegroundWindow(IntPtr hwnd);

The method SetForegroundWindow is a standard Windows function, and teh above declaration allows us to us it as if it was a .NET method:
C#
public static void SingleInstance(this Process thisProcess)
    {
    foreach (Process proc in Process.GetProcessesByName(thisProcess.ProcessName))
        {
        if (proc.Id != thisProcess.Id)
            {
            ShowWindow(proc.MainWindowHandle, SW_RESTORE);
            SetForegroundWindow(proc.MainWindowHandle);
            thisProcess.Kill();
            }
        }
    }
 
Share this answer
 
Comments
amirmohamad 28-Oct-12 9:18am    
thank's good help
+5
OriginalGriff 28-Oct-12 9:22am    
You're welcome!
You did not search everywhere, otherwise you would have found this[^].
 
Share this answer
 
This[^] might also give you extra information.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month


CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900