Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

convert problem for CreateFile(LPCTSTR lpFileName,

34 views
Skip to first unread message

mfuser

unread,
Oct 19, 2009, 8:43:17 AM10/19/09
to
i use a function like (the function definition can not be changed) :

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

Ulrich Eckhardt

unread,
Oct 19, 2009, 9:40:23 AM10/19/09
to
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

mfuser

unread,
Oct 20, 2009, 3:21:01 AM10/20/09
to
found solution:

#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
>

> .
>

0 new messages