0

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
}
Community
  • 1
  • 1
  • What is `paramUserName`? Please present a real [testcase](http://stackoverflow.com/help/mcve). – Lightness Races in Orbit May 28 '15 at 19:54
  • @LightnessRacesinOrbit apologies, it was meant to be 'paramName'. This is the smallest amount of code I can trim down to that creates the same problem. It is just a means of converting from _C++ String_ to _C++ TCHAR_. – Zero Aurora May 28 '15 at 19:59
  • I can guarantee you can trim it more! But the testcase must be _complete_, which this isn't. Please follow the link I gave you (you were also presented with this information when you joined). – Lightness Races in Orbit May 28 '15 at 20:00
  • 1
    What would you expect `TCHAR pszName[CREDUI_MAX_USERNAME_LENGTH] = paramName;` to do? Copy the string? If you want to copy the data then you should do that with `_tcscpy` or whatever. Your short example doesn't compile because `paramName` isn't in scope, but if you correct that and try to compile you'll see the error. – Retired Ninja May 28 '15 at 20:04
  • @LightnessRacesinOrbit I _believe_ I added at complete version to the bottom of the original post. If it is not as trimmed as possible I am not sure where I can trim it. C++ is somewhat foreign to me, especially in this type of use. I am currently on internship and the only person working on this project outside of the scope of what I've learned previously so I am learning as I go and have hit this wall. – Zero Aurora May 28 '15 at 20:09
  • @RetiredNinja I was under the impression that line would assign or copy the value of `paramName` to `pszName`. The code in the original version that I linked read as `TCHAR pszName[CREDUI_MAX_USERNAME_LENGTH] = "";` Where would the `_tcscpy` go? I have never used C++ for this type of work. How would I correct the scope issue as well? – Zero Aurora May 28 '15 at 20:18
  • For the scope issue it would seem you might need to learn a bit more about C++ before you try to use it. That's quite a broad subject. There's already an example of copying string data in your code which I assume you wrote? You would want to do something similar with `std::copy` or `_tcscpy` to copy a different string. I realize you may have inherited this project, but for a beginner the `TCHAR` stuff will cause you more confusion than it is worth, especially since you have a fair amount of non TCHAR code already. – Retired Ninja May 28 '15 at 20:28
  • @RetiredNinja this is a project I am tasked with converting (very) old code from VB to C#, and while I am comfortable with C#, it doesn't have the needed libraries(?) to use the Microsoft Task Scheduler API. My coding background is more focused on Java, C# for Unity, and various game engines, so while C++ is somewhat similar to these languages there are many small differences. – Zero Aurora May 28 '15 at 20:34
  • @ZeroAurora You allocate `paramName` locally, copy a string to it, and then do nothing with it except leak memory. C++ is not Java -- that memory is leaked if you don't call `delete[]`. – PaulMcKenzie May 28 '15 at 20:48
  • @ZeroAurora When you say "... [C#] doesn't have the needed libraries(?) to use the Microsoft Task Scheduler API..." have you tried using COM interop? In a C# project's Solution Explorer right click on references, select Add reference, then select the COM tab on the left-hand side and type 'sched' in the search COM box. You should see the TaskScheduler type library in the search results. Add a reference to that type library and then check out the Interop.TaskScheduler assembly in the Object browser. – Frank Boyne May 28 '15 at 21:14
  • @FrankBoyne I am aware that C# and WPF have a Task Scheduler in their libraries, however I need the task to run outside of the programming environment. My understanding of C#'s task scheduler is that is doesn't allow for what I am tasked with making. – Zero Aurora May 29 '15 at 14:47
  • @ZeroAurora I wasn't suggesting you use the [TaskScheduler class](https://msdn.microsoft.com/en-us/library/system.threading.tasks.taskscheduler(v=vs.110).aspx). I was suggesting you use [COM Interop](https://msdn.microsoft.com/en-us/library/sd10k43k.aspx) to invoke the [Windows Task Scheduler COM Interfaces](https://msdn.microsoft.com/en-us/library/windows/desktop/aa383600.aspx) from a C# program. – Frank Boyne Jun 11 '15 at 19:20

0 Answers0