起初都是用ANSI字符集编写代码,但考虑到以后编写国际化程序的需要,我还是选择了通用编程。
这当中,由于有VC大量的宏使得编程还算顺利,但是有些api无法同时支持两种字符集,故在ansi和unicode编码的转换就很重要了
以下是最重要的两个函数,也是我今天所使用的。
MultiByteToWideChar
Maps a character string to a wide character (Unicode UTF-16) string. The character string mapped by this function is not necessarily from a multibyte character set.
int MultiByteToWideChar( UINT CodePage, DWORD dwFlags, LPCSTR lpMultiByteStr, int cbMultiByte, LPWSTR lpWideCharStr, int cchWideChar );
WideCharToMultiByte
int WideCharToMultiByte(
UINT CodePage,
DWORD dwFlags,
LPCWSTR lpWideCharStr,
int cchWideChar,
LPSTR lpMultiByteStr,
int cbMultiByte,
LPCSTR lpDefaultChar,
LPBOOL lpUsedDefaultChar
);
具体使用查看msdn platform sdk。
贴下我IIniManager接口代码
////////////////////////////////////////////////////////////////////////////
// Copyright(c) 1999-2010, RIPPLE , All Rights Reserved
// Author: RIPPLE
// Created: 2010/01/03
//
/// @file IIniManager.hpp
/// @brief 配置文件接口
/// @version 0.1
////////////////////////////////////////////////////////////////////////////
#ifndef _H_IINIMANAGER_H_
#define _H_IINIMANAGER_H_
#include "ApplicationConfig.h"
#include <windows.h>
#include <iostream>
using namespace std;
#include <tchar.h>
//INIMANAGERDLL 已经在ILogManager接口实现的工程设置的预处理器中定义,对于其他工程,将是没有定义的
#ifdef INIMANAGERDLL
#define IniManagerDLL __declspec(dllexport)
#else
#define IniManagerDLL __declspec(dllimport)
#endif
//////////////////////////////////////////////////////////////////////////
/// @class IIniManager
/// @brief 配置文件接口
//////////////////////////////////////////////////////////////////////////
class IniManagerDLL IIniManager
{
public:
~IIniManager(){}
virtual void add() = 0;
virtual bool SetInt(LPCTSTR key, int value) = 0; //设置Int
virtual int GetInt(LPCTSTR key) = 0;
virtual bool SetString(LPCTSTR key, LPCTSTR value) = 0;
virtual bool GetString(LPCTSTR key, OUT LPTSTR value, size_t count) = 0;
};
/////////////////////////////////////
/// @brief 创建配置模块实例
/// @param [in] string SectionName = "APPLICATION"
/// @param [in] string FileName = ".//AppSetting.ini"
/// @return IIniManager*
/////////////////////////////////////
IniManagerDLL IIniManager* CreateIIniManagerInstance(LPCTSTR SectionName = _T("APPLICATION"), LPCTSTR FileName = _T(".//AppSetting.ini") );
#endif