DLL中导出ANSI和UNICODE函数

模仿window中的DLL导出ANSI和UNICODE版本的函数,使用UNICODE宏来控制使用哪个版本;

在函数实际的执行代码UNICODE版本中,在ANSI函数的版本中只做参数的转换,及ANSI字符串转UNICODE字符串,然后调用UNICODE版本的函数。 

 0、DLL头文件
#include <Windows.h>

#ifndef _ICAL_H_
#define _ICAL_H_

#ifdef  DLL_EXPORT_IMP
    #define DLL_EXPORT  extern "C" __declspec(dllexport)
#else
    #define DLL_EXPORT  extern "C" __declspec(dllimport)
#endif

DLL_EXPORT int Add(int a, int b);
DLL_EXPORT void ShowA(LPCSTR lpStr);
DLL_EXPORT void ShowW(LPCWSTR lpStr);

#ifdef _UNICODE
    #define  Show ShowW
#else    
    #define  Show ShowA
#endif
 

#endif
 1、DLL导出函数的实现
#include "stdafx.h"

#define   DLL_EXPORT_IMP 
#include "ICal.h" 
#include <iostream>


int Add(int a, int b)
{
    return 0;
}



void ShowW( LPCWSTR lpStr )
{
    std::wcout << lpStr << std::endl;
}

 

void ShowA( LPCSTR lpStr )
{
    if (! lpStr)
        return;
     
    int nCntOfChs = MultiByteToWideChar(CP_ACP, 0, lpStr,-1, NULL, 0);
    LPWSTR lpwStr = new WCHAR[nCntOfChs]();
    if (! lpwStr)
        return;

    nCntOfChs = MultiByteToWideChar(CP_ACP, 0, lpStr, -1, lpwStr, nCntOfChs);
    ShowW(lpwStr);
    delete[] lpwStr;
    lpwStr = NULL;
}
 

  

 2、使用DLL中的导出函数
    Show(TEXT("hello T world"));
    ShowA("hello A world");
    ShowW(L"hello W world");

  

  

原文地址:https://www.cnblogs.com/cuish/p/4509638.html