Converting std::wstring to SQLWCHAR *

2019-07-12 20:29发布

I have a C++ program that is dynamically creating a query string which will then be passed to a SQLExecDirect call to connect to a database using ODBC. I'm having trouble passing the variable from one function to another, so I think I must be missing something basic?

In the ConstructQuery function (which returns type SQLWCHAR *), I have:

std::wstring test = L"test string"; //This test string will actually be several concatenated strings
SQLWCHAR *statement = (SQLWCHAR *)test.c_str();
std::wcout << statement;
return statement;

This prints the statement variable as expected. But when I pass the variable to my main function like this:

SQLStatement = ConstructQuery(SQLStatement);
std::wcout << SQLStatement;

I get no output.

If, instead of statement = (SQLWCHAR *)test.c_str();

I use: statement = L"test string";

The variable passes fine, but then I am not able to dynamically create the "test string" query in the earlier part of the function.

I was having a hard time finding out much about SQLWCHAR. I'm guessing that I may be converting std::wstring to SQLWCHAR * incorrectly? Another option would be to rewrite the function so that all of the wstring are SQLWCHAR * and do the concatenation that way - but I'm not sure that's possible and even if it was I don't think it's preferred?

1条回答
看我几分像从前
2楼-- · 2019-07-12 20:52

You are returning a pointer to a local variable that goes out of scope at the end of the function ConstructQuery. It might be easiest to return a std::wstring by value and then work from there.

查看更多
登录 后发表回答