473,386 Members | 1,867 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,386 software developers and data experts.

MultiByteToWideChar and GCC?

I hope this is the appropriate forum for this question. I apologize
if it is not.

Consider the following code snippet:

DWORD length;
PCHAR buf = new CHAR[64];
strcpy( buf, "HelloWorld" );
printf( "*(&length - 1) = 0x%x\n", *(&length - 1) );
length = MultiByteToWideChar( CP_ACP, 0, buf, -1, NULL, 0 );
printf( "*(&length - 1) = 0x%x\n", *(&length - 1) );

When compiled under GCC 3.4.2 (mingw) with the optimizer on, output is
produced such as:

*(&length - 1) = 0x22fc40
*(&length - 1) = 0x0

In other words, MultiByteToWideChar is somehow overwriting another
local variable on the stack. Specifically it is the variable at the
memory address four bytes previous to the "length" DWORD. (in my
particular case, it happens to be the 'this' pointer for the class I'm
implementing, which causes the entire class to implode.) It turns out
that whatever value I pass as the sixth argument to
MultiByteToWideChar (the WCHAR character count of theoutput buffer)
gets written to the DWORD at address (&length - 1).

When compiled under GCC 3.4.2 with the optimizer off, the entire
MultiByteToWideChar call fails with last error set to "invalid
parameter".

Now consider the following code, which is exactly the same as above
except the character buffer is allocated on the stack:

DWORD length;
CHAR buf[64];
strcpy( buf, "HelloWorld" );
printf( "*(&length - 1) = 0x%x\n", *(&length - 1) );
length = MultiByteToWideChar( CP_ACP, 0, buf, -1, NULL, 0 );
printf( "*(&length - 1) = 0x%x\n", *(&length - 1) );

In this case GCC 3.4.2 has no problem with the code, either optimized
or not.

Why should the behavior be different just based upon whether the
buffer is allocated on the heap or the stack?

I've disassembled the binary in each case and can't see anything wrong
with what GCC is doing. It seems to me like the bug would have to be
within MultiByteToWideChar itself (unless I am doing something obvious
and stupid and just can't see it).

The frustrating thing is that I cannot make this happen if I recompile
the above code from scratch in a new program. However, in the class I
was originally writing, where this first arose, I can make it happen
every time.

I must be doing something wrong, but I can't for the life of me see
what. Any ideas would be greatly appreciated. Thank you very much!
Jul 31 '08 #1
5 6783
Banfa
9,065 Expert Mod 8TB
The frustrating thing is that I cannot make this happen if I recompile
the above code from scratch in a new program. However, in the class I
was originally writing, where this first arose, I can make it happen
every time.
I have to say that to me this suggests that although the symptom occurs in this section of code the actual error is elsewhere.
Jul 31 '08 #2
Thank you for the reply.

Yes, I might expect so too, but if I dump the entire stack frame at each instruction, I find the problem occurs exactly at the call to MultiByteToWideChar. And then if I write the exact code snippet I posted, which makes no reference to any previous code at all, the problem occurs. The stack is intact before the call and corrupted after it. I might expect you were right, if the call passed even a single local variable not defined in this code snippet. But it doesn't.
Jul 31 '08 #3
weaknessforcats
9,208 Expert Mod 8TB
Do not expect calls like this to work as log as you have hard-coded types like char in your programs.

Use the TCHAR mappings.

Then things should work regardlesss of wheher you are using Unicode or not. I expect in this case you are compiling with Unicode ON (the default) and using char data types which are too small.
Aug 1 '08 #4
I am reading a file encoded in UTF-8, and I need to convert it to UTF-16. This is the standard API for that purpose, and it has nothing to do with TCHAR. Either the API works or it doesn't --- but it has to be used in this case.
Aug 1 '08 #5
Anyone, any ideas? Thanks!
Aug 3 '08 #6

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

Similar topics

7
by: Robert Diamond | last post by:
Hi ppl, just a quick question... I need to use "MultiByteToWideChar(stuff)" to convert a char to unicode, so that OleLoadPicturePath can get the image files i want, and load it into a HBITMAP,...
7
by: Karthik | last post by:
Hello I am trying to convert the following??? The Code std::string* ChkName;
2
by: caviar | last post by:
I'm trying to read in a ref parameter from a native dll, its working in vb if i use the kernel32 functions below transforming the ref param to a vb string: Now, i want to skip this vb dll and...
10
by: Bonj | last post by:
Hello. I hope somebody can help me on this, because I'm running out of options to turn to. I have almost solved my regular expression function. Basically it works OK if unicode is defined. It...
5
by: B Vidyadhar Joshi | last post by:
I have a function which takes a NOT NULL terminated PWCHAR. int myFunction (PWCHAR pszKey, ULONG ulKeyLen); I have to pass a 4 charecter value to the "key" parameter. For example, "1111". How...
1
by: Egbert Nierop \(MVP for IIS\) | last post by:
Hi, I'm 'improving' CComBSTR (yes, I do still program unmanaged code in addition to C# ) to contain features, not found in it. Does anybody have good code which maches LastIndexOf()? If...
2
by: groups | last post by:
I have a C# application which needs to convert MultiByte strings to Unicode. However, I cannot get MultiByteToWideChar to behave as expected within ..net. I have declared it as follows: ...
4
by: sweety | last post by:
Dear all, Kindy help to convert the char* ( LPCSTR) to WCHAR*(LPCWSTR). Would be great if you tell if any function does this job in VC++. Quick response will be greatfull...as its blocked... ...
6
by: MattWilson.6185 | last post by:
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 ...
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: 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: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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...
0
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,...
0
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...
0
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...

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.