How to use [DllImport("")] in C#?

C#ProcessDllimport

C# Problem Overview


I found a lot of questions about it, but no one explains how I can use this.

I have this:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.InteropServices;
using System.Windows.Forms;
using Microsoft.FSharp.Linq.RuntimeHelpers;
using System.Diagnostics;
using System.Runtime.InteropServices;
using System.IO;

public class WindowHandling
{
    public void ActivateTargetApplication(string processName, List<string> barcodesList)
    {
        [DllImport("User32.dll")]
        public static extern int SetForegroundWindow(IntPtr point);
        Process p = Process.Start("notepad++.exe");
        p.WaitForInputIdle();
        IntPtr h = p.MainWindowHandle;
        SetForegroundWindow(h);
        SendKeys.SendWait("k");
        IntPtr processFoundWindow = p.MainWindowHandle;
    }
}

Can someone help me to understand why it gives me an error on the DllImport line and on the public static line?

Does anyone have an idea, what can I do? Thank you.

C# Solutions


Solution 1 - C#

You can't declare an extern local method inside of a method, or any other method with an attribute. Move your DLL import into the class:

using System.Runtime.InteropServices;


public class WindowHandling
{
	[DllImport("User32.dll")]
	public static extern int SetForegroundWindow(IntPtr point);

	public void ActivateTargetApplication(string processName, List<string> barcodesList)
	{
		Process p = Process.Start("notepad++.exe");
		p.WaitForInputIdle();
		IntPtr h = p.MainWindowHandle;
		SetForegroundWindow(h);
		SendKeys.SendWait("k");
		IntPtr processFoundWindow = p.MainWindowHandle;
	}
}

Solution 2 - C#

Starting with C# 9, your syntax would be valid if you remove the public keyword from the SetForegroundWindow deceleration:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.InteropServices;
using System.Windows.Forms;
using Microsoft.FSharp.Linq.RuntimeHelpers;
using System.Diagnostics;
using System.Runtime.InteropServices;
using System.IO;

public class WindowHandling
{
    public void ActivateTargetApplication(string processName, List<string> barcodesList)
    {
        [DllImport("User32.dll")]
        static extern int SetForegroundWindow(IntPtr point);
        Process p = Process.Start("notepad++.exe");
        p.WaitForInputIdle();
        IntPtr h = p.MainWindowHandle;
        SetForegroundWindow(h);
        SendKeys.SendWait("k");
        IntPtr processFoundWindow = p.MainWindowHandle;
    }
}

In C# 9 local functions can have attributes, see https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/proposals/csharp-9.0/local-function-attributes">here</a>

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionThomasFeyView Question on Stackoverflow
Solution 1 - C#vcsjonesView Answer on Stackoverflow
Solution 2 - C#Coconut9View Answer on Stackoverflow