4

Win32 API 中的 Window-Procedure 必须是静态 \ 全局函数,因为它不能采用类对象 (the this) 参数。当然可以使用 hWnd->object 字典等变通方法。

我想知道D是否有办法优雅地解决它,例如为每个对象创建一个微小的成员函数副本(以调用对象的真实处理程序)或我可以分配给WNDCLASS.lpfnWndProc的匿名函数(我知道有匿名函数,但我不能使用他们的extern(Windows)财产)?

我可以做这样的事情:


class Window {
    extern (Windows)
    LRESULT delegate (HWND hWnd, UINT msg, WPARAM w, LPARAM l) MyWinProcDelegate;

    this() {
        MyWinProcDelegate = &Events;
    }

    extern (Windows)
    LRESULT Events (HWND hWnd, UINT msg, WPARAM w, LPARAM l) {
        MessageBoxA(null , "Success!!!" , null ,0);
        return DefWindowProcA(hWnd, message, wParam, lParam);
    }
}

(省略注册\创建\味精循环...)

Events() 似乎没有触发......我错过了什么吗?

4

3 回答 3

3

在这里,我为你做了这个(基于 BCS 的回答):

version (Windows)
{
    import std.c.windows.windows;

    void makeExecutable(ubyte[] code)
    {
        DWORD old;
        VirtualProtect(code.ptr, code.length, PAGE_EXECUTE_READWRITE, &old);
    }
}
else
version (linux)
{
    import core.sys.posix.sys.mman;
    import core.sys.posix.unistd;

    static if (!is(typeof(&mprotect)))
        extern(C) int mprotect(void*, size_t, int);

    void makeExecutable(ubyte[] code)
    {
        auto pageSize = sysconf(_SC_PAGE_SIZE);
        auto address = ((cast(size_t)code.ptr) & ~(pageSize-1));
        int pageCount =
            (address/pageSize == (address+code.length)/pageSize) ? 1 : 2;
        mprotect(cast(void*)address, pageSize * pageCount,
            PROT_READ | PROT_WRITE | PROT_EXEC);
    }
}
else
    static assert(0, "TODO");

R function(A) delegate2function(R, A...)(R delegate(A) d)
{
    enum size_t TEMPLATE1 = cast(size_t)0x01234567_01234567;
    enum size_t TEMPLATE2 = cast(size_t)0x89ABCDEF_89ABCDEF;

    static R functionTemplate(A args)
    {
        R delegate(A) d;
        d.ptr     = cast(typeof(d.ptr    ))TEMPLATE1;
        d.funcptr = cast(typeof(d.funcptr))TEMPLATE2;
        return d(args);
    }

    static void functionTemplateEnd() {}

    static void replaceWord(ubyte[] a, size_t from, size_t to)
    {
        foreach (i; 0..a.length - size_t.sizeof + 1)
        {
            auto p = cast(size_t*)(a.ptr + i);
            if (*p == from)
            {
                *p = to;
                return;
            }
        }
        assert(0);
    }

    auto templateStart = cast(ubyte*)&functionTemplate;
    auto templateEnd   = cast(ubyte*)&functionTemplateEnd;
    auto templateBytes = templateStart[0 .. templateEnd - templateStart];

    // must allocate type with pointers, otherwise GC won't scan it
    auto functionWords = new void*[(templateBytes.length / (void*).sizeof) + 3];
    // store context in word-aligned boundary, so the GC can find it
    functionWords[0] = d.ptr;
    functionWords[1] = d.funcptr;
    functionWords = functionWords[2..$];

    auto functionBytes = (cast(ubyte[])functionWords)[0..templateBytes.length];
    functionBytes[] = templateBytes[];

    replaceWord(functionBytes, TEMPLATE1, cast(size_t)d.ptr    );
    replaceWord(functionBytes, TEMPLATE2, cast(size_t)d.funcptr);
    makeExecutable(functionBytes);

    return cast(typeof(return)) functionBytes.ptr;
}

void main()
{
    import std.stdio;

    auto context = 42;

    void del(string s)
    {
        writeln(s);
        writeln(context);
    }

    auto f = delegate2function(&del);
    f("I am a pretty function");
}

在 Windows 32 位和 Linux 64 位上测试。

于 2011-12-28T13:08:57.710 回答
1

一个非常不可移植的解决方案是动态创建一个包装调用的函数。我会通过编写如下所示的函数来做到这一点:

extern(C) RetType TestFn(Arg arg /* and any others */) {
   Class c = cast(Class)(0xDEAD_BEEF);
   return c.Method(arg);
}

然后,您可以将此函数编译为未优化的PIC,对其进行反编译,并找到一个可以混合成您需要的字节序列。最终结果将是一个类型(可能是一个结构),它有一个返回函数指针的方法,并且在构造时,void用您在上述步骤中找到的字节填充一个内部数组,并将有问题的对象插入适当的位置。

稍微高级一点的解决方案是使用对象和方法指针填充委托,以便将两者都提供给构造函数。更高级的解决方案将模板类型并利用 C 和 D 调用约定的知识来动态生成参数转发代码。

于 2011-12-28T05:29:47.573 回答
1

使用SetWindowLong存储this在窗口本身中怎么样?

于 2011-12-28T12:48:54.530 回答