简体   繁体   English

将变量传递给WIN32 API LPCWSTR?

[英]Passing a variable to WIN32 API LPCWSTR?

Recently I've made an update client for my software. 最近,我为我的软件做了一个更新客户端。 It uses WinHTTP to connect to my company's server, I wanted to add a special string in the user-agent section of WINDOWS API in WinHttpOpen. 它使用WinHTTP连接到我公司的服务器,我想在WinHttpOpen的WINDOWS API的用户代理部分中添加一个特殊字符串。 I need to pass a variable to pwszUserAgent of WinHttpOpen which is LPCWSTR. 我需要将变量传递给WinHttpOpen的pwszUserAgent,它是LPCWSTR。

Here's part of my code 这是我的代码的一部分

//convert string to wstring
wstring s2ws(const string& s)
{
    int len;
    int slength = (int)s.length() + 1;
    len = MultiByteToWideChar(CP_ACP, 0, s.c_str(), slength, 0, 0);
    wchar_t* buf = new wchar_t[len];
    MultiByteToWideChar(CP_ACP, 0, s.c_str(), slength, buf, len);
    wstring r(buf);
    delete[] buf;
    return r;
}


//This defined string indicates a string variable I got previously
string MyVariable_grabbed_previously = "version:15.3, Date:2016/12/10"
//a flag indicate if version string variable exists
bool Version_variable = TRUE;

//define LPCWSTR for winAPI user-agent section
LPCWSTR UserAgent;

if (Version_variable) {
//version variable exist
        string UA = "my custom UA & version" + MyVariable_grabbed_previously;
        wstring UAWS = s2ws(UA);
        UserAgent = UAWS.c_str();



    }
    else {
//Version variable not exist
        UserAgent = L"my custom UA";

    }


hSession = WinHttpOpen(UserAgent, WINHTTP_ACCESS_TYPE_NO_PROXY, WINHTTP_NO_PROXY_NAME, WINHTTP_NO_PROXY_BYPASS, 0);

However it seems my program kept using empty string as User-agent, I wonder why my value can not be passed correctly? 但是,似乎我的程序一直使用空字符串作为User-agent,我想知道为什么我的值不能正确传递吗? I'm new to Windows API. 我是Windows API的新手。

The problem is that you are passing an invalid pointer to WinHttpOpen() . 问题是您正在传递无效的指针到WinHttpOpen() You are creating a temporary std::wstring object, grabbing a pointer to its data, and then passing that pointer after the std::wstring is destroyed. 您正在创建一个临时的 std::wstring对象,获取一个指向其数据的指针,然后销毁std::wstring之后传递该指针。

Change your UserAgent variable to std::wstring instead, and then use c_str() when you are ready to pass it, eg: 将您的UserAgent变量改为std::wstring ,然后在准备好传递它时使用c_str() ,例如:

wstring s2ws(const string& s)
{
    wstring r;
    int slength = s.length();
    if (slength > 0)
    {
        int len = MultiByteToWideChar(CP_ACP, 0, s.c_str(), slength, 0, 0);
        r.resize(len);
        MultiByteToWideChar(CP_ACP, 0, s.c_str(), slength, &r[0], len);        
    }
    return r;
}

string MyVariable_grabbed_previously = "version:15.3, Date:2016/12/10"; 
bool Version_variable = true;    
wstring UserAgent;

...

if (Version_variable) {
    UserAgent = s2ws("my custom UA & " + MyVariable_grabbed_previously);
}
else {
    UserAgent = L"my custom UA";
}

/* Alternatively:
UserAgent = L"my custom UA";
if (Version_variable) {
    UserAgent += (L" & " + s2ws(MyVariable_grabbed_previously));
}
*/

hSession = WinHttpOpen(UserAgent.c_str(), WINHTTP_ACCESS_TYPE_NO_PROXY, WINHTTP_NO_PROXY_NAME, WINHTTP_NO_PROXY_BYPASS, 0);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM