473,574 Members | 2,168 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Issue while converting LPTSTR to char *

6 New Member
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*siz eof(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 14312
AHMEDYO
112 New Member
HI...

you using unicode or muli-byte characters??

Best Regards
Nov 23 '07 #2
pskumaran
6 New Member
Hi ,

Thanks for the reply.

I use UNICODE.

Regards
PSKumaran.
Nov 23 '07 #3
xoinki
110 New Member
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 New Member
hi,
after using wchar_t* use swprintf() to get proper output.

Thnx
Xoinki
Nov 23 '07 #5
pskumaran
6 New Member
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 Recognized Expert Moderator Expert
LPTSTR TestStr= _T("Test String");
char s1[1000];
char *s2;
s2 = (char *) malloc(1000*siz eof(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
10176
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 following. Please answer with yes/no and then elaborate as you please..
9
25776
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 to get the value i want but i need to initialize it before i pass it to QueryStringValue, how do i initialize
3
12574
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); is this code to have my null terminated teststr.
1
5974
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 parameter LpszHcNum: The head of the buffer area used to store the serial number of the terminal, 9 bytes at least.
0
1051
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 variable, IntPtr pToken, that I wish to use for the hToken parameter in the unmanaged function, but I can't manage to convert it to type HANDLE. Could...
1
2609
by: Lewap | last post by:
Hi! I've piece of code like follow: <code> LPTSTR lpszPipename = (LPTSTR) "\\\\.\\pipe\\testpipe"; hPipe = CreateNamedPipe(
5
2529
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 now I need it to work on Russian computers and I realized that the application should be converted to Unicode to work in Russian. I am totally new...
9
5656
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 a window or not. My callback function get a long pointer (lParam) to a structure which contains further information. This structure is described...
2
17622
by: sudhivns | last post by:
Hi, How to convert LPTSTR to char*.? Thanks in advance. Bye
0
7799
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7716
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
8042
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
1
7806
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
5297
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3739
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3748
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1333
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
1053
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.