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

      

      

  • 相关阅读:
    最新版 VS2015|Visual Studio Enterprise 2015简体中文版(企业版)
    MY97 日期控件只输入今天之前的值
    VARIANT类型
    C++ Struct
    MFC网络编程
    套接字编程(VC_Win32)
    套接字相关函数
    网络编程
    char str[] 与 char *str的区别详细解析
    Windows下C++多线程同步与互斥简单运用
  • 原文地址:https://www.cnblogs.com/cuish/p/4509638.html
Copyright © 2011-2022 走看看