需要动态分配大小的得自己把转化封装一下,MinGW5下跑过.

test.c

#include <windows.h>
#include <stdlib.h>
#include <stdio.h>
#include <locale.h>
void print_hex(const UCHAR *buf, int len)
{int i;for (i = 0; i < len; i++) {printf("0x%02x ", *(buf + i));if (!((i + 1) % 4))printf("\t");if (!((i + 1) % 8))printf("\n");}
}
int main(int argc, char **argv)
{char buf[] = {0x41, 0x00, 0x42, 0x00,     0x43, 0x00, 0x44, 0x00,0x0d, 0x59, 0xf6, 0x4e,        0x00, 0x00};WCHAR *uStr = (WCHAR *)buf;int uLen, aLen;char *aStr, *locale;/* Set locale */locale = setlocale(LC_ALL, "");if(locale == NULL) printf("Locale not set\n");elseprintf("Locale set to %s\n", locale);/* Print buf */wprintf(L"buf: %ws\n", buf);print_hex((UCHAR *)buf, 16);printf("\n");/* Unicode -> ANSI */aLen = WideCharToMultiByte(CP_ACP, 0, uStr, -1, NULL, 0, NULL, NULL);aStr = (char*) malloc(aLen);WideCharToMultiByte(CP_ACP, 0, uStr, -1, aStr, aLen, NULL, NULL);printf("aStr: %s\n", aStr);print_hex((UCHAR *)aStr, 16);printf("lstrlenA(aStr): %d\n", lstrlenA(aStr));printf("aLen: %d\n\n", aLen);/* ANSI -> Unicode */uLen = MultiByteToWideChar(CP_ACP, 0, aStr, -1, NULL, 0);uStr = (WCHAR *) malloc(uLen * sizeof(WCHAR));MultiByteToWideChar(CP_ACP, 0, aStr, -1, uStr, uLen);wprintf(L"uStr: %ws\n", uStr);print_hex((UCHAR *)uStr, 16);printf("lstrlenW(uStr): %d\n", lstrlenW(uStr));printf("uLen: %d\n\n", uLen);free(aStr);free(uStr);return EXIT_SUCCESS;
}

结果:

Locale set to Chinese (Simplified)_People's Republic of China.936
buf: ABCD复件
0x41 0x00 0x42 0x00     0x43 0x00 0x44 0x00
0x0d 0x59 0xf6 0x4e     0x00 0x00 0x40 0x00aStr: ABCD复件
0x41 0x42 0x43 0x44     0xb8 0xb4 0xbc 0xfe
0x00 0x4c 0x4c 0x41     0x54 0x45 0x3d 0x43
lstrlenA(aStr): 8
aLen: 9uStr: ABCD复件
0x41 0x00 0x42 0x00     0x43 0x00 0x44 0x00
0x0d 0x59 0xf6 0x4e     0x00 0x00 0x6f 0x70
lstrlenW(uStr): 6
uLen: 7

转载于:https://www.cnblogs.com/Leo-Forest/archive/2013/01/18/2866146.html

Windows API Unicode 和 多字节转化demo相关推荐

  1. go 调用winapi_golang 调用windows API 中文的处理

    Go语言发展势头很猛,其实缺点也很多,好在有广大爱好者提供了无数的库,把优点表现得太好了,搞得什么都是拿来就使用,基本完全不理会指针,性能还不错. 最近在windows下使用遇到一个中文的问题,首先要 ...

  2. 在C#中调用windows API函数

    Api函数是构筑Windws应用程序的基石,每一种Windows应用程序开发工具,它提供的底层函数都间接或直接地调用了Windows API函数,同时为了实现功能扩展,一般也都提供了调用Windows ...

  3. C++ WINDOWS API 第2章 Windows API概要

    目录 2.1       Windows数据类型.. 1 2.1.1       Windows数据类型示例.. 1 2.1.2       Windows 数据类型与标准C 数据类型的关系.. 5 ...

  4. 在c#中使用WINDOWS API(转)

    下面以C#为例简单介绍调用API的基本过程: 动态链接库函数的声明 动态链接库函数使用前必须声明,相对于VB,C#函数声明显得更加罗嗦,前者 通过 Api Viewer粘贴以后,可以直接使用,而后者则 ...

  5. Windows API 大全

    Windows API (包含了所有Windows API) Api函数名                               函数说明                             ...

  6. Windows API参考大全

    第一章 Win32 API概论 1.1为什么使用 Wu32 API 在Windows程序设计领域处于发展初期时,Windows程序员可使用的编程工具唯有API 函数.这些函数在程序员手中犹如" ...

  7. VB 与 Windows API 讲座(叁)转载

    code demo download VB 与 Windows API 讲座(叁) Windows 的讯息系统 /王国荣 VB 程式有两种工作模式-主动模式及事件驱动模式,主动模式与传统的 DOS 程 ...

  8. 使用C#调用windows API

    使用C#调用windows API(从其它地方总结来的,以备查询) C#调用windows API也可以叫做C#如何直接调用非托管代码,通常有2种方法: 1.  直接调用从 DLL 导出的函数. 2. ...

  9. C#中调用Windows API时的数据类型对应关系

    C#中调用Windows API时的数据类型对应关系 原文 C#中调用Windows API时的数据类型对应关系 BOOL=System.Int32 BOOLEAN=System.Int32 BYTE ...

  10. C#调用windows api的要点

    在.Net Framework SDK文档中,关于调用Windows API的指示比较零散,并且其中稍全面一点的是针对Visual Basic .net讲述的.本文将C#中调用API的要点汇集如下,希 ...

最新文章

  1. vue 背景透明度_一款媒体小白喜爱的视频编辑软件,vue视频编辑APP,想学就来...
  2. python 参数拼接token_对python的unittest架构公共参数token提取方法详解
  3. 深度学习中反向传播得到的损失是一个标量
  4. 产品经理常用工具有哪些?哪些产品经理工具最有用?
  5. C#实践设计模式原则SOLID
  6. 二叉排序树删除子节点以及遍历
  7. 直通BAT必考题系列:7种JVM垃圾收集器特点,优劣势、及使用场景
  8. java cfg_如何使用antlr生成Java CFG(控制流图)?
  9. POJ1769(线段树+DP)
  10. Python debug —— invalid literal for int() with base 10
  11. 时间序列的归一化方法
  12. Microsoft SQL Server 2008详细安装步骤
  13. 脉冲神经网络 神经元模型-Izhikevich模型(3)
  14. VC获取系统临时文件夹temp
  15. 定时删除虚拟服务器快照,自动执行VMware快照并删除超过5天的快照
  16. 浮窗---创建系统浮窗(可拖动)
  17. Python将数字转换成大写字母
  18. 手机微信广告页html代码,微信公众号h5网页被嵌入广告 不知道什么原因
  19. 2023年北京师范大学应用统计考研上岸前辈备考经验指导
  20. 2023.02.11草图大师浴室效果图1skp素材效果图

热门文章

  1. 【spring】spring动态代理和Spring_AOP
  2. php图片处理-补全图片链接,在链接前添加ip或其他路径-在正则匹配到的字符串前添加指定字符串
  3. Spark之UpdateStateByKey算子
  4. Spring Boot 2.x 集成 SLF4j + log4j2 日志框架
  5. 小D课堂 - 新版本微服务springcloud+Docker教程_5-06 高级篇幅之深入源码
  6. Spring Boot 知识笔记(集成zookeeper)
  7. 关闭jtag保留swd
  8. c++ primer 6.3.3节练习答案
  9. MongoDB 分片操作
  10. *SQL Server系统表的应用