convert from 'char *' to 'LPCWSTR'
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Page 1 of 2 12 LastLast
Results 1 to 15 of 20

Thread: convert from 'char *' to 'LPCWSTR'

  1. #1
    Join Date
    Oct 2005
    Posts
    31

    convert from 'char *' to 'LPCWSTR'

    Hi,

    I am a total noob at c++.

    I have the following code:
    Code:
       CoInitialize (NULL);
    
       HRESULT hr;
       IActiveDesktop *pActiveDesktop;
       char* wpurl;
    
       wpurl = GrabTextFromEdit(IDE_WPURL);
       hr = CoCreateInstance(CLSID_ActiveDesktop, NULL, CLSCTX_INPROC_SERVER, IID_IActiveDesktop, (void**)&pActiveDesktop);
       //OLECHAR szFile [] = OLESTR(wpurl);
       hr = pActiveDesktop->SetWallpaper(wpurl, 0);
    
       pActiveDesktop->ApplyChanges(AD_APPLY_ALL);
       pActiveDesktop->Release();
    
       MessageBox(m_hWnd, "The wallpaper has been changed", "",MB_OK);
       return true;
    When i run this code i get this error:
    error C2664: 'IActiveDesktop::SetWallpaper' : cannot convert parameter 1 from 'char *' to 'LPCWSTR'

    So i have tried to covert the wpurl variable to a LPCWSTR with the mbstowcs function but couldnt figure out how to implement it.

    Can anyone help me to covert the character?

    Thanks alot i advnance

  2. #2
    Join Date
    May 2005
    Posts
    4,950

    Re: convert from 'char *' to 'LPCWSTR'

    try this:

    Code:
    char* wpurl;
    wpurl = GrabTextFromEdit(IDE_WPURL);
    
    if ( wpurl )
    {
      int len = strlen(wpurl)+1;
      wchar_t *wText = new wchar_t[len];
      if ( wText == 0 )
        return;
      memset(wText,0,len);
      ::MultiByteToWideChar(  CP_ACP, NULL,wpurl, -1, wText,len );
    
      //now pass wText
      hr = pActiveDesktop->SetWallpaper(wText, 0);
    
      
      // when finish using wText dont forget to delete it
      delete []wText;
    
    }
    Cheers
    Last edited by golanshahar; October 12th, 2005 at 06:07 PM.
    If a post helped you dont forget to "Rate This Post"

    My Article: Capturing Windows Regardless of Their Z-Order

    Cheers

  3. #3
    Join Date
    Oct 2005
    Posts
    31

    Re: convert from 'char *' to 'LPCWSTR'

    Worked perfectly, thank you

  4. #4
    Join Date
    Aug 2002
    Location
    Madrid
    Posts
    4,588

    Re: convert from 'char *' to 'LPCWSTR'

    Actually this can lead to memory corruption if your string is longer than MAX_PATH characters. For a better way, and more explanations, check out this FAQ
    Get this small utility to do basic syntax highlighting in vBulletin forums (like Codeguru) easily.
    Supports C++ and VB out of the box, but can be configured for other languages.

  5. #5
    Join Date
    May 2005
    Posts
    4,950

    Re: convert from 'char *' to 'LPCWSTR'

    Quote Originally Posted by Yves M
    Actually this can lead to memory corruption if your string is longer than MAX_PATH characters. For a better way, and more explanations, check out this FAQ
    Yup, you are right, good point,

    i fixed the code.

    Cheers
    If a post helped you dont forget to "Rate This Post"

    My Article: Capturing Windows Regardless of Their Z-Order

    Cheers

  6. #6
    Join Date
    Aug 2002
    Location
    Madrid
    Posts
    4,588

    Re: convert from 'char *' to 'LPCWSTR'

    That's actually not always correct either. The length of the multi-byte string is not always the same as the 8-bit string, because the 8-bit string may not be ASCII (but e.g. UTF-8, etc.) So this solution only works with codepages which map 1 character to 1 character in Unicode.

    Check the second part of the FAQ for that.
    Get this small utility to do basic syntax highlighting in vBulletin forums (like Codeguru) easily.
    Supports C++ and VB out of the box, but can be configured for other languages.

  7. #7
    Join Date
    May 2005
    Location
    Oregon
    Posts
    3,716

    Lightbulb Re: convert from 'char *' to 'LPCWSTR'

    Quote Originally Posted by mexicaan
    hr = CoCreateInstance(CLSID_ActiveDesktop, NULL,
    CLSCTX_INPROC_SERVER, IID_IActiveDesktop, (void**)
    &pActiveDesktop);
    //OLECHAR szFile [] = OLESTR(wpurl);
    hr = pActiveDesktop->SetWallpaper(wpurl, 0);
    Before calling your method mean to say at the time when u used ::CocreateInstance.you should check whether function succeed or not after that only gave a call to your com function
    Code:
    hr = CoCreateInstance(CLSID_ActiveDesktop, NULL, 
    CLSCTX_INPROC_SERVER, IID_IActiveDesktop, (void**)
    &pActiveDesktop);
    if(SUCCEEDED(hr))
      //call your SetWalpaper function now.
    Quote Originally Posted by golanshahar
    try this:
    Code:
     
    char* wpurl;
    wpurl = GrabTextFromEdit(IDE_WPURL);
    Cheers
    when ever declare a pointer .u should initialize it other wise u are going to get some big problem.

    Second thing whenever u used new to allocate memory .i think u should initialize it with memset so proper initialization can take place because new allocate memory for you.but initialization doesn't take place in this case.which is some time going to gave problem.

  8. #8
    Join Date
    May 2005
    Posts
    4,950

    Re: convert from 'char *' to 'LPCWSTR'

    Quote Originally Posted by humptydumpty

    when ever declare a pointer .u should initialize it other wise u are going to get some big problem.
    basically you are right however in this code the second line after declaring the pointer it is being set to a value returned from the
    Code:
    GrabTextFromEdit(IDE_WPURL);
    which btw i dont know what it is cause its OP function.
    so even if i would set it to 0 second line its value would have changed.

    Quote Originally Posted by humptydumpty
    Second thing whenever u used new to allocate memory .i think u should initialize it with memset so proper initialization can take place because new allocate memory for you.but initialization doesn't take place in this case.which is some time going to gave problem.
    well i am doing it...look again in the code

    Cheers
    If a post helped you dont forget to "Rate This Post"

    My Article: Capturing Windows Regardless of Their Z-Order

    Cheers

  9. #9
    Join Date
    May 2005
    Location
    Oregon
    Posts
    3,716

    Lightbulb Re: convert from 'char *' to 'LPCWSTR'

    yes u r using for wtext but not for wpurl.and i am saying in case of wpurl and if u decalre a simple pointer in c++ and just check it out .mean to say without initialize just try to put some value in the pointer u r going to get exception. that's all

  10. #10
    Join Date
    May 2007
    Posts
    2

    Re: convert from 'char *' to 'LPCWSTR'

    Hi i have that code it works on DevCpp but in Visual Studio gives error.

    //______________________________________________

    #include <cstdio> // getchar()
    #include <iostream>
    #include <windows.h> // PlaySound()

    #define SND_FILENAME 0x20000 // from mmsystem.h
    #define SND_LOOP 8
    #define SND_ASYNC 1
    #define SND_ALIAS 0x10000

    int main()
    {
    char sound[] = "C:/ohhnooo.wav";

    PlaySound(sound,NULL,SND_FILENAME|SND_ASYNC);

    getchar();

    return 0;
    }

    //______________________________________________


    The Error is :
    error C2664: 'PlaySoundW' : cannot convert parameter 1 from 'char [15]' to 'LPCWSTR'

    What is the problem? :/

  11. #11
    Join Date
    Nov 2006
    Location
    Essen, Germany
    Posts
    1,344

    Re: convert from 'char *' to 'LPCWSTR'

    There are two possible solutions:

    1) convert the filename from MultiByte string to WideString using MultiByteToWideChar

    2) change your project settings from Unicode to MultiByte strings

  12. #12
    Join Date
    May 2007
    Posts
    2

    Re: convert from 'char *' to 'LPCWSTR'

    i tried them. A friend solved it.

    here's the solution:

    typedef wchar_t WCHAR ;

    typedef CONST WCHAR *LPCWSTR;

    wchar_t sound[]=L"C:/ohhnooo.wav";

    PlaySoundW(sound,NULL,SND_FILENAME|SND_ASYNC);

  13. #13
    Join Date
    Nov 2006
    Location
    Essen, Germany
    Posts
    1,344

    Re: convert from 'char *' to 'LPCWSTR'

    Ah, ok, I missed the third solution.

    Instead of using L"string" you better use the TEXT macro which automatically converts static text to the appropriate type.

    Code:
    ::PlaySoundW( TEXT( "c:\\ohhnooo.wav", NULL, SND_FILENAME|SND_ASYNC);

  14. #14
    Join Date
    Apr 2004
    Location
    England, Europe
    Posts
    2,492

    Re: convert from 'char *' to 'LPCWSTR'

    A general note:


    Many COM functions take a BSTR, not a LPCWSTR, and BSTRs need to be allocated using SysAllocString and are freed using SysFreeString .
    Last edited by Zaccheus; May 21st, 2007 at 07:49 AM.
    My hobby projects:
    www.rclsoftware.org.uk

  15. #15
    Join Date
    Mar 2002
    Location
    NY, USA
    Posts
    12,097

    Re: convert from 'char *' to 'LPCWSTR'

    Quote Originally Posted by Zaccheus
    A general note:


    Many COM functions take a BSTR, not a LPCWSTR, and BSTRs need to be allocated using SysAllocString and are freed using SysFreeString .
    And both operations need to take place within the same context. (i.e. You can not Alloc from one heap, and Free to a different heap).
    TheCPUWizard is a registered trademark, all rights reserved. (If this post was helpful, please RATE it!)
    2008, 2009
    In theory, there is no difference between theory and paractice; in practice there is.

    * Join the fight, refuse to respond to posts that contain code outside of [code] ... [/code] tags. See here for instructions
    * How NOT to post a question here
    * Of course you read this carefully before you posted
    * Need homework help? Read this first

Page 1 of 2 12 LastLast

Posting Permissions

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


Azure Activities Information Page

Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center