Question

Need: Concatenate the result of the function GetComputerName (nameBuf) with the command to open the Chormium.

Objective: Create a new profile with the name of the machine that is being carried out the installation.

Problem: I do not know how I can accomplish this concatenation've tried to accomplish with strcpy and strcat without success.

Here my code:

int WINAPI WinMain(HINSTANCE inst,HINSTANCE prev,LPSTR cmd,int show)
{

    //Close phpdesktop.exe
    WinExec("taskkill /F /IM phpdesktop.exe", SW_HIDE);
    //Sleep
    Sleep(500);
    //Open phpdesktop.exe mini server 
    WinExec("phpdesktop.exe -S 127.0.0.1:54007 -t www -c php.ini", SW_HIDE);
    //Sleep    
    Sleep(500);

    //Get computer name
    TCHAR nameBuf[MAX_COMPUTERNAME_LENGTH + 2];
    DWORD nameBufSize;

    nameBufSize = sizeof nameBuf - 1;
    if (GetComputerName(nameBuf, &nameBufSize) == TRUE) 
    {
       //How to make the concatenation result nameBuf GetComputerName function with the command of Chromium.                  
       WinExec(strcpy("chromium\\ChromiumPortable -app=http://127.0.0.1:54007/ --profile-directory=", nameBuf), SW_HIDE);
    }

    //Return 
    return 0;
}

--

Pastbin: http://pastebin.com/N1eyAfGV

Was it helpful?

Solution 2

If you dont want to use a second buffer you acn try this

THAR nameBuf[254 + 2]={0};
DWORD nameBufSize;
strcpy(nameBuf,"chromium\\ChromiumPortable -app=http://127.0.0.1:54007/ --profile-directory=");
nameBufSize = sizeof nameBuf - 1;
int index = strlen(nameBuf);
if (GetComputerName(&nameBuf[index], &nameBufSize) == TRUE) 
{
   WinExec(nameBuf, SW_HIDE);
}

OTHER TIPS

strcpy into a const char * invokes undefined behavior:

std::string s = "chromium\\ChromiumPortable ...";
s += nameBuf;

WinExec(s.c_str(), SW_HIDE);

Try it:

TCHAR command[1024];
strcpy(command, "chromium\\ChromiumPortable -app=http://127.0.0.1:54007/ --profile-directory=");
strcat(command,nameBuf);
WinExec(command, SW_HIDE);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top