473,385 Members | 1,492 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Issue while converting LPTSTR to char *

Hi All,

I am trying to convert LPTSTR to char* by using sprintf,
But sprintf copies only the first character from the string. i tried both array and pointer also , but the result is same. Here is my code...

LPTSTR TestStr= _T("Test String");
char s1[1000];
char *s2;
s2 = (char *) malloc(1000*sizeof(char));
sprintf(s1,"%s",TestStr);
sprintf(s2,"%s",TestStr);

Both s1 and s2 stores only 'T'.

Kindly help me in this regard...
Thanks for your time...

Regards
PSKumaran.
Nov 23 '07 #1
6 14286
AHMEDYO
112 100+
HI...

you using unicode or muli-byte characters??

Best Regards
Nov 23 '07 #2
Hi ,

Thanks for the reply.

I use UNICODE.

Regards
PSKumaran.
Nov 23 '07 #3
xoinki
110 100+
hi,
If u have compiled your program with UNICODE and _UNICODE defined, then LPTSTR will automatically become LPWSTR (Long pointer to a wide string) u cant assign this to char* but u can assign this to a variable of type wchar_t*.

Thnx
Xoinki
Nov 23 '07 #4
xoinki
110 100+
hi,
after using wchar_t* use swprintf() to get proper output.

Thnx
Xoinki
Nov 23 '07 #5
Thanks a lot Xoinki for the timely help.

I used wchar_t* as you suggested, It works fine.

Regards
PSKumaran.
Nov 23 '07 #6
weaknessforcats
9,208 Expert Mod 8TB
LPTSTR TestStr= _T("Test String");
char s1[1000];
char *s2;
s2 = (char *) malloc(1000*sizeof(char));
sprintf(s1,"%s",TestStr);
sprintf(s2,"%s",TestStr);
You could have avoided this whole mess by not using char in the first place. The whole point of the TCHAR mappings is to seamlessly switch between char and wchar_t based on the project settings.

That means once you use a TCHAR macro you cannot use char or wchar_t in your code. You fixed this by using wchar_t and now your code won't work with char.

You should have used TCHAR, _stprintf() and TEXT:
Expand|Select|Wrap|Line Numbers
  1. LPTSTR TestStr= TEXT("Test String");
  2. TCHAR s1[1000];
  3. PTCHAR s2;
  4. s2 = (PTCHAR) malloc(1000*sizeof(TCHAR));
  5. _stprintf(s1,TEXT("%s"),TestStr);
  6. _stprintf(s2,TEXT("%s"),TestStr);
  7.  
Read this http://msdn2.microsoft.com/en-us/library/ms860358.aspx.
Nov 24 '07 #7

Sign in to post your reply or Sign up for a free account.

Similar topics

4
by: sorty | last post by:
Hi, I have read in many places that TCHAR can be 'char' or 'wchar_t' depending on ANSI or UNICODE. I have also read that LPTSTR is a long pointer to a TCHAR. I am confused about the...
9
by: alex | last post by:
how do i initialize an LPTSTR variable? i'm trying to call a microsoft function LONG QueryStringValue( LPCTSTR pszValueName, LPTSTR pszValue, ULONG* pnChars ) throw( );pszValue is supposed...
3
by: free2cric | last post by:
Hi , is this correct to have final LPTSTR to char * CString fname(argv ); LPTSTR p1 = fname.GetBuffer(fname.GetLength()); char teststr; sprintf(teststr,"%s",p1); //or strcpy(teststr,p1); ...
1
by: Steven Blair | last post by:
Hi there. Having a problem calling a win32 function from a dll. Here is the function header: DWORD thrdGetHcNum (DWORD nPort, DWORD nBaudrate, LPTSTR lpszHcNum) //Description of the 3rd...
0
by: Nils M. Lunde | last post by:
Hi! I have an unmanaged function: BOOL WINAPI UnmanagedClass::Function(LPTSTR sz1, LPTSTR sz2, LPTSTR sz3, HANDLE& hToken) . This I want to call from managed code. In managed code I have a...
1
by: Lewap | last post by:
Hi! I've piece of code like follow: <code> LPTSTR lpszPipename = (LPTSTR) "\\\\.\\pipe\\testpipe"; hPipe = CreateNamedPipe(
5
by: Sonu | last post by:
Hello everyone and thanks in advance. I have a multilingual application which has been built in MFC VC++ 6.0 (non-Unicode). It support English German Hungarian so far, which has been fine. But...
9
by: Hugo Amselschlag | last post by:
Hi there, I've implemented a local system hook to suppress certain windows beeing displayed by the axWebbrowser control. Now I need some more information before I can decide, whether to suppress...
2
by: sudhivns | last post by:
Hi, How to convert LPTSTR to char*.? Thanks in advance. Bye
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.