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");

      

      

  • 相关阅读:
    golang zip 压缩,解压(含目录文件)
    c3p0参数详解
    golang协程——通道channel阻塞
    乔恩与加菲猫引发的思考
    部署bugzilla(bugzilla+apache+mysql+linux)
    新的篇章
    SQL基础2
    SQL基础1
    引用
    const 对象默认为文件的局部变量
  • 原文地址:https://www.cnblogs.com/cuish/p/4509638.html
Copyright © 2011-2022 走看看