简体   繁体   English

我正在尝试在新类中使用dll导入,但是在此声明类型上出现错误属性'DllImport'无效

[英]I'm trying to use dll import in my new class but getting error Attribute 'DllImport' is not valid on this declaration type

The error Attribute 'DllImport' is not valid on this declaration type. 错误属性“ DllImport”在此声明类型上无效。 It is only valid on 'method' declarations. 它仅对“方法”声明有效。 Tried to add the dll import before the class in a method but same error. 试图在方法中添加dll导入,但方法相同。

using System;
using System.Collections.Generic;
using System.Text;
using System.Management;
using System.Diagnostics;
using System.Runtime.InteropServices;

namespace MinimizeCapture
{

    class WatchForWindow
    {
        [DllImport("user32.dll")]
        private const int SW_SHOWNORMAL = 1;
        private const int SW_SHOWMINIMIZED = 2;
        private const int SW_SHOWMAXIMIZED = 3;

        private static ManagementEventWatcher watcher = null;

        public static void StartWatching()
        {
            WqlEventQuery query = new WqlEventQuery("Select * From __InstanceCreationEvent Within 2 Where TargetInstance Isa 'Win32_Process'");
            watcher = new ManagementEventWatcher(query);
            watcher.EventArrived += new EventArrivedEventHandler(watcher_EventArrived);
            watcher.Start();
        }

        public static void StopWatching()
        {
            if (watcher != null)
            {
                watcher.Stop();
            }
        }

        private static void watcher_EventArrived(object sender, EventArrivedEventArgs e)
        {
            ManagementBaseObject obj = (ManagementBaseObject)e.NewEvent["TargetInstance"];
            string t = obj["Name"].ToString();
            GetHWND(t);
        }

        private static void GetHWND(string wName)
        {
            IntPtr hWnd = FindWindow("Notepad", "Untitled - Notepad");

        }
    }
}

The error is on this line: 错误在此行上:

[DllImport("user32.dll")]

I'm trying to use it since FindWindow is not exist. 我正在尝试使用它,因为FindWindow不存在。

You must apply the [DllImport] attribute on a method declaration without a body, bearing the static extern modifiers. 您必须将[DllImport]属性应用到没有主体的方法声明中,并带有static extern修饰符。

You can look up translated function declarations on PInvoke.net , including relevant structures when required. 您可以在PInvoke.net上查找翻译后的函数声明 ,包括所需的相关结构。 The function FindWindow , which you require, looks like this: 您需要的功能FindWindow如下所示:

[DllImport("user32.dll", SetLastError = true)]
static extern IntPtr FindWindow(string lpClassName, string lpWindowName);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 我正在尝试使用dll文件名:CDSDK.dll,但出现错误,此错误是什么? - I'm trying to use a dll file name: CDSDK.dll but getting an error what is this error? “属性'依赖关系'在此声明类型上无效。”错误 - “Attribute 'Dependency' is not valid on this declaration type.” error 属性对声明类型无效 - Attribute not valid on declaration type 属性'luismodel'在此声明类型上无效 - Attribute 'luismodel' is not valid on this declaration type 尝试打开/显示新表单时,有时会收到ArgumentException参数无效的原因? - I'm getting ArgumentException parameter is not valid sometimes when trying to open/show a new form why? 属性 UnityEngine.RequireComponent 在此声明类型上无效。 它仅对类声明有效 - The attribute UnityEngine.RequireComponent is not valid on this declaration type. It is valid on class declarations only 使用DLLImport导入类 - Using DLLImport to import a class 我收到错误,因为无法为我的项目解析服务类型 - I'm getting Error as Unable to resolve service for type for my project 我在使用DLLImport时应该在哪里放置dll文件? - Where should I place the dll file when I use DLLImport? 为什么从类库项目添加dll文件时出现错误:错误Provider1上的错误? - Why when adding dll file from Class Library project i'm getting error: Error on error Provider1?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM