Erstellen einer einfachen Dynamic-Link-Bibliothek

Das folgende Beispiel zeigt den Quellcode, der zum Erstellen einer einfachen DLL erforderlich ist, Myputs.dll. Es definiert eine einfache Zeichenfolgendruckfunktion namens myPuts. Die Myputs-DLL definiert keine Einstiegspunktfunktion, da sie mit der C-Laufzeitbibliothek verknüpft ist und über keine eigenen Initialisierungs- oder Bereinigungsfunktionen verfügt.

Befolgen Sie zum Erstellen der DLL die Anweisungen in der Dokumentation, die in Ihren Entwicklungstools enthalten ist.

Ein Beispiel, das myPuts verwendet, finden Sie unter Using Load-Time Dynamic Linking or Using Run-Time Dynamic Linking.

// The myPuts function writes a null-terminated string to
// the standard output device.
 
// The export mechanism used here is the __declspec(export)
// method supported by Microsoft Visual Studio, but any
// other export method supported by your development
// environment may be substituted.
 
 
#include <windows.h>
 
#define EOF (-1)
 
#ifdef __cplusplus    // If used by C++ code, 
extern "C" {          // we need to export the C interface
#endif
 
__declspec(dllexport) int __cdecl myPuts(LPCWSTR lpszMsg)
{
    DWORD cchWritten;
    HANDLE hConout;
    BOOL fRet;
 
    // Get a handle to the console output device.

    hConout = CreateFileW(L"CONOUT$",
                         GENERIC_WRITE,
                         FILE_SHARE_WRITE,
                         NULL,
                         OPEN_EXISTING,
                         FILE_ATTRIBUTE_NORMAL,
                         NULL);

    if (INVALID_HANDLE_VALUE == hConout)
        return EOF;
 
    // Write a null-terminated string to the console output device.
 
    while (*lpszMsg != L'\0')
    {
        fRet = WriteConsole(hConout, lpszMsg, 1, &cchWritten, NULL);
        if( (FALSE == fRet) || (1 != cchWritten) )
            return EOF;
        lpszMsg++;
    }
    return 1;
}
 
#ifdef __cplusplus
}
#endif