I searched the internet for nearly an hour and did not find the appropriate transfer type code. I wrote it in 10 minutes, and I was a little unconfident. Lazy !! Let's post it and share it with you to save your time.
Extern "C"
{
Void _ declspec (dllexport) W2C (wchar * pwstr, char * CSTR)
{
Char * pwssrc = (char *) pwstr;
Ulong length = 0;
While (1)
{
If (* pwssrc! = 0x00 & * (pwssrc + 1) = 0x00)
{
CSTR [length ++] = * pwssrc;
Pwssrc + = 2;
} Else
{
CSTR [length] = 0x00;
Break;
}
}
}
Void _ declspec (dllexport) c2w (char * CSTR, wchar * wstr)
{
Char * pcstr = CSTR;
Wchar * pwcstr = wstr;
Ulong I = 0;
Ulong J = 0;
While (1)
{
If (pcstr [I]! = 0x00)
{
(Char *) pwcstr) [J] = pcstr [I];
(Char *) pwcstr) [++ J] = 0x00;
I ++;
J ++;
} Else
{
(Char *) pwcstr) [J] = 0x00;
(Char *) pwcstr) [++ J] = 0x00;
Break;
}
}
}
}
The preceding method has limitations when used recently. The strings cannot contain Chinese characters.
The improved version is as follows:
Namespace charobj
{
//--------------------------------
// Convert multiple character sets to Unicode wide character sets
//--------------------------------
Void mutiltowide (char szmutilsrcdata [], DWORD dwmutilsrcdatabufsize,
Wchar lpwidecharstr [], DWORD dwwidecharbufsize)
{
Multibytetowidechar (cp_acp, 0,
Szmutilsrcdata,
Dwmutilsrcdatabufsize,
Lpwidecharstr,
Dwwidecharbufsize );
}
//--------------------------------
// Convert Unicode wide character set to multi-Character Set
//--------------------------------
Void widetomutil (wchar lpwidecharstr [], DWORD dwwidecharbufsize,
Char szmutilsrcdata [], DWORD dwmutilsrcdatabufsize)
{
Widechartomultibyte (cp_acp, // code page
0, // performance and mapping flags
Lpwidecharstr, // wide-character string
Dwwidecharbufsize, // Number of chars in string
Szmutilsrcdata, // buffer for New String
Dwmutilsrcdatabufsize,
Null,
Null );
}
}
C Runtime Library Standard Functions
Mbstowcs
Wcstombs