zoukankan      html  css  js  c++  java
  • 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");

      

      

  • 相关阅读:
    NOI 2016 区间 解题报告
    有关莫队
    [JSOI2008]最大数 线段树解法
    HDU P3341 Lost's revenge 题解+数据生成器
    BZOJ P1212 [HNOI2004] L语言
    洛谷P3168 [CQOI2015]任务查询系统
    普通平衡树Tyvj1728、luogu P3369 (splay)
    洛谷P3384 树链剖分
    BZOJ P2157 旅游
    【算法导论】第6章,堆排序
  • 原文地址:https://www.cnblogs.com/cuish/p/4509638.html
Copyright © 2011-2022 走看看