Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 934
ThomasFey Avatar asked Oct 18 '13 13:10

ThomasFey


People also ask

What is use of Dllimport?

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).

How do I use Dllimport in C++?

__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.

What is InteropServices?

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.

What is extern C#?

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.


1 Answers

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;     } } 
like image 125
vcsjones Avatar answered Sep 24 '22 13:09

vcsjones