Alright, I am aware of my issue at the moment. I am trying to convert a string to a TCHAR. The error I get looks like this:
IntelliSense: initialization with '{...}' expected for aggregate object
The code I am using, large in part is from an msdn guide/tutorial where I am looking to replace lines about filling in a username and password based on briefly stored information. The guide asks for a prompt for username and password, however due to the requirements from my higher-ups, I can't do this. This C++ code is being ran as part of a larger C# program to add to the Microsoft Task Scheduler as C# doesn't have the functionality (as far as I can see).
string functionName;
std::wstring functionNameW;
LPCWSTR functionNameR;
[insert irrelevant code]
ifstream myfile ("~~string location~~");
if (myfile.is_open())
{
...
getline (myfile,functionName);
functionNameW = s2ws(functionName);
functionNameR = functionNameW.c_str();
//TCHAR of name
TCHAR *paramName=new TCHAR[functionName.size()+1];
paramName[functionName.size()]=0;
//As much as we'd love to, we can't use memcpy() because
//sizeof(TCHAR)==sizeof(char) may not be true:
std::copy(functionName.begin(),functionName.end(),paramName);
}
[insert irrelevant code]
TCHAR pszName[CREDUI_MAX_USERNAME_LENGTH] = paramName; //apparently not keep "const"-ness
So basically I read in the desired name from a .txt file. I then convert this name into multiple string types that are required at various stages throughout my code.
Now I have read up on this error from other stack-overflow submissions, but I run into the problem that many of these threads are specifically about arrays/vectors AND there do not seem to be any answers on how to actually deal with this problem. Everyone merely states that this error is attempting to say you are putting a "const" value into a non-const variable (or vice-versa).
So, is there a way I can get through this error? If it is an error dealing with "const"-ness, can I de-const a variable when placing it in a new location?
If you're looking for a "full" mini-version of the code, I guess it would look like this:
#include "stdafx.h" //created when MVS made this
#include <fstream> //need to read
#include <string> //need to read
#define _WIN32_DCOM //from msdn Task Manager Demo
#include <windows.h> //from msdn Task Manager Demo
#include <iostream> //from msdn Task Manager Demo
#include <stdio.h> //from msdn Task Manager Demo
#include <comdef.h> //from msdn Task Manager Demo
#include <wincred.h> //from msdn Task Manager Demo
// Include the task header file. //from msdn Task Manager Demo
#include <taskschd.h> //from msdn Task Manager Demo
# pragma comment(lib, "taskschd.lib") //from msdn Task Manager Demo
# pragma comment(lib, "comsupp.lib") //from msdn Task Manager Demo
# pragma comment(lib, "credui.lib") //from msdn Task Manager Demo
using namespace std;
std::wstring s2ws(const std::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);
std::wstring r(buf);
delete[] buf;
return r;
}
int __cdecl wmain()
{
string functionName;
std::wstring functionNameW;
LPCWSTR functionNameR;
ifstream myfile ("~~string location~~");
if (myfile.is_open())
{
getline (myfile,functionName);
functionNameW = s2ws(functionName);
functionNameR = functionNameW.c_str();
//TCHAR of name
TCHAR *paramName=new TCHAR[functionName.size()+1];
paramName[functionName.size()]=0;
//As much as we'd love to, we can't use memcpy() because
//sizeof(TCHAR)==sizeof(char) may not be true:
std::copy(functionName.begin(),functionName.end(),paramName);
}
TCHAR pszName[CREDUI_MAX_USERNAME_LENGTH] = paramName; //apparently not keep "const"-ness
}