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.
DllImport attribute is used at run time to call a function exported in an external DLL implemented with unmanaged code that is executed outside the control of common language runtime (CLR).
__declspec(dllimport) tells the compiler that a symbol is imported, but it doesn't tell where from. To specify that, you need to link with the import library that accompanies the DLL. If you don't have one, you can have the linker generate it for you by providing a definition file.
Marshal Class (System.Runtime.InteropServices) Provides a collection of methods for allocating unmanaged memory, copying unmanaged memory blocks, and converting managed to unmanaged types, as well as other miscellaneous methods used when interacting with unmanaged code.
The extern modifier is used to declare a method that is implemented externally. A common use of the extern modifier is with the DllImport attribute when you are using Interop services to call into unmanaged code. In this case, the method must also be declared as static , as shown in the following example: C# Copy.
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; } }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With