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

Hybrid View

  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,954

    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,954

    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,725

    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,954

    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,725

    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
    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

  12. #12
    Join Date
    Mar 2002
    Location
    St. Petersburg, Florida, USA
    Posts
    12,125

    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,2010
    In theory, there is no difference between theory and practice; 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

  13. #13
    Join Date
    Feb 2007
    Posts
    186

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

    I feel your pain on string conversions. c++ overly complicates strings. They have a string class, but it doesnt actually convert all string types from and to every other type. I dont understand why converting strings is the huge pain it is unless its just me not understanding why there are 10 different types of strings.

  14. #14
    Join Date
    Apr 2009
    Posts
    1

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

    Greetings everyone, I have a problem, I'm just starting to work with Ogre Visual Studio 2005 and I have a problem with a conversion of a char * THIS CODE LPCWSRT ... ....


    int main(int argc, char **argv)
    #endif
    {
    // Create application object
    TutorialApplication app;

    try
    {
    app.go();
    }
    catch( Exception& e )
    {

    #if OGRE_PLATFORM == PLATFORM_WIN32 || OGRE_PLATFORM == OGRE_PLATFORM_WIN32
    MessageBox(NULL, e.getFullDescription().c_str(), "An exception has occurred!", MB_OK | MB_ICONERROR | MB_TASKMODAL);

    #else
    fprintf(stderr, "An exception has occurred: &#37;s\n",
    e.getFullDescription().c_str());
    #endif
    }

    return 0;
    }

    and this is the error I get ...

    error C2664: 'MessageBoxW' : cannot convert parameter 2 from 'const char *' to 'LPCWSTR'
    Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast

  15. #15
    Join Date
    Aug 2007
    Posts
    858

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


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
  •  





Click Here to Expand Forum to Full Width

Featured