zoukankan      html  css  js  c++  java
  • windows下wchar_t* 转char*

    这个在windows下很常见,常用,留个档。

    一般用这个函数:

    size_t wcstombs(
       char *mbstr,
       const wchar_t *wcstr,
       size_t count 
    );

    mbstr

    The address of a sequence of multibyte characters.

    wcstr

    The address of a sequence of wide characters.

    count

    The maximum number of bytes that can be stored in the multibyte output string.

    locale

    The locale to use.

    下面是我写的一个测试,是根据项目改过来的:

     1 #include <windows.h>
     2 #include <string.h>
     3 #include <stdio.h>
     4 #include <stdlib.h>
     5 #include <boost/filesystem.hpp>
     6 
     7 namespace bfs = boost::filesystem;
     8 
     9 
    10 bool get_path(char *file_name,size_t size){
    11 
    12 
    13 char ownPth[MAX_PATH];
    14 
    15 HMODULE hModule = GetModuleHandle(NULL);    // Will contain exe path,When passing NULL to GetModuleHandle, it returns handle of exe itself
    16 if (hModule != NULL)
    17 {
    18 GetModuleFileNameA(hModule,ownPth, MAX_PATH);    // 
    19 }
    20 else
    21 ownPth[0]=0;
    22 
    23 bfs::path iic_file(ownPth);
    24 iic_file = iic_file.parent_path() / "dlp_usb.iic" ; //For releaseing
    25 
    26 if( !bfs::is_regular_file(iic_file))
    27 {
    28 printf("
    Error,open 68013 iic file fail
    ");
    29 return false ;
    30 }
    31 
    32 
    33 wcstombs(file_name,iic_file.c_str(),size);
    34 
    35 //memcpy(file_name,iic_file.c_str(),byte_count);
    36 
    37 return true;
    38 
    39 }
    40 
    41 int main(){
    42 
    43 char iic_file[512];
    44 
    45 if(get_path( iic_file,512)){
    46 
    47 printf("TRUE
    ");
    48 }
    49 
    50 return 0;
    51 }

    references:

    http://msdn.microsoft.com/en-us/library/5d7tc9zw.aspx

    http://blog.163.com/tianshi_17th/blog/static/4856418920085209414977/

  • 相关阅读:
    JAVA的显式锁
    JAVA线程池
    多线程中的各种锁
    《深入理解JAVA虚拟机》第三版 第七,八章 要点总结
    《深入理解JAVA虚拟机》第三版 第六章 要点总结
    JVM垃圾收集器总结
    《深入理解JAVA虚拟机》第三版 第二,三章 要点总结
    Map接口的实现类
    博客收藏列表
    毕设开发日志2017-12-28 完成!
  • 原文地址:https://www.cnblogs.com/foohack/p/4191274.html
Copyright © 2011-2022 走看看