Click to See Complete Forum and Search --> : trying to convert char* to LPCWSTR


dragonuv
May 1st, 2010, 12:57 PM
I'm trying to use the following function:

DWORD test = GetTargetProcessIdFromWindow("Untitled - Notepad");


DWORD GetTargetProcessIdFromWindow(char * WindowName)
{
HWND targetWnd;
targetWnd = FindWindow(NULL,(LPCWSTR)WindowName);
DWORD procID = GetWindowThreadProcessId(targetWnd, NULL);
return procID;
}

but targetWnd always returns 00000. Does anyone know how to properly convert from a string to LPCWSTR ?

Chris_F
May 1st, 2010, 01:32 PM
I'm trying to use the following function:

DWORD test = GetTargetProcessIdFromWindow("Untitled - Notepad");


DWORD GetTargetProcessIdFromWindow(char * WindowName)
{
HWND targetWnd;
targetWnd = FindWindow(NULL,(LPCWSTR)WindowName);
DWORD procID = GetWindowThreadProcessId(targetWnd, NULL);
return procID;
}

but targetWnd always returns 00000. Does anyone know how to properly convert from a string to LPCWSTR ?

Your compiler can't be expected to somehow convert an ANSI string into unicode simply because you cast it as such, not that there is any reason to in the first place. You have one of two options.


DWORD test = GetTargetProcessIdFromWindow(L"Untitled - Notepad");


DWORD GetTargetProcessIdFromWindow(LPCWSTR WindowName)
{
HWND targetWnd;
targetWnd = FindWindow(NULL, WindowName);
DWORD procID = GetWindowThreadProcessId(targetWnd, NULL);
return procID;
}


OR


DWORD test = GetTargetProcessIdFromWindow("Untitled - Notepad");


DWORD GetTargetProcessIdFromWindow(LPCSTR WindowName)
{
HWND targetWnd;
targetWnd = FindWindowA(NULL, WindowName);
DWORD procID = GetWindowThreadProcessId(targetWnd, NULL);
return procID;
}


There are two versions of each Windows API function. One for ANSI strings, and one for unicode. They have the format MessageBoxA() and MessageBoxW() respectively. MessageBox() is a pseudonym for MessageBoxW().

Ajay Vijay
May 9th, 2010, 06:37 AM
If you are confused over LPCTSTR, LPWSTR etc. see this (http://www.codeproject.com/Tips/76252/What-are-TCHAR-WCHAR-LPSTR-LPWSTR-LPCTSTR-etc.aspx)