CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3
  1. #1
    Join Date
    May 2010
    Posts
    25

    trying to convert char* to LPCWSTR

    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 ?

  2. #2
    Join Date
    Aug 2008
    Posts
    902

    Re: trying to convert char* to LPCWSTR

    Quote Originally Posted by dragonuv View Post
    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.

    Code:
    DWORD test = GetTargetProcessIdFromWindow(L"Untitled - Notepad");
        
    
    DWORD GetTargetProcessIdFromWindow(LPCWSTR WindowName)
    {
    	HWND targetWnd;
    	targetWnd = FindWindow(NULL, WindowName);
    	DWORD procID = GetWindowThreadProcessId(targetWnd, NULL);
    	return procID;
    }
    OR

    Code:
    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().

  3. #3
    Join Date
    Mar 2003
    Location
    India {Mumbai};
    Posts
    3,871

    Re: trying to convert char* to LPCWSTR

    If you are confused over LPCTSTR, LPWSTR etc. see this
    My latest article: Explicating the new C++ standard (C++0x)

    Do rate the posts you find useful.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured