|
It's much better now
Note that you are allocating two buffers. You are returning one of them as the result. The other should be deleted before returning, to avoid leaking memory. Add the following line before the return:
delete[] input;
You should note that the callers of this function are responsible for deleting the result...
Besides that, it seems to be already doing what it's supposed to do. Now, if you want to do it really right, keep reading...
First I reccomend you read the following article by Joel Spolsky: The Absolute Minimum Every Software Developer Absolutely, Positively Must Know About Unicode and Character Sets (No Excuses!)
[^]
Now that you have read it (if not, go and read it, I'll wait right here), you surely understand why a name like UnicodeCharToBigEndian[...] doesn't make much sense for what this function does (i.e.: it's not receiving an Unicode string, but a multibyte string.)
A better name would be MultiByteToBigEndianWideChar...
One additional problem with your function is that it returns a buffer that must be released by the caller. This is a problematic approach because as a user of the function, I couldn't deduce that from the function signature alone.
If all I see is a function declared in a header file as
WCHAR* MultiByteToBigEndianWideChar(char* message);
I have no way to know whether I should delete the returned buffer or not, or in case I'm expected to delete it, whether I should use delete[], free, or anything else. I'd have to rely on some documentation or on having access to the implementation itself in order to find out.
To avoid this ambiguity, it's a common practice in this kind of functions to make the caller supply the buffer and its size as parameters to the function. When the buffer is supplied by the caller, the caller already knows if that buffer is allocated in the stack or in the heap, and how to release it in the latter case. That's exactly the approach followed by MultiByteToWideChar and all the API functions.
In fact, you might start with a function with exactly the same parameters as MultiByteToWideChar
int MultiByteToBigEndianWideChar(
UINT CodePage,
DWORD dwFlags,
LPCSTR lpMultiByteStr,
int cbMultiByte,
LPWSTR lpWideCharStr,
int cchWideChar
)
{
int nResult = MultiByteToWideChar(
CodePage,
dwFlags,
lpMultiByteStr,
cbMultiByte,
cchWideChar? lpWideCharStr + 1 : 0,
cchWideChar? cchWideChar-1 : 0
);
if (cchWideChar != 0)
{
lpWideCharStr[0] = 0xFEFF;
WideCharToBigEndian(lpWideCharStr, nResult+1);
}
return nResult + 1;
}
When cchWideChar is 0, it calls MultiByteToWideChar with 0, and returns its result plus 1, because this function will need to add a byte order marker (BOM) at the start of the buffer).
When cchWideChar is not 0, it calls MultiByteToWideChar to make the conversion to Unicode, leaving the first position available for the BOM, then puts the BOM at the first position, and calls WideCharToBigEndian, which simply does the byte swapping in the same buffer it receives.
void WideCharToBigEndian(LPWSTR lp, int cchWideChar)
{
for(int i = 0; i < cchWideChar; i++)
lp[i] = ((lp[i] & 0xFF) << 8) | ((lp[i] & 0xFF00) >> 8);
}
Now your function can be used exactly like MultiByteToWideChar, including the option to query a buffer size, and with the same flexibility (i.e.: all the options in code page, flags, etc.) You can refer anyone to the documentation for MultiByteToWideChar and simply state that you add a BOM and convert the unicode string to big-endian.
Now suppose you want to provide a simplified version in which some of the options are filled with default values.
int MultiByteToBigEndianWideChar(
LPCSTR lpMultiByteStr,
LPWSTR lpWideCharStr,
int cchWideChar
)
{
return MultiByteToBigEndianWideChar(
CP_ACP,
MB_COMPOSITE,
lpMultiByteStr,
-1,
lpWideCharStr,
cchWideChar
);
}
There, now you only need to compile and test. Good luck!
--
jlr
http://jlamas.blogspot.com/[^]
|
|
|
|
|
I got the answer!!!
The reason is that when I change the ASCII character to big-endian, take 'B', with unicode \x42\x00, it become \x00\x42.
And I used strlen to check the string length of (char* UnicodeCharToBigEndianConverter(char* message)), which finds the terminating character \x00, and gives me the wrong string length!!
My code in the above post should be ok.
Anyone interested in have a look in it and test it.
|
|
|
|
|
|
Hi,
Is there any application which tells the component class of the Java application which is similar to WinSPY ?
If anybody know please let me know.
thanks,
Mahender
|
|
|
|
|
hi,
i am trying to create a ftp application in which i want to show the progress bar. i tried using the OpenFile of CFtpConnection to open the file and then read it. but the problem i am facing is that whenever i try to get the file size by using the SeekToEnd function which returns the file size i get an exception. and i think that the file is not being opened properly. can anyone help me. i have used IIS for the ftp adn created a virtual directory. the name of the computer in IIS is : homepc. so how should i give the file path( what should be the path of the file : suppose the file to be copied is test.txt what should be the exact path). also can anyone provide any example for the same. is there any other way to use a progress bar in this application
thanks
aditya
|
|
|
|
|
morning,
my debug will let error like "access violation" by pass when I keep going by breakpoints, it says "first exception handles....", but I think the access violation is the window error, it will crash in release version, not sure why my debug is not picking it up as "unhandled exception"
thank you!
|
|
|
|
|
and what is the exception thrown ???
and what is the code that make the crash ?
TOXCCT >>> GEII power [toxcct][VisualCalc]
|
|
|
|
|
the exception will be like :
"First-chance exception in Epred.exe: 0xC0000005: Access Violation."
crash because the code couldn't recorgnize and return data type
thanks
|
|
|
|
|
can we have a piece of the crashing code to understand what you're doing wrong ?
TOXCCT >>> GEII power [toxcct][VisualCalc]
|
|
|
|
|
valerie99 wrote:
"First-chance exception in Epred.exe: 0xC0000005: Access Violation."
This mean you application try to access the memory which is actually not available.
try to debug and check is every Varible getting required memory ?
"Opinions are neither right nor wrong. I cannot change your opinion. I can, however, change what influences your opinion." - David Crow
cheers,
Alok Gupta
VC Forum Q&A :- I/ IV
|
|
|
|
|
I am looking for a free proxy server to redirect UDP and TCP Traffic for a brief test.
A free UDP/TCP proxy server or source code for same would be cool as well. C++, VB (Gah!), C#, VB.Net, anything like that would work.
Doesn't have to be the greatest thing in the world, just something that can handle a few tests. The OS platforms would be a windows server, NT, 2000, 2003 server, any of those.
The reason I need it on windows is because the VP client to perform the tests is windows only.
Thanks!
.............................
There's nothing like the sound of incoming rifle and mortar rounds to cure the blues. No matter how down you are, you take an active and immediate interest in life.
Fiat justitia, et ruat cælum
|
|
|
|
|
hiho@ll
i just wanted to know if it's possible to "hook" the windows shutdown and if yes how?
i thought about a simple alarm prog, which does something if you want to shutdown windows
for example it checks if you did some task before shutdown
or it simply reminds you to do something before shuting down
anybody has an idea?
thx@ll
|
|
|
|
|
ThinkingPrometheus wrote:
i thought about a simple alarm prog, which does something if you want to shutdown windows
HANDLE WM_QUERYENDSESSION message !
"Opinions are neither right nor wrong. I cannot change your opinion. I can, however, change what influences your opinion." - David Crow
cheers,
Alok Gupta
VC Forum Q&A :- I/ IV
|
|
|
|
|
maybe i'm wrong but if i handle such an event, windows will try to kill the application if it doesn't exit!?
so is it possible to abort the shutdown if i use WM_QUERYENDSESSION?
|
|
|
|
|
ThinkingPrometheus wrote:
so is it possible to abort the shutdown if i use WM_QUERYENDSESSION?
Yeap it possible, you just return the FALSE o this event and then you have to shutdown by your self!
"Opinions are neither right nor wrong. I cannot change your opinion. I can, however, change what influences your opinion." - David Crow
cheers,
Alok Gupta
VC Forum Q&A :- I/ IV
|
|
|
|
|
hello to all members i have been given task to add functionality in project
on right clicking icon on taskbar it shows menu like windows messenger shows
Exit,open,signout,sign in,sign in as,my status,send an email message and my e-mail Inbox
my project icon is also showing its menu like that
but on double clicking windows messenger icon in taskbar messenger gets opened
how will i add this double clicking functionality to my project
Ankit Aneja
|
|
|
|
|
Ankit Aneja wrote:
windows icon in taskbar it gets opened
?
V
|
|
|
|
|
i mean when i double click windows messenger icon on task bar it gets opened
same functionality i want here
Ankit Aneja
"Nothing is impossible. The word itself says - I M possible"
|
|
|
|
|
Ankit Aneja wrote:
on right clicking icon on taskbar
I Believe you are talking about System Tray!
Ankit Aneja wrote:
but on double clicking windows icon in taskbar it gets opened
Handle the Function associated With the System Tray Message! , you Will get what you want!
"Opinions are neither right nor wrong. I cannot change your opinion. I can, however, change what influences your opinion." - David Crow
cheers,
Alok Gupta
VC Forum Q&A :- I/ IV
|
|
|
|
|
thanks
Ankit Aneja
"Nothing is impossible. The word itself says - I M possible"
|
|
|
|
|
|
Hi, Jungs, I´m trying to execute an exe-file from my own programm. I tried to use th _exec-family functions, but it didn't work. The executable is a console programm, so i need to be able to pass some parameters, too.
Thanks.
Elale
|
|
|
|
|
Use CreateProcess[^]
lpCommandLine permits passing parameters.
Marc Soleda.
... she said you are the perfect stranger she said baby let's keep it like this... Tunnel of Love, Dire Straits.
|
|
|
|
|
|
use WinExec()
/\|-||\/|/\|)
|
|
|
|
|