You are not logged in.

Jesse

Unregistered

1

Thursday, October 21st 2004, 10:51pm

Processes in WIndows

This worked in Viscual C++ v 6.0

STARTUPINFO si = { sizeof(si) };
PROCESS_INFORMATION pi;
char szExe[] = "c:/.../runrti.bat";

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

I upgraded to Microsove Visual.net (2003) and now I get this error

error C2664: 'CreateProcessW' : cannot convert parameter 2 from 'char [33]' to 'LPWSTR'
Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast

I know how to cast, but I've never heard of LPWSTR type. As far as I can see on microsoft it's a null terminated string pointer. I've tried to implement this as a string pointer and I get the same thing. Has anyone had this problem?

jacek

Master

  • "jacek" is male

Posts: 2,729

Location: Warsaw, Poland

  • Send private message

2

Thursday, October 21st 2004, 11:03pm

RE: Processes in WIndows

LPWSTR --- W like Wide, which means Unicode.

Try:

Source code

1
_TCHAR szExe[] = _T("c:/.../runrti.bat");
and remember to include tchar.h

PS. http://www.qtforum.org/thread.php?threadid=3177&sid=

Jesse

Unregistered

3

Thursday, October 21st 2004, 11:10pm

RE: Processes in WIndows

Thanks for the quick response. When did they change this cast? Used to be LPSTR. I fixed it by using a cast.

STARTUPINFO si = { sizeof(si) };
PROCESS_INFORMATION pi;
char* szExe[] = "c:/.../runrti.bat";

if( CreateProcess(0, (wchar_t*)szExe, 0, 0, FALSE, 0, 0, 0, &si, &pi))
{
CloseHandle(pi.hProcess);
CloseHandle(pi.hThread);
}

I've never heard of wchar_t, what's the difference between that and a char?

Jesse

Unregistered

4

Thursday, October 21st 2004, 11:12pm

Processes

Btw, no [] in the fix.

  • "wysota" is male

Posts: 4,276

Location: Warsaw, POLAND

  • Send private message

5

Thursday, October 21st 2004, 11:14pm

RE: Processes in WIndows

It sounds more professional :D

jacek

Master

  • "jacek" is male

Posts: 2,729

Location: Warsaw, Poland

  • Send private message

6

Thursday, October 21st 2004, 11:30pm

RE: Processes in WIndows

You can't just do a cast --- it won't work.

windows can encode strings in Unicode using wide characters wchar_t (ie. using two bytes), but they
can also use ordinary char type.

wchar_t* is a string encoded in UTF-16
char* is a ASCII string

They aren't compatible.

If you go to MSDN site, you'll see LPCTSTR or LPTSTR everywhere as a string types. Those can
be either wchar_t* or char* depending on whether _UNICODE macro is defined. Types with "W"
inside use Unicode only.

To make programmers' lifes easier (?) M$ instoduced a set of macros, which change their meaning
whether you compile you program as Unicode enabled or not. LPCTSTR is one of them. Others are
_T, _TEXT, _TCHAR,...

Read this.

Jesse

Unregistered

7

Thursday, October 21st 2004, 11:47pm

hmm...

Seems you are right, I'm getting an exception thrown at that piece of code. It compiled fine with the casts, I'll implement the non-casting method.

jacek

Master

  • "jacek" is male

Posts: 2,729

Location: Warsaw, Poland

  • Send private message

8

Thursday, October 21st 2004, 11:51pm

RE: hmm...

Quoted

Originally posted by JesseIt compiled fine with the casts, I'll implement the non-casting method.

Well, you told the compiler that you know better, so it didn't complain. That's why all the casts in C++
are so ugly --- so you can easily spot them, as they can really mess things up.

Jesse

Unregistered

9

Friday, October 22nd 2004, 12:03am

Compiles but no execution

It is now compiling, but the process is not executing the bat file.

it says I need to define _UNICODE, which I did. But it still doesn't seem to be executing this bat file, any ideas?

STARTUPINFO si = { sizeof(si) };
PROCESS_INFORMATION pi;
_TCHAR szExe = _T("c:/ordept/dst/scripts/runrti.bat");

if( CreateProcess(0, (wchar_t*)szExe, 0, 0, FALSE, 0, 0, 0, &si, &pi))
{
CloseHandle(pi.hProcess);
CloseHandle(pi.hThread);
}

jacek

Master

  • "jacek" is male

Posts: 2,729

Location: Warsaw, Poland

  • Send private message

10

Friday, October 22nd 2004, 12:07am

RE: Compiles but no execution

Try this:

Source code

1
2
3
4
5
6
7
8
9
STARTUPINFO si = { sizeof(si) };
PROCESS_INFORMATION pi;
_TCHAR szExe[] = _T("c:/ordept/dst/scripts/runrti.bat");

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

Jesse

Unregistered

11

Friday, October 22nd 2004, 12:10am

RE: Compiles but no execution

seems as if my #define _UNICODE isn't working, i'm getting cannot cast char[33] to LPWSTR.

jacek

Master

  • "jacek" is male

Posts: 2,729

Location: Warsaw, Poland

  • Send private message

12

Friday, October 22nd 2004, 12:13am

RE: Compiles but no execution

Throw away that _UNICODE macro --- IMO you shouldn't use it in your code.

Jesse

Unregistered

13

Friday, October 22nd 2004, 12:16am

RE: Compiles but no execution

Cannot convert _TCHAR to LPWSTR...

jacek

Master

  • "jacek" is male

Posts: 2,729

Location: Warsaw, Poland

  • Send private message

14

Friday, October 22nd 2004, 12:17am

RE: Compiles but no execution

Could you post what the compiler said? Did you include tchar.h?

Jesse

Unregistered

15

Friday, October 22nd 2004, 12:22am

RE: Compiles but no execution

compiler:
error C2664: 'CreateProcessW' : cannot convert parameter 2 from '_TCHAR' to 'LPWSTR'

Here's implemented code.

#include "tchar.h"

.
.
.

STARTUPINFO si = { sizeof(si) };
PROCESS_INFORMATION pi;
_TCHAR szExe = _T("c:/ordept/dst/scripts/runrti.bat");

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

jacek

Master

  • "jacek" is male

Posts: 2,729

Location: Warsaw, Poland

  • Send private message

16

Friday, October 22nd 2004, 12:25am

RE: Compiles but no execution

Ok, last try:

Source code

1
2
3
4
5
6
7
LPWSTR szExe = L"c:/ordept/dst/scripts/runrti.bat";

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

Jesse

Unregistered

17

Friday, October 22nd 2004, 12:30am

Threw an exception

the L macro through an exception when referencing the memory. I found a site that created a wrapper for ASCII and UNICODE strings
http://www.codeproject.com/string/tconvert.asp
I'm gonna give that a shot.

jacek

Master

  • "jacek" is male

Posts: 2,729

Location: Warsaw, Poland

  • Send private message

18

Friday, October 22nd 2004, 12:38am

RE: Threw an exception

Maybe there should be LPCWSTR?

  • "wysota" is male

Posts: 4,276

Location: Warsaw, POLAND

  • Send private message

19

Friday, October 22nd 2004, 1:05am

RE: Threw an exception

Hmm... I found here that it should be LPCTSTR.

Go to sleep guys... 8o

jacek

Master

  • "jacek" is male

Posts: 2,729

Location: Warsaw, Poland

  • Send private message

20

Friday, October 22nd 2004, 10:12am

RE: Threw an exception

In this case LPCTSTR == LPCWSTR.