void Test(const char *p) {
CreateFile(_T("COM1:"), ...
}
the function Test gets called from a .net wrapper in c# with argument
p=Encoding.ASCII.GetBytes("COM1:")
if i use the above hardcoedeapproach with _T("COM1:") for lpFileName
everthing works fine
but which conversion i must perform, if i would pass 'COM1:' through
argument 'p' (UNICODE enabled) without modifying the function definition ?
tried with several functions (like MultiByteToWideChar) but without success
CreateFile under CE is always CreateFileW which takes a WCHAR string.
> but which conversion i must perform, if i would pass 'COM1:' through
> argument 'p' (UNICODE enabled) without modifying the function definition ?
You mean the function signature/prototype. The definition includes the
source inside the function, i.e. its implementation.
That said, the code you showed seemed to assume ASCII encoding, not one that
supports the full Unicode range.
> tried with several functions (like MultiByteToWideChar) but without
> success
You rather need the opposite of that of MBTWC. However, since you already
know that the passed content is ASCII, the following should work:
size_t const len = strlen(p);
std::wstring w(len);
for(size_t i=0; i!=len; ++i) {
unsigned char c = p[i];
// make sure it's an ASCII char
if(c>=127)
throw std::runtime_error("invalid ASCII character");
w[i] = c;
}
...CreateFileW(w.c_str(),...)...
Uli
--
Sator Laser GmbH
Geschäftsführer: Thorsten Föcking, Amtsgericht Hamburg HR B62 932
#include <atlbase.h>
#include <atlconv.h>
USES_CONVERSION
CreateFile(A2W(fileName),...
"Ulrich Eckhardt" wrote:
> mfuser wrote:
> > i use a function like (the function definition can not be changed) :
> >
> > void Test(const char *p) {
> > CreateFile(_T("COM1:"), ...
> > }
>
> CreateFile under CE is always CreateFileW which takes a WCHAR string.
>
> > but which conversion i must perform, if i would pass 'COM1:' through
> > argument 'p' (UNICODE enabled) without modifying the function definition ?
>
> You mean the function signature/prototype. The definition includes the
> source inside the function, i.e. its implementation.
>
> That said, the code you showed seemed to assume ASCII encoding, not one that
> supports the full Unicode range.
>
> > tried with several functions (like MultiByteToWideChar) but without
> > success
>
> You rather need the opposite of that of MBTWC. However, since you already
> know that the passed content is ASCII, the following should work:
>
> size_t const len = strlen(p);
> std::wstring w(len);
> for(size_t i=0; i!=len; ++i) {
> unsigned char c = p[i];
> // make sure it's an ASCII char
> if(c>=127)
> throw std::runtime_error("invalid ASCII character");
> w[i] = c;
> }
> ....CreateFileW(w.c_str(),...)...
>
>
> Uli
>
> --
> Sator Laser GmbH
> Geschäftsführer: Thorsten Föcking, Amtsgericht Hamburg HR B62 932
>
> .
>