TCHAR Array to a concatenate LPCSTR


TCHAR Array to a concatenate LPCSTR



I am reading a ini file and want to execute a external program (VBS File) after that. But I am having problems with the string types.



This is my code.


LPCTSTR path = _T(".my.ini");
TCHAR fileName[500];
int b = GetPrivateProfileString(_T("Paths"), _T("filename"), _T(""), fileName, 500, path);
// fileName = myscript.vbs
// I need to execute "wscript myscript.vbs arg1"
// Execute script file. Doesnt work.
WinExec("wscript " + fileName + " arg1", MINIMZIED);
// OR. Doesnt work.
system("wscript " + fileName + " arg1");



This doesnt work. WinExec wants a LPCSTR but I have the fileName in a TCHAR and want to concatenate with some other string.


LPCSTR


fileName


TCHAR



How can I convert or concatenate it correctly?




2 Answers
2



From the WinExec() documentation:



This function is provided only for compatibility with 16-bit Windows. Applications should use the CreateProcess function.


CreateProcess



Which is CreateProcessW() in your case.


CreateProcessW()



Alternatively, you can use _wsystem().


_wsystem()





And when you go with CreateProcessW, get rid of all the TCHAR stuff. Use wide characters throughout, not something that might be wide character and might not. 20 years ago it was a useful bridge as the world adjusted to Windows NT-based OSes, but when was the last time you needed to write code to support Windows 98?
– user4581301
2 days ago


CreateProcessW


TCHAR





I don´t need to support Windows 98. I think the minimum would be Windows Server 2000. I am new to C++ and I am still learing a lot. Can you provide a code snippet for my case?
– Peter
2 days ago





Simply replace T with W in your string types, replace _T() with the L prefix, and append W to API function names, eg: LPCWSTR path = L".my.ini"; WCHAR fileName[500]; int b = GetPrivateProfileStringW(L"Paths", L"filename", L"", fileName, 500, path); ...
– Remy Lebeau
2 days ago


T


W


_T()


L


W


LPCWSTR path = L".my.ini"; WCHAR fileName[500]; int b = GetPrivateProfileStringW(L"Paths", L"filename", L"", fileName, 500, path); ...



You need to concatenate the strings using another buffer, for example:


LPCTSTR path = _T(".my.ini");
TCHAR fileName[500];
TCHAR command[520];

int b = GetPrivateProfileString(_T("Paths"), _T("filename"), _T(""), fileName, 500, path);
_stprintf_s(command, 520, _T("wscript %.*s arg1"), b, filename);



Then you can use command as needed, eg:


command


STARTUPINFO si = {};
si.cb = sizeof(si);
si.dwFlags = STARTF_USESHOWWINDOW;
si.wShowWindow = SW_MINIMIZED;

PROCESS_INFORMATION pi = {};

if (CreateProcess(NULL, command, NULL, NULL, FALSE, 0, NULL, NULL, &si, &pi))
{
...
CloseHandle(pi.hThread);
CloseHandle(pi.hProcess);
}



Or:


#ifdef UNICODE
#define system_t(cmd) _wsystem(cmd)
#else
#define system_t(cmd) system(cmd)
#endif

system_t(command);






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Comments

Popular posts from this blog

paramiko-expect timeout is happening after executing the command

Possible Unhandled Promise Rejection (id: 0): ReferenceError: user is not defined ReferenceError: user is not defined

Opening a url is failing in Swift