发新话题
打印

41 D for Win32

本主题由 yidabu 于 2007-12-19 12:37 移动

41 D for Win32

知识若不分享 实在没有意义 http://www.d-programming-language-china.org 20070426

点击下面网址查看原文:
http://www.d-programming-language-china.org

by: D语言论坛 http://www.d-programming-language-china.org
from: http://www.digitalmars.com/d/windows.html
version: 基于D 1.012 (Apr 12, 2007)

本节描述了 32 位 Windows 系统下的 D 实现。自然,Windows 专用的 D 特性对其他平台是不可移植的。
在 C 语言中,我们这样做:

#include <windows.h>

而在 D 中,我们这样做:

import std.c.windows.windows;

Calling Conventions调用惯例

C调用Windows API用__stdcall,在D里很简单:

extern (Windows)
{
    /* ... function declarations ... */
}

The Windows linkage attribute sets both the calling convention and the name mangling scheme to be compatible with Windows.

For functions that in C would be __declspec(dllimport) or __declspec(dllexport), use the export attribute:

export void func(int foo);
If no function body is given, it's imported. If a function body is given, it's exported.( 本文出处: http://www.d-programming-language-china.org )

Windows ExecutablesWindows 可执行程序

你可以使用 D 编写 Windows GUI 应用程序。一个简单的例子是 \dmd\samples\d\winsamp.d 。( 本文出处: http://www.d-programming-language-china.org )

编写Windows可执行程序需要这样做:

需要用WinMain函数代替main作为入口
WinMain 必须是以下格式:

import std.c.windows.windows;

extern (C) void gc_init();
extern (C) void gc_term();
extern (C) void _minit();
extern (C) void _moduleCtor();
extern (C) void _moduleDtor();
extern (C) void _moduleUnitTests();

extern (Windows)
int WinMain(HINSTANCE hInstance,
    HINSTANCE hPrevInstance,
    LPSTR lpCmdLine,
    int nCmdShow)
{
    int result;

    gc_init();            // initialize garbage collector
    _minit();            // initialize module constructor table

    try
    {
    _moduleCtor();        // call module constructors
    _moduleUnitTests();    // run unit tests (optional)

    result = myWinMain(hInstance, hPrevInstance, lpCmdLine, nCmdShow);

    _moduleDtor();        // call module destructors
    }

    catch (Object o)        // catch any uncaught exceptions
    {
    MessageBoxA(null, cast(char *)o.toString(), "Error",
            MB_OK | MB_ICONEXCLAMATION);
    result = 0;        // failed
    }

    gc_term();            // run finalizers; terminate garbage collector
    return result;
}

int myWinMain(HINSTANCE hInstance,
    HINSTANCE hPrevInstance,
    LPSTR lpCmdLine,
    int nCmdShow)
{
    /* ... insert user code here ... */
}

The myWinMain() function is where the user code goes, the rest of WinMain is boilerplate to initialize and shut down the D runtime system.

A .def (Module Definition File) with at least the following two lines in it:
http://www.digitalmars.com/ctg/ctgDefFiles.html( 本文出处: http://www.d-programming-language-china.org )

QUOTE:
EXETYPE NT
SUBSYSTEM WINDOWS

Without those, Win32 will open a text console window whenever the application is run.

The presence of WinMain() is recognized by the compiler causing it to emit a reference to __acrtused_dll and the phobos.lib runtime library.
http://www.digitalmars.com/ctg/acrtused.html

( lastupdate:20070426 最新文章请访问http://www.d-programming-language-china.org )

关于一大步成功社区:
yidabu提倡在交流中学习,在分享中提高
收集感兴趣的知识,写下心得,通过网络与别人一起分享
理解一点就实践一步,收获什么就分享什么,成功就是这样一点点一步步累积起来的
网络只是一个工具,只有自己身心提高才是实实在在的。d-programming-language-china.org为大家提供一个学习交流各种知识的平台

TOP

发新话题