Converting std::wstring to SQLWCHAR *

时光总嘲笑我的痴心妄想 提交于 2019-12-24 00:34:08

问题


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:


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.



来源:https://stackoverflow.com/questions/11275225/converting-stdwstring-to-sqlwchar

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!