LPCSTR to LPCWSTR

This is a discussion on LPCSTR to LPCWSTR within the Windows Programming forums, part of the Platform Specific Boards category; I'm trying to perform the type conversion as shown in the subject title. Below shows the working copy of my ...

  1. #1
    Registered User
    Join Date
    Jul 2004
    Posts
    222

    LPCSTR to LPCWSTR

    I'm trying to perform the type conversion as shown in the subject title. Below shows the working copy of my code relevant to this conversion so far:

    Code:
    	LPCSTR lpszOutput = "c:\\PDFTron_before.pdf";
    
    	SHELLEXECUTEINFOW File;
    
    	File.cbSize = sizeof(SHELLEXECUTEINFOW);
    	File.hwnd = NULL;
    	File.lpVerb = L"open";
    	File.lpFile = lpszOutput;
    If I try using the green highlighted code, I'm getting the following error message:
    Code:
    1>c:\pdftron_vprint\shellscript\shellscript.cpp(20) : error C2440: '=' : cannot convert from 'LPCSTR' to 'LPCWSTR'
    1>        Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast
    If I try replacing the highlighted code with:
    Code:
    int byteWritten = MultiByteToWideChar(CP_ACP,
    								MB_PRECOMPOSED,
    								lpszOutput,
    								-1,
    								(LPWSTR) File.lpFile,
    								0);
    I'm getting a runtime error saying that File was used without being initialized. Does it imply that I should be allocating memory to File.lpFile?

  2. #2
    Registered User
    Join Date
    Jul 2004
    Posts
    222
    I think I have found a potential workaround for this. Below shows what I'm trying to do right now:

    Code:
    wstring fn = wstring(lpszOutput);
    However, I'm experiencing a problem with the conversion of LPCSTR to wstring. Any pointers on general methods on getting this conversion accomplished?

  3. #3
    Registered User
    Join Date
    Jul 2004
    Posts
    222
    nvm on this question. I did some hacks to get around this problem.

  4. #4
    int x = *((int *) NULL); Cactus_Hugger's Avatar
    Join Date
    Jul 2003
    Location
    Banks of the River Styx
    Posts
    902
    Why not just declare lpszOutput as a LPWSTR?

    Additionally, yes, you need allocate memory to File.lpFile, and that cast should not be needed. There is a way to call MultiByteToWideChar such that it will tell you how much memory to allocate. (Read MSDN docs)
    long time; /* know C? */
    Unprecedented performance: Nothing ever ran this slow before.
    Any sufficiently advanced bug is indistinguishable from a feature.
    Real Programmers confuse Halloween and Christmas, because dec 25 == oct 31.
    The best way to accelerate an IBM is at 9.8 m/s/s.
    recursion (re - cur' - zhun) n. 1. (see recursion)

  5. #5
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,538
    Here is my own ascii to utf16 function:

    Code:
    	Strings::CStringExW AnsiToUTF16(const Strings::CStringExA& strFrom)
    	{
    		Strings::CStringExW strTo;
    		int nLen = MultiByteToWideChar(CP_ACP, 0, strFrom.get(), -1, NULL, NULL);
    		wchar_t* strBuffer = strTo.GetBuffer(nLen + 1);
    		strBuffer[0] = 0;
    		CHECK_ERROR( MultiByteToWideChar(CP_ACP, 0, strFrom.get(), -1, strBuffer, nLen) );
    		strTo.ReleaseBuffer();
    		return strTo;
    	}
    Study the code well and all should be fine you'll see.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  6. #6
    Registered User
    Join Date
    May 2007
    Posts
    41
    Code:
    WCHAR lpszOutput[] = L"c:\\PDFTron_before.pdf";
    
    	SHELLEXECUTEINFOW File;
    
    	File.cbSize = sizeof(SHELLEXECUTEINFOW);
    	File.hwnd = NULL;
    	File.lpVerb = L"open";
    	File.lpFile = lpszOutput;
    Now it should work.

  7. #7
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,485
    Declare your variables with the Unicode compatibility declarations (and macros).
    Then you can flip between Ascii and Unicode without having to hack the code.

    http://msdn.microsoft.com/en-us/libr...61(VS.85).aspx
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  8. #8
    Registered User
    Join Date
    Jul 2008
    Posts
    66
    You can include the <tchar.h> header and use TCHAR szString [] = _T("I am a string"); or use the TEXT("") macro ...


    Greetz
    Greenhorn

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. How to convert string into LPCWSTR
    By krishnampkkm in forum C++ Programming
    Replies: 2
    Last Post: 06-15-2009, 06:02 AM
  2. a type problem (TCHAR, LPCWSTR)
    By Crazed in forum Windows Programming
    Replies: 6
    Last Post: 03-13-2008, 01:19 PM
  3. copy LPCSTR type
    By Gordon in forum Windows Programming
    Replies: 3
    Last Post: 10-17-2007, 09:33 PM
  4. LPCSTR and LPCWSTR
    By George2 in forum C Programming
    Replies: 2
    Last Post: 08-20-2007, 02:46 PM
  5. The Interactive Animation - my first released C program
    By ulillillia in forum A Brief History of Cprogramming.com
    Replies: 48
    Last Post: 05-10-2007, 02:25 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21