【问题标题】:How to use [DllImport("")] in C#?如何在 C# 中使用 [DllImport("")]?
【发布时间】:2013-10-27 08:43:21
【问题描述】:

我发现了很多关于它的问题,但没有人解释我如何使用它。

我有这个:

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;
    }
}

谁能帮我理解为什么它在DllImport 行和public static 行上给我一个错误?

有没有人有想法,我该怎么办? 谢谢。

【问题讨论】:

  • 除了@vcsjones 提到的内容之外,还有多个问题。你有使用这个的 User32.dll 吗?并且还要检查你写 [DllImport] 语句的地方。它是错误的地方。
  • @PM。 User32.dll 是一个非常标准的 Win32 DLL,它总是在 SysWow64 或 System32 中,所以找到它应该没有问题。如果有另一个同名的 DLL 在搜索序列中“更接近”,它可能会找到错误,但这会给大多数程序带来灾难。
  • @vcsjones 哦,好吧,我的错。我不知道。

标签: c# process dllimport


【解决方案1】:

从 C# 9 开始,如果您从 SetForegroundWindow 减速中删除 public 关键字,您的语法将是有效的:

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;
    }
}

在 C# 9 中,局部函数可以有属性,请参阅here

【讨论】:

    【解决方案2】:

    您不能在方法或任何其他具有属性的方法中声明 extern 本地方法。将您的 DLL 导入移动到类中:

    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;
        }
    }
    

    【讨论】:

    • 您还必须在 Microsoft Visual Studio 中使用 System.Runtime.InteropServices
    • 您还必须使用 System.Diagnostics 才能使用 Process 对象
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-11-26
    • 2011-01-14
    • 1970-01-01
    • 2012-04-19
    • 2021-06-06
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多