8

This is my first time and I'd like to make a parallel process using the windows CreateProcess function. Based on the example at MSDN I created a LPTSTR "(non-const) TCHAR string" command line argument like this

LPTSTR szCmdline[] = _tcsdup(TEXT("\C:\\MyProgram_linux_1.1\\MyProgram.exe") );

The LPTSTR and other char and string types are discussed here

The command line argument is passed to CreateProcess like this

if (!CreateProcess(NULL, szCmdline, /*...*/) ) cout << "ERROR: cannot start CreateProcess" << endl;

And these headers are present

#include <stdio.h>
#include <stdlib.h>
#include <windows.h>
#include <strsafe.h>
#include <direct.h>

On compile this is the error:

error C3861: '_tcsdup': identifier not found

A search for this error found the same error but the solution was specific to using a .NET framework rather than explaining the error C3861: '_tcsdup'

Not sure if it related but there is also an error C2059: syntax error : ')' on the if (!CreateProcess(NULL, szCmdline, /*...*/) ) cout << "ERROR: cannot start CreateProcess" << endl;

How is this error fixed? And, what is going on with this?

Also, I am using the CreateProcess as a learning step towards learning the Linux fork() function - the Visual Studio interface is easier for me to use and once this is debugged and works, I will change to the g++ interface and change to fork() and debug from there - so a solution that leads to fork(), if possible, is the most beneficial.

Community
  • 1
  • 1
forest.peterson
  • 755
  • 2
  • 13
  • 30
  • You need to `#include ` (or change `_tcsdup` to `strdup` and stop using the `T` macros). – Nik Bougalis Mar 13 '13 at 18:12
  • 3
    Wow...you are using `CreateProcess` to learn `fork`? Why not just learn `fork`? These are very different beasts. – nneonneo Mar 13 '13 at 18:13
  • The only thing `fork` and `CreateProcess` have in common is that they're both functions... Tinkering with `CreateProcess` is about as likely to help you understand `fork` as learning French is likely to help you speak Japanese. – Nik Bougalis Mar 13 '13 at 18:15
  • yes, but everything i found online says that `CreateProcess` is the closest thing windows has to `fork()` - cited in the question the source. I am sure there are issues general to prallel processing in my code that must be fixed, I'd like to fix these global parallel processing issues in visual studio, then fix `fork()` specific issues in g++; if I am going the wrong direction with this - please point me right – forest.peterson Mar 13 '13 at 18:18
  • @NikBougalis: I'd say they have a tiny bit more in common than that; for example, `CreateProcess` -- just like `fork` -- adds a process to the system and returns that process's handle to the caller. The similarities definitely don't go all the way, though. Analogywise, it's more like learning French to understand German. – cHao Mar 13 '13 at 18:19
  • @NikBougalis error C2664: 'strdup' : cannot convert parameter 1 from 'const wchar_t [38]' to 'const char *' – forest.peterson Mar 13 '13 at 18:20
  • @forest.peterson: Your project is set for UNICODE. Just `#include `. Or, if you feel adventurous, to remove the _T() around strings and change the settings in your project to compile as ANSI/MBCS. As for pointing you in the right direction: I'll repeat what others said: learn `fork` by using `fork`. – Nik Bougalis Mar 13 '13 at 18:22
  • this might be a dumb question - why is the MSDN example using "unnecessary schizo char typing"? – forest.peterson Mar 13 '13 at 18:34
  • 1
    @forest.peterson: They have to be concerned to some degree with source compatibility across all the char types. At one time schizo char typing was the norm, because there were systems that didn't support Unicode or some other reasons to stick with 8-bit chars. Add to that, there are people using all of them to some degree and it's a waste to have three examples that only differ in char type. You don't really have to care; your code can decide whether to use wide chars, MBCS, or ANSI, and IMO it should. – cHao Mar 13 '13 at 19:06

2 Answers2

8

Add the following include:

#include <tchar.h>
Mike Kwan
  • 24,123
  • 12
  • 63
  • 96
  • error C2440: 'initializing' : cannot convert from 'wchar_t *' to 'LPTSTR []' – forest.peterson Mar 13 '13 at 18:23
  • 3
    Eh. Just lose the `TCHAR`s and `TEXT("string")`s and `_tcsdup`s and such. Use `wchar_t` and `L"string"` and `wcsdup`, or `char` and `"string"` and `strdup`. There's no compelling reason for the schizo char typing anymore. – cHao Mar 13 '13 at 18:27
  • As for why you're getting that error, though... `LPTSTR stuff[]` is not what you want. You want `some_char_type stuff[]` or `some_char_type *stuff`. The first is giving you an array of pointers to strings. – cHao Mar 13 '13 at 18:31
  • @cHao `char *cmdLine = strdup("\C:\\Daedalus_linu_1.1\\daedalus.exe 1 1" );` this worked – forest.peterson Mar 13 '13 at 18:31
  • @forest.peterson: The comment would cause that (assuming it's not just a placeholder for stuff you removed for posting purposes). To the compiler, that looks like `if (!CreateProcess(NULL, szCmdLine, ) )` -- instead of a closing parenthesis, the compiler would expect another argument after that last comma. – cHao Mar 13 '13 at 18:34
  • @cHao I copied it exactly from the MSDN (no)help site - it looks liek this now `CreateProcess(NULL, cmdLine, NULL, NULL, false, 0, NULL, NULL, &si, &pi);` not sure what `STARTUPINFO si;` and `PROCESS_INFORMATION pi;` are – forest.peterson Mar 13 '13 at 18:43
  • @MikeKwan I marked this as the solution, it technically solved the problem, but based on the comments I think this needs a lot more explanation about the "schizo char typing" - I could not find a solution that compiled without using the exact MSDN example format. – forest.peterson Mar 13 '13 at 19:01
  • 1
    @forest.peterson: A STARTUPINFO is initial params and stuff, like the position, size, I/O handles etc. A PROCESSINFO contains the process and thread IDs and handles of the new process. See http://msdn.microsoft.com/en-us/library/windows/desktop/ms686331(v=vs.85).aspx and http://msdn.microsoft.com/en-us/library/windows/desktop/ms684873(v=vs.85).aspx – cHao Mar 13 '13 at 19:03
3

_tcsdup is a macro, that maps to implementation function depending on your Unicode settings. As you have not included a header file (tchar.h) the compiler thinks it is a symbol and emits wrong code.

Depending on actual locate settings _tcsdump maps to one of those:

  • _strdup
  • _mbsdup
  • _wcsdup

http://msdn.microsoft.com/en-us/library/y471khhc(v=vs.110).aspx

Valeri Atamaniouk
  • 5,125
  • 2
  • 16
  • 18