加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

混合c标准字符串和Windows API

发布时间:2020-12-16 10:27:20 所属栏目:百科 来源:网络整理
导读:许多 Windows API采用指向缓冲区和大小元素的指针,但结果需要进入c字符串. (我在这里使用windows unicode,所以它们是wstrings) 这是一个例子: – #include iostream#include string#include vector#include windows.husing namespace std;// This is the me
许多 Windows API采用指向缓冲区和大小元素的指针,但结果需要进入c字符串. (我在这里使用windows unicode,所以它们是wstrings)

这是一个例子: –

#include <iostream>
#include <string>
#include <vector>
#include <windows.h>

using namespace std;

// This is the method I'm interested in improving ...
wstring getComputerName()
{
    vector<wchar_t> buffer;
    buffer.resize(MAX_COMPUTERNAME_LENGTH+1);
    DWORD size = MAX_COMPUTERNAME_LENGTH;

    GetComputerNameW(&buffer[0],&size);

    return wstring(&buffer[0],size);
}

int main()
{
    wcout << getComputerName() << "n";
}

我的问题是,这是编写getComputerName函数的最佳方法,以便更好地适应C,还是有更好的方法?除非我错过了什么,否则我没有看到任何方式直接使用字符串而不通过向量?它工作正常,但不知何故看起来有点难看.问题不在于特定的API,它只是一个方便的例子.

解决方法

在这种情况下,我没有看到std :: vector为聚会带来了什么. MAX_COMPUTERNAME_LENGTH可能不是很大,所以我只想使用C风格的数组作为临时缓冲区.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读