-
October 12th, 2005, 02:04 PM
#1
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
-
October 12th, 2005, 02:11 PM
#2
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.
-
October 12th, 2005, 02:45 PM
#3
Re: convert from 'char *' to 'LPCWSTR'
Worked perfectly, thank you
-
October 12th, 2005, 03:40 PM
#4
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.
-
October 12th, 2005, 06:06 PM
#5
Re: convert from 'char *' to 'LPCWSTR'
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
-
October 13th, 2005, 10:07 AM
#6
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.
-
October 14th, 2005, 02:03 AM
#7
Re: convert from 'char *' to 'LPCWSTR'
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.
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.
-
October 14th, 2005, 02:29 AM
#8
Re: convert from 'char *' to 'LPCWSTR'
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.
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
-
October 14th, 2005, 02:52 AM
#9
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
-
May 21st, 2007, 03:12 AM
#10
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? :/
-
May 21st, 2007, 04:52 AM
#11
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
- Guido
-
May 21st, 2007, 05:24 AM
#12
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);
-
May 21st, 2007, 06:11 AM
#13
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);
- Guido
-
May 21st, 2007, 07:46 AM
#14
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.
-
May 21st, 2007, 07:57 AM
#15
Re: convert from 'char *' to 'LPCWSTR'
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
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|