Compartir a través de


Creación de una biblioteca de Dynamic-Link simple

En el ejemplo siguiente se muestra el código fuente necesario para crear un archivo DLL simple, Myputs.dll. Define una función de impresión de cadenas simple denominada myPuts. El archivo DLL de Myputs no define una función de punto de entrada, ya que está vinculada a la biblioteca en tiempo de ejecución de C y no tiene funciones de inicialización ni limpieza propias para realizar.

Para compilar el archivo DLL, siga las instrucciones de la documentación incluida con las herramientas de desarrollo.

Para obtener un ejemplo que usa myPuts, vea Usando Load-Time Vínculo Dinámico o Usando Run-Time Vínculo Dinámico.

// 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