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

2

59

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.

Gannon answered 18/10, 2013 at 13:22 Comment(3)
There are multiple issues in addition to what @vcsjones has mentioned. Do you have User32.dll where you are using this. And also check the place where you have written the [DllImport] statement.Its wrong place.Kulda
@Kulda User32.dll is a pretty standard Win32 DLL that will always be in SysWow64 or System32, so there shouldn't be an issue with finding it. It might find the wrong one if there is another DLL with the same name that is "closer" in the search sequence, but that would bring disaster to most programs.Reprisal
@Reprisal Oh ok, my bad. I was not aware.Kulda
R
96

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;
    }
}
Reprisal answered 18/10, 2013 at 13:24 Comment(2)
You must also be using System.Runtime.InteropServices in Microsoft Visual StudioStrychnine
You must also be using System.Diagnostics in order to use the Process objectWinterize
J
5

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

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 this article.

Jetliner answered 24/3, 2021 at 13:16 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.