473,805 Members | 2,129 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Char * to LPWSTR

Hello!

I am trying to convert a char * to a LPWSTR, and I am going absolutly
mad! I can't find anything besides typle L"string" Unfortunetaly I
can't use that...

basicaly the setup is

callbackFromAno therFunctionWhi chMustBeOfChar( char* helpMe) {
//...
foo(some LPWSTR conversion);
}

this is the closest I have come to acomplishing this

char* convertMe = new char[sizeof(material s[i].pTextureFilena me)];
strcpy(convertM e,materials[i].pTextureFilena me);
wchar_t fileNameBuff[1];
int buffSize =
MultiByteToWide Char(CP_ACP,0,c onvertMe,strlen (convertMe),fil eNameBuff,0);
LPWSTR gah = (LPWSTR)new wchar_t[buffSize];
MultiByteToWide Char(CP_ACP,0,c onvertMe,strlen (convertMe),gah ,buffSize);

it converts all of the characters but I get a bunch of junk at the end
and I can't open the file.

Thanks :)

Nov 25 '06 #1
6 38989
* Ma************* @gmail.com:
callbackFromAno therFunctionWhi chMustBeOfChar( char* helpMe) {
//...
foo(some LPWSTR conversion);
}
Terribly off-topic here as it's not about C++, but about stupid MFC/ATL, but
you might want to peek at i

http://msdn2.microsoft.com/en-us/lib...a3(VS.80).aspx

--
Martijn van Buul - pi**@dohd.org
Nov 25 '06 #2
Ma************* @gmail.com wrote:
Hello!

I am trying to convert a char * to a LPWSTR, and I am going absolutly
mad! I can't find anything besides typle L"string" Unfortunetaly I
can't use that...
L"string" is an array of wchar_t characters.
The topical question is how to change an array of
char to an array of wchar_t.

char narrowarray[] = "Mairzy doats and dozy doats.";
wchar_t widearray[100]
mbstowcs(widear ray, narrowarray, 100);

LPSTR is a Microsoft abomination
Nov 25 '06 #3
L"string" is an array of wchar_t characters.
The topical question is how to change an array of
char to an array of wchar_t.

char narrowarray[] = "Mairzy doats and dozy doats.";
wchar_t widearray[100]
mbstowcs(widear ray, narrowarray, 100);

LPSTR is a Microsoft abomination
Here Here! Thank you so much for the answer! I now have a curiosity
question if any are willing to indulge, to solve the problem i had
earlier I used

strcpy(convertM e,materials[i].pTextureFilena me);
wchar_t fileNameBuff[1];
int buffSize
=MultiByteToWid eChar(CP_ACP,0, convertMe,strle n(convertMe),fi leNameBuff,0);
LPWSTR gah = (LPWSTR)new wchar_t[buffSize];
MultiByteToWide Char(CP_ACP,0,c onvertMe,strlen (convertMe),gah ,buffSize);
/*LOOK HERE!!*/
wstring final;
for(int i = 0; i < sizeof(convertM e)*2+1;i++) {
final.push_back (gah[i]);

Can anyone tell me, why did I have to use a size of 2 + 1? It makes NO
sence at all to me, I mean I understand a wchar is twice as many bytes,
however in traversing the array does it access each part per byte
(char)? And if so then how does push_back understand it's getting char
and not wchar? Even when it's clearly being fed a wchar... I should
be able to say final.push_back (L"w"); not final.push_back "(L"w");
final.push_back (0); or whatever it be

thanks ;)

Matt

Nov 25 '06 #4
<Ma************ *@gmail.comwrot e in message
news:11******** **************@ h54g2000cwb.goo glegroups.com.. .
Hello!

I am trying to convert a char * to a LPWSTR, and I am going absolutly
mad! I can't find anything besides typle L"string" Unfortunetaly I
can't use that...

basicaly the setup is

callbackFromAno therFunctionWhi chMustBeOfChar( char* helpMe) {
//...
foo(some LPWSTR conversion);
}
Matt,
You should probably post this to: microsoft.publi c.vc.language

Your problem is that you are not copying the terminating null.
Try this :

int buffSize = MultiByteToWide Char(CP_ACP, 0, convertMe,
strlen(convertM e)+1, NULL, 0);
LPWSTR gah = new wchar_t[buffSize];
MultiByteToWide Char(CP_ACP, 0, convertMe, strlen(convertM e)+1, gah,
buffSize);

or, even easier:

int buffSize = (int)strlen(con vertMe) + 1;
LPWSTR gah = new wchar_t[buffSize];
MultiByteToWide Char(CP_ACP, 0, convertMe, buffSize, gah, buffSize);

Now gah will have the null-terminated Unicode string.

Dag
Nov 25 '06 #5
On 25 Nov 2006 07:54:51 -0800, Ma************* @gmail.com wrote in
comp.lang.c++:
Hello!

I am trying to convert a char * to a LPWSTR, and I am going absolutly
mad! I can't find anything besides typle L"string" Unfortunetaly I
can't use that...

basicaly the setup is

callbackFromAno therFunctionWhi chMustBeOfChar( char* helpMe) {
//...
foo(some LPWSTR conversion);
}

this is the closest I have come to acomplishing this

char* convertMe = new char[sizeof(material s[i].pTextureFilena me)];
strcpy(convertM e,materials[i].pTextureFilena me);
wchar_t fileNameBuff[1];
int buffSize =
MultiByteToWide Char(CP_ACP,0,c onvertMe,strlen (convertMe),fil eNameBuff,0);
LPWSTR gah = (LPWSTR)new wchar_t[buffSize];
MultiByteToWide Char(CP_ACP,0,c onvertMe,strlen (convertMe),gah ,buffSize);

it converts all of the characters but I get a bunch of junk at the end
and I can't open the file.

Thanks :)
You say you can't use standard C++ features, and your code uses
non-standard, Windows specific types and API functions, all off-topic
here.

You need to ask this in a Windows group, such as
news:comp.os.ms-windows.program mer.win32.

--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://c-faq.com/
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.l earn.c-c++
http://www.contrib.andrew.cmu.edu/~a...FAQ-acllc.html
Nov 27 '06 #6
Ma************* @gmail.com wrote:
>L"string" is an array of wchar_t characters.
The topical question is how to change an array of
char to an array of wchar_t.

char narrowarray[] = "Mairzy doats and dozy doats.";
wchar_t widearray[100]
mbstowcs(widea rray, narrowarray, 100);

LPSTR is a Microsoft abomination

Here Here! Thank you so much for the answer! I now have a curiosity
question if any are willing to indulge, to solve the problem i had
earlier I used

strcpy(convertM e,materials[i].pTextureFilena me);
wchar_t fileNameBuff[1];
int buffSize
=MultiByteToWid eChar(CP_ACP,0, convertMe,strle n(convertMe),fi leNameBuff,0);
LPWSTR gah = (LPWSTR)new wchar_t[buffSize];
MultiByteToWide Char(CP_ACP,0,c onvertMe,strlen (convertMe),gah ,buffSize);
/*LOOK HERE!!*/
wstring final;
for(int i = 0; i < sizeof(convertM e)*2+1;i++) {
final.push_back (gah[i]);

Can anyone tell me, why did I have to use a size of 2 + 1? It makes NO
sence at all to me, I mean I understand a wchar is twice as many bytes,
however in traversing the array does it access each part per byte
(char)? And if so then how does push_back understand it's getting char
and not wchar? Even when it's clearly being fed a wchar... I should
be able to say final.push_back (L"w"); not final.push_back "(L"w");
final.push_back (0); or whatever it be

thanks ;)

Matt
MultiByteToWide Char does not return a nul terminated C-style string, just
the converted characters. If you want a C-style string, you need to do the
bookkeeping yourself:

int buffSize = MultiByteToWide Char(
CP_ACP,
0,
materials[i].pTextureFilena me, // no need to copy the src string
-1, // -1 means src string is nul terminated
NULL, // not used when calc'ing size
0);
wchar_t gah = new wchar_t[buffSize + 1]; // translated chars plus trailing
nul
MultiByteToWide Char(
CP_ACP,
0,
materials[i].pTextureFilena me,
-1,
gah,
buffSize);
gah[buffSize] = 0; // make it a C-string

Suppose you did something like this:

char fred[]="fred";
char* newfred = new char[strlen(fred)];
strcpy(newfred, fred);

You'd blow up because you neglected to include the extra nul that strcpy
puts on the end of the string. With MultiByteToWide Char, you need to do the
strcpy-like bookkeeping yourself.
Nov 28 '06 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
2621
by: Jinhee Myoung | last post by:
I wish to call a COM Object which has a parameter of type LPWSTR ** on a method call from C# The COM Method :- STDMETHODIMP CClassType::GetDataInfo(LPWSTR ** ppData) { ...... return S_OK;
5
7072
by: Jason Bell | last post by:
I'm an experienced programmer, but a newbie to c#, so please be kind :) In a c++, unmanaged, DLL of my own making I have a function called void XGL_Core_SetCaption(XGL_Core * core, char * caption); Using DLLImport I've gotten all functions working except for the ones requiring char * . Anyone care to inform me how to pass the contents of a c# string to this c++ function? I've tried passing string and char, with no luck.
20
3213
by: Peteroid | last post by:
How the heck does one convert a String* to char? Specifically: String* my_string = "HELLO" ; char m_char_array ; m_char_array = my_string ; // error strcpy( m_char_array, my_string ) ; // error strcpy( m_char_array, my_string.ToCharArray( ) ) ; // error strcpy( m_char_array, my_string.ToCharArray(0,32 ) ) ; // error strcpy( m_char_array, my_string.ToCharArray(0,0) ) ; // error
2
13318
by: suddenexpire | last post by:
For the life of me, I can't figure out what is wrong with this code and why it is giving me an error. DWORD size=256; char CurrentComp; GetComputerName(CurrentComp,&size); The third line gives an error: C2664: 'GetComputerNameW' : cannot convert parameter 1 from 'char ' to 'LPWSTR'
2
9332
by: dehi | last post by:
Hello, My problem ist that I don't know how to convert a variable from LPWSTR to LPCWSTR. Anyone an idea? Regards, Denis
11
2718
by: Angus | last post by:
I am working with a C API which often requires a char* or char buffer. If a C function returns a char* I can't use string? Or can I? I realise I can pass a char* using c_str() but what about receiving a char buffer into a string? Reason I ask is otherwise I have to guess at what size buffer to create? And then copy buffer to a string. Doesn't seem ideal.
1
14978
codexparse
by: codexparse | last post by:
I am writing a browser program in C++ Builder 6 that loads a web page using the following code: void __fastcall TForm1::ToolButton1Click(TObject *Sender) { wchar_t buff; MultiByteToWideChar(CP_ACP, MB_PRECOMPOSED, "http://www.thescripts.com", -1, buff, sizeof(buff)); CppWebBrowser1->Navigate(buff, 0, 0, 0, 0); } To include the address bar implementation, I need a temporary string derived from an edit box. This is...
3
6905
by: Samant.Trupti | last post by:
HI, I want to dynamically allocate array variable of type LPWSTR. Code looks like this... main() { LPWSTR *wstr; int count = Foo (wstr); for (int i = 0; i < count; i++) //print each element;
28
7171
by: Trups | last post by:
HI, I want to dynamically allocate array variable of type LPWSTR. Code looks like this... main() { LPWSTR *wstr; int count = Foo (wstr); for (int i = 0; i < count; i++)
0
9718
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 usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9596
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 synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10614
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. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10363
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
10109
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7649
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5544
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 the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
4327
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3847
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.