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
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
Re: convert from 'char *' to 'LPCWSTR'
Worked perfectly, thank you
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
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, :thumb:
i fixed the code. :wave:
Cheers
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.
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.
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 :D 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
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.:thumb: that's all
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? :/
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
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);
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);
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 .
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).