Thread: WChar to char

  1. #1
    Registered User
    Join Date
    Jan 2006
    Location
    England, Norwich
    Posts
    38

    WChar to char

    Hello,

    i'm trying to use COM to interface with iactivedesktop inorder to change wallpaper on my desktop but i seemed to be having problems converting a WChar to a Char or somthing along thoghts lines.

    My code:
    Code:
    #include <stdio.h>
    #include <Windows.h>
    #include <WinInet.h>
    #include <ShlObj.h>
    #include <iostream>
    
    using namespace std;
    
    int main ()
    {
    	CoInitialize (0) ;
    
    	HRESULT hr;
    	IActiveDesktop *pActiveDesktop;
    
    	hr = CoCreateInstance(CLSID_ActiveDesktop, NULL, CLSCTX_INPROC_SERVER,
    						IID_IActiveDesktop, (void**)&pActiveDesktop);
    
    		
    	LPCWSTR  FilePath[77];	
    	FilePath = "C:/Documents and Settings/Michael Knights/My Documents/My Pictures/board.bmp";	
    	
    	pActiveDesktop -> SetWallpaper (FilePath,77);
    
    
    	WCHAR wcTemp[512] ;
    	hr = pActiveDesktop -> GetWallpaper (wcTemp, 512, 0);
    
    	MessageBoxW (0, wcTemp, 0, 0);
    
    	//cout << wcTemp;
    
    
    	pActiveDesktop->Release();
    
    	CoUninitialize ();
    
    	return 0;
    }
    the error's i get from my complier are:
    Cpp1.cpp(23) : error C2734: 'FilePath' : const object must be initialized if not extern
    Cpp1.cpp(24) : error C2440: '=' : cannot convert from 'char [77]' to 'const unsigned short [77]'
    There is no context in which this conversion is possible


    I would be greatful for any help,

    Chears.

  2. #2
    Registered User
    Join Date
    Aug 2005
    Posts
    1,267
    assuming UNICODE is defined TEXT macro will convert from char* to wchar_t*.

    Code:
    LPCWSTR  FilePath = TEXT( "C:/Documents and Settings/Michael Knights/My Documents/My Pictures/board.bmp");
    If UNICODE is not defined you can force the coversion by using L
    Code:
    LPCWSTR  FilePath = L"C:/Documents and Settings/Michael Knights/My Documents/My Pictures/board.bmp";
    There are a few win32 api functions that do not like the forward *nix-style path separaters, but all of them use the normal MS-Windows style back-slashes.
    Last edited by Ancient Dragon; 04-16-2006 at 12:41 PM.

  3. #3
    Registered User
    Join Date
    Jan 2006
    Location
    England, Norwich
    Posts
    38
    Tryed it and i got the same errors.

  4. #4
    Registered User
    Join Date
    Aug 2005
    Posts
    1,267
    Quote Originally Posted by MK4554
    Tryed it and i got the same errors.

    repost code -- it works with VC++ 2005 Express. what version of VC++ are you using? you may need to include <tchar.h>

  5. #5
    Registered User
    Join Date
    Jan 2006
    Location
    England, Norwich
    Posts
    38
    Quote Originally Posted by Ancient Dragon
    repost code -- it works with VC++ 2005 Express. what version of VC++ are you using? you may need to include <tchar.h>
    Code:
    #include <stdio.h>
    #include <Windows.h>
    #include <WinInet.h>
    #include <ShlObj.h>
    #include <iostream>
    #include <tchar.h>
    
    using namespace std;
    
    int main ()
    {
    	CoInitialize (0) ;
    
    	HRESULT hr;
    	IActiveDesktop *pActiveDesktop;
    	
    
    
    
    	hr = CoCreateInstance(CLSID_ActiveDesktop, NULL, CLSCTX_INPROC_SERVER,
    						IID_IActiveDesktop, (void**)&pActiveDesktop);
    
    		
    	LPCWSTR  FilePath[77];	
    	FilePath =  TEXT( "C:/Documents and Settings/Michael Knights/My Documents/My Pictures/board.bmp");	
    	
    	pActiveDesktop -> SetWallpaper (FilePath,77);
    
    
    	WCHAR wcTemp[512] ;
    	hr = pActiveDesktop -> GetWallpaper (wcTemp, 512, 0);
    
    	MessageBoxW (0, wcTemp, 0, 0);
    
    	//cout << wcTemp;
    
    
    	pActiveDesktop->Release();
    
    	CoUninitialize ();
    
    	return 0;
    }


    hey,
    i'm useing vs6 ( yeh i know ) and i just tryed what u told me and it didnt work hmm i dont know what else to try....
    Last edited by MK4554; 04-16-2006 at 03:08 PM.

  6. #6
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    Code:
    LPCWSTR  FilePath[77];	
    	FilePath =  TEXT( "C:/Documents and Settings/Michael Knights/My Documents/My Pictures/board.bmp");
    for arrays, you can only make direct assignment during initialization. once created, your only option is to copy the data manually (using wstrcpy, memcpy, etc). of course, you could use a std::wstring instead which would allow you to use that construct.
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

  7. #7

  8. #8
    Registered User
    Join Date
    Aug 2005
    Posts
    1,267
    [QUOTE=MK4554]
    Code:
    	LPCWSTR  FilePath[77];	
    	FilePath =  TEXT( "C:/Documents and Settings/Michael Knights/My Documents/My Pictures/board.bmp");
    That is not like the example I posted. character arrays cannot be assigned that way. See the example I posted for the correct method. Another way is like this
    Code:
    LPCWSTR FilePath[] = TEXT( "C:/Documents and Settings/Michael Knights/My Documents/My Pictures/board.bmp");
    If FilePath was declared someplace else, then use one of the conversion functions previously posted.

  9. #9
    Registered User
    Join Date
    Jan 2006
    Location
    England, Norwich
    Posts
    38
    hey,
    i found what my problem is now its that

    pActiveDesktop -> SetWallpaper (FilePath,77);

    doesnt work probly with varibles i dunno why but it does the only way i can get it to work is by doing somthing like

    pActiveDesktop -> SetWallpaper (L"C:\joke.jpg",0);

    does anyone know how to use a varable insade of this.

  10. #10
    Registered User
    Join Date
    Aug 2005
    Posts
    1,267
    this is from a project I got from codeproject.com. I illustrates the use of that and other functions
    Code:
    void CWallpaperQDlg::ChangeWallpaper(int iRandom)
    {
    	USES_CONVERSION;
    
    	int iImageNum = 0;
    	int iPosition = 1;
    	CString cstrTileWallpaper;
    	CString cstrWallpaperStyle;
    	TCHAR szBuf[MAX_PATH];
    
    	// Choose then next image in the list, or choose a random image.
    	if (iRandom == 1)
    	{
    		srand((unsigned)time(NULL));
    
    		iImageNum = rand()%m_lbQList.GetCount();
    
    		iCurrentImage = iImageNum;
    	}
    	else
    	{
    		iCurrentImage++;
    		if (iCurrentImage + 1 > m_lbQList.GetCount()) iCurrentImage = 0;
    		CRegistry::SetRegDWORD("Software\\JasonSoft\\WallpaperQ", "CurrentImage", (DWORD)iCurrentImage);
    
    		iImageNum = iCurrentImage;
    	}
    
    	// Get the image path from the registry by the image number.
    	iPosition = (int)m_lbQList.GetItemData(iImageNum);
    	m_lbQList.GetText(iImageNum, szBuf);
    
    	if (szBuf[0] != '/0')
    	{
    		// If Active Desktop is not available, use the SystemParametersInfo function.
    		if (pIActiveDesktop == NULL)
    		{
    			if (iPosition == 0)
    			{
    				// Tiled
    				cstrTileWallpaper = "1";
    				cstrWallpaperStyle = "1";
    			}
    			else if (iPosition == 1)
    			{
    				// Center
    				cstrTileWallpaper = "0";
    				cstrWallpaperStyle = "1";
    			}
    			else
    			{
    				// Stretch
    				cstrTileWallpaper = "0";
    				cstrWallpaperStyle = "2";
    			}
    
    			// Set registry values for the wallpaper position.
    			CRegistry::SetRegString("Control Panel\\Desktop", "TileWallpaper", cstrTileWallpaper);
    			CRegistry::SetRegString("Control Panel\\Desktop", "WallpaperStyle", cstrWallpaperStyle);
    
    			// Change the wallpaper.
    			SystemParametersInfo(SPI_SETDESKWALLPAPER, 0, (void*)szBuf, 
    				SPIF_UPDATEINIFILE | SPIF_SENDWININICHANGE);	
    		}
    		else
    		{
    			WALLPAPEROPT wpOptions;
    			COMPONENTSOPT compOptions;
    
    			compOptions.dwSize = sizeof(COMPONENTSOPT);
    			compOptions.fActiveDesktop = TRUE;
    			compOptions.fEnableComponents = TRUE;
    
    			pIActiveDesktop->SetDesktopItemOptions(&compOptions, 0);
    
    			// Set the wallpaper position.
    			wpOptions.dwSize = sizeof(WALLPAPEROPT);
    			if (iPosition == 0)
    				wpOptions.dwStyle = WPSTYLE_TILE;
    			else if (iPosition == 1)
    				wpOptions.dwStyle = WPSTYLE_CENTER;
    			else
    				wpOptions.dwStyle = WPSTYLE_STRETCH;
    
    			pIActiveDesktop->SetWallpaperOptions(&wpOptions, 0);
    
    			// Set the wallpaper image.
    
    			pIActiveDesktop->SetWallpaper(T2CW(szBuf), 0);
    
    			// Apply all changes.
    			pIActiveDesktop->ApplyChanges(AD_APPLY_ALL);
    		}
    
    		if (iExitAfter == 1) OnExit();
    	}
    }

  11. #11
    Registered User
    Join Date
    Jan 2006
    Location
    England, Norwich
    Posts
    38
    what the hell is T2CW?

  12. #12
    Registered User
    Join Date
    Aug 2005
    Posts
    1,267
    A macro that converts from char* to wchar_t*

  13. #13
    Registered User
    Join Date
    Jan 2006
    Location
    England, Norwich
    Posts
    38
    do you know how i would go about including it - i did search the internet befoure asking.

  14. #14
    Registered User
    Join Date
    Aug 2005
    Posts
    1,267
    You might want to read this article about unicode conversions. That macro is defined in atlconv.h

  15. #15
    Registered User
    Join Date
    Jan 2006
    Location
    England, Norwich
    Posts
    38
    Quote Originally Posted by Ancient Dragon
    You might want to read this article about unicode conversions. That macro is defined in atlconv.h
    ok i hade ago at using it but when i did i got header file errors such as:
    c:\program files\microsoft visual studio\vc98\include\wininet.h(48) : error C2146: syntax error : missing ';' before identifier 'HINTERNET'
    c:\program files\microsoft visual studio\vc98\include\wininet.h(48) : fatal error C1004: unexpected end of file found
    Error executing cl.exe.
    is it possable if somone isnt too busy that they could write me a quick example of using T2CW.

    btw Thank you for that link i read the whole page i must addmit i didnt understand everything but most of it i did.

    Edit:

    btw i figured out that setwallpaper has this syntax

    HRESULT SetWallpaper( LPCWSTR pwszWallpaper,
    DWORD dwReserved
    );

    i don't think it helps much but i may was well post it
    Last edited by MK4554; 04-19-2006 at 05:28 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Sorting Linked Lists
    By DKING89 in forum C Programming
    Replies: 6
    Last Post: 04-09-2008, 07:36 AM
  2. Conversion Char To Char * Problem
    By ltanusaputra in forum Windows Programming
    Replies: 3
    Last Post: 03-01-2008, 02:06 PM
  3. Wierd Segmentation Faults on Global Variable
    By cbranje in forum C Programming
    Replies: 6
    Last Post: 02-19-2005, 12:25 PM
  4. I'm having a problem with data files.
    By OmniMirror in forum C Programming
    Replies: 4
    Last Post: 05-14-2003, 09:40 PM
  5. String sorthing, file opening and saving.
    By j0hnb in forum C Programming
    Replies: 9
    Last Post: 01-23-2003, 01:18 AM