مشاركة عبر


كيفية القيام بما يلي: قم بتطبيق دالات رد الاتصال

الإجراء والمثال التالي شرح كيفية تطبيق تتم إدارته، استخدام استدعاء النظام الأساسي، يمكنك طباعة قيمة المؤشر لكل إطار على الكمبيوتر المحلي. Specifically, the إجراء و مثال استخدم the EnumWindows دالة إلى step through the قائمة of windows و a مدارة callback دالة (named CallBack) إلى طباعة the القيمة of the نافذة مؤشر.

إلى implement a callback دالة

  1. Look at the توقيع for the EnumWindows دالة قبل going further مع the implementation. EnumWindows has the following توقيع:

    BOOL EnumWindows(WNDENUMPROC lpEnumFunc, LPARAM lParam)
    

    واحد clue that this دالة يتطلب a callback هو the presence of the lpEnumFunc وسيطة. It هو عام to see the lp (long pointer) بادئة combined مع the Func suffix في the اسم of الوسيطات that take a مؤشر إلى a callback دالة. For documentation حول Win32 دالات, see the Microsoft النظام الأساسي SDK.

  2. إنشاء the مدارة callback دالة. The مثال declares a تفويض نوع, called CallBack, which takes الثاني الوسيطات (hwnd و lparam). The أول وسيطة هو a مؤشر إلى the نافذة; the ثانية وسيطة هو معرفة بواسطة تطبيق. في this يطرح المنتج, كلاهما الوسيطات must be integers.

    Callback دالات generally return nonzero قيم إلى indicate نجاح و zero إلى indicate فشل. This مثال بوضوح sets the return القيمة إلى صواب إلى متابعة the قائمة تعداد.

  3. إنشاء a تفويض و pass it كـ an وسيطة إلى the EnumWindows دالة. النظام الأساسي invoke converts the تفويض إلى a familiar callback تنسيق automatically.

  4. Ensure that the حاوي المهملات does not reclaim the تفويض قبل the callback دالة completes its work. When you pass a تفويض كـ a معلمة, أو pass a تفويض contained كـ a حقل في a بنية, it remains uncollected for the مدة of the يتصل. So, كـ هو the حالة في the following قائمة تعداد مثال, the callback دالة completes its work قبل the يتصل إرجاع و يتطلب لا إضافى إجراء بواسطة the مدارة caller.

    If, however, the callback دالة can be invoked بعد the يتصل إرجاع, the مدارة caller must take steps إلى ensure that the تفويض remains uncollected until the callback دالة finishes. لمعلومات مُفصل حول منع تجميع البيانات المهملة، راجع التنظيم والإرسال Interop "استدعاء النظام الأساسي".

مثال

Imports System
Imports System.Runtime.InteropServices

Public Delegate Function CallBack( _
hwnd As Integer, lParam As Integer) As Boolean

Public Class EnumReportApp

    Declare Function EnumWindows Lib "user32" ( _
       x As CallBack, y As Integer) As Integer

    Public Shared Sub Main()
        EnumWindows(AddressOf EnumReportApp.Report, 0)
    End Sub 'Main

    Public Shared Function Report(hwnd As Integer, lParam As Integer) _
    As Boolean
        Console.Write("Window handle is ")
        Console.WriteLine(hwnd)
        Return True
    End Function 'Report
End Class 'EnumReportApp
using System;
using System.Runtime.InteropServices;

public delegate bool CallBack(int hwnd, int lParam);

public class EnumReportApp {

    [DllImport("user32")]
    public static extern int EnumWindows(CallBack x, int y); 

    public static void Main() 
    {
        CallBack myCallBack = new CallBack(EnumReportApp.Report);
        EnumWindows(myCallBack, 0);
    }

   public static bool Report(int hwnd, int lParam) { 
        Console.Write("Window handle is ");
        Console.WriteLine(hwnd);
        return true;
    }
}
using namespace System::Runtime::InteropServices;

// A delegate type.
__delegate bool CallBack(int hwnd, int lParam);

// Managed type with the method to call.
__gc class EnumReport 
{
// Report the window handle.
public:
    bool Report(int hwnd, int lParam) {
       Console::Write(L"Window handle is ");
       Console::WriteLine(hwnd);
       return true;
   }
};

[DllImport("user32")] 
extern "C" int EnumWindows(CallBack* x, int y); 

void main(void) { 
    EnumReport* er = new EnumReport;
    CallBack* myCallBack = new CallBack(er, &EnumReport::Report);
    EnumWindows(myCallBack, 0); 
}

راجع أيضًا:

المبادئ

دالات الاستدعاء

موارد أخرى

استدعاء دالة DLL