dast.blog();

"Find a job you love, and you will never have to work a day in your life."

  Home  |   Contact  |   Syndication    |   Login
  19 Posts | 0 Stories | 0 Comments | 0 Trackbacks

News



Google dast.blog();





These postings are provided "AS IS" with no warranties, and confer no rights.

Locations of visitors to this page



Archives

Post Categories

Image Galleries

Artikel

CodeProject Artikel

Homepage

Links

PocketPCDN Artikel

Friday, November 24, 2006 #

I have written a little Win32 console application in C++ that will show how to do it, to run it you have to install the PocketConsole from SymbolicTools:

The following image shows the output of the application running on our HP iPAQ hx2490b:


For a simple string conversion between ANSI and Unicode on a Pocket PC I have written some C++ classes:

  • CTtoA for conversion from LPCTSTR to LPCSTR
  • CTtoW for conversion from LPCTSTR to LPCWSTR
  • CAtoT for conversion from LPCSTR to LPCTSTR
  • CWtoT for conversion from LPCWSTR to LPCTSTR
  • CAtoW for conversion from LPCSTR to LPCWSTR
  • CWtoA for conversion from LPCWSTR to LPCSTR

strconv.h

    1 //////////////////////////////////////////////////////////////////////////

    2 //

    3 //  strconv.h - Some useful classes for string conversion.

    4 //

    5 //  Copyright (c) 2004-2007 by Daniel Strigl.

    6 //

    7 //  This code may be used in compiled form in any way you desire.

    8 //  This file may be redistributed unmodified by any means PROVIDING

    9 //  it is not sold for profit without the authors written consent,

   10 //  and providing that this notice and the authors name is included.

   11 //  By the way, if the source code in this file is used in any

   12 //  application I would appreciate and enjoy hearing about them.

   13 //

   14 //  This file is provided "as is" with no expressed or implied warranty.

   15 //  The author accepts no liability for any damage, in any form, caused

   16 //  by this code. Use it at your own risk and as with all code expect

   17 //  bugs! It's been tested, but I'm not perfect ;-)

   18 //

   19 //  Please use the contact page of my blog to report any

   20 //  bug, comment or suggestion.

   21 //

   22 //  Contact:

   23 //  --------

   24 //

   25 //    Daniel Strigl [http://geekswithblogs.net/dastblog/]

   26 //

   27 //  History:

   28 //  --------

   29 //

   30 //    21 Jul 2004 - Initial version.

   31 //    20 Nov 2006 - Added class CAtoT, CWtoT, CAtoW and CWtoA.

   32 //

   33 //////////////////////////////////////////////////////////////////////////

   34 

   35 #if !defined(_STRCONV_H_)

   36 #define _STRCONV_H_

   37 

   38 //////////////////////////////////////////////////////////////////////////

   39 //

   40 //  CTtoA

   41 //

   42 //      LPCTSTR to LPCSTR conversion.

   43 //

   44 class CTtoA

   45 {

   46     LPSTR m_psz;

   47 

   48 public:

   49     CTtoA(LPCTSTR ptsz) : m_psz(NULL)

   50     {

   51         ASSERT(ptsz != NULL);

   52 #if defined(_UNICODE) // for UNICODE build

   53         const int size = WideCharToMultiByte(CP_ACP, 0, ptsz, -1,

   54             NULL, 0, NULL, NULL);

   55 #ifdef _DEBUG

   56         if (size == 0) {

   57             ASSERT(!_T("Function \"WideCharToMultiByte\" failed."));

   58         }

   59 #endif // _DEBUG

   60         ASSERT(size > 0);

   61         m_psz = (LPSTR) malloc(sizeof(CHAR)*size);

   62         ASSERT(m_psz != NULL);

   63 #ifdef _DEBUG

   64         memset(m_psz, 0, sizeof(CHAR)*size);

   65 #endif // _DEBUG

   66         VERIFY(WideCharToMultiByte(CP_ACP, 0, ptsz, -1,

   67             m_psz, size, NULL, NULL) != 0);

   68 #else // for non-UNICODE build

   69         m_psz = _strdup(ptsz);

   70         ASSERT(m_psz != NULL);

   71 #endif // defined(_UNICODE)

   72     }

   73 

   74     CTtoA(const CTtoA& rhs) : m_psz(_strdup(rhs.m_psz))

   75     {

   76         ASSERT(m_psz != NULL);

   77     }

   78 

   79     CTtoA& operator=(const CTtoA& rhs)

   80     {

   81         if (this != &rhs)

   82         {

   83             free(m_psz);

   84             m_psz = _strdup(rhs.m_psz);

   85             ASSERT(m_psz != NULL);

   86         }

   87         return *this;

   88     }

   89 

   90     ~CTtoA()

   91     {

   92         free(m_psz);

   93     }

   94 

   95     size_t GetLength() const { return strlen(m_psz); }

   96 

   97     operator LPCSTR() const { return m_psz; }

   98 };

   99 

  100 //////////////////////////////////////////////////////////////////////////

  101 //

  102 //  CTtoW

  103 //

  104 //      LPCTSTR to LPCWSTR conversion.

  105 //

  106 class CTtoW

  107 {

  108     LPWSTR m_pwsz;

  109 

  110 public:

  111     CTtoW(LPCTSTR ptsz) : m_pwsz(NULL)

  112     {

  113         ASSERT(ptsz != NULL);

  114 #if defined(_UNICODE) // for UNICODE build

  115         m_pwsz = _wcsdup(ptsz);

  116         ASSERT(m_pwsz != NULL);

  117 #else // for non-UNICODE build

  118         const int size = MultiByteToWideChar(CP_ACP, 0, ptsz, -1, NULL, 0);

  119 #ifdef _DEBUG

  120         if (size == 0) {

  121             ASSERT(!_T("Function \"MultiByteToWideChar\" failed."));

  122         }

  123 #endif // _DEBUG

  124         ASSERT(size > 0);

  125         m_pwsz = (LPWSTR) malloc(sizeof(WCHAR)*size);

  126         ASSERT(m_pwsz != NULL);

  127 #ifdef _DEBUG

  128         memset(m_pwsz, 0, sizeof(WCHAR)*size);

  129 #endif // _DEBUG

  130         VERIFY(MultiByteToWideChar(CP_ACP, 0, ptsz, -1, m_pwsz, size) != 0);

  131 #endif // defined(_UNICODE)

  132     }

  133 

  134     CTtoW(const CTtoW& rhs) : m_pwsz(_wcsdup(rhs.m_pwsz))

  135     {

  136         ASSERT(m_pwsz != NULL);

  137     }

  138 

  139     CTtoW& operator=(const CTtoW& rhs)

  140     {

  141         if (this != &rhs)

  142         {

  143             free(m_pwsz);

  144             m_pwsz = _wcsdup(rhs.m_pwsz);

  145             ASSERT(m_pwsz != NULL);

  146         }

  147         return *this;

  148     }

  149 

  150     ~CTtoW()

  151     {

  152         free(m_pwsz);

  153     }

  154 

  155     size_t GetLength() const { return wcslen(m_pwsz); }

  156 

  157     operator LPCWSTR() const { return m_pwsz; }

  158 };

  159 

  160 //////////////////////////////////////////////////////////////////////////

  161 //

  162 //  CAtoT

  163 //

  164 //      LPCSTR to LPCTSTR conversion.

  165 //

  166 class CAtoT

  167 {

  168     LPTSTR m_ptsz;

  169 

  170 public:

  171     CAtoT(LPCSTR psz) : m_ptsz(NULL)

  172     {

  173         ASSERT(psz != NULL);

  174 #if defined(_UNICODE) // for UNICODE build

  175         const int size = MultiByteToWideChar(CP_ACP, 0, psz, -1, NULL, 0);

  176 #ifdef _DEBUG

  177         if (size == 0) {

  178             ASSERT(!_T("Function \"MultiByteToWideChar\" failed."));

  179         }

  180 #endif // _DEBUG

  181         ASSERT(size > 0);

  182         m_ptsz = (LPWSTR) malloc(sizeof(WCHAR)*size);

  183         ASSERT(m_ptsz != NULL);

  184 #ifdef _DEBUG

  185         memset(m_ptsz, 0, sizeof(WCHAR)*size);

  186 #endif // _DEBUG

  187         VERIFY(MultiByteToWideChar(CP_ACP, 0, psz, -1, m_ptsz, size) != 0);

  188 #else // for non-UNICODE build

  189         m_ptsz = _strdup(psz);

  190         ASSERT(m_ptsz != NULL);

  191 #endif // defined(_UNICODE)

  192     }

  193 

  194     CAtoT(const CAtoT& rhs) : m_ptsz(_tcsdup(rhs.m_ptsz))

  195     {

  196         ASSERT(m_ptsz != NULL);

  197     }

  198 

  199     CAtoT& operator=(const CAtoT& rhs)

  200     {

  201         if (this != &rhs)

  202         {

  203             free(m_ptsz);

  204             m_ptsz = _tcsdup(rhs.m_ptsz);

  205             ASSERT(m_ptsz != NULL);

  206         }

  207         return *this;

  208     }

  209 

  210     ~CAtoT()

  211     {

  212         free(m_ptsz);

  213     }

  214 

  215     size_t GetLength() const { return _tcslen(m_ptsz); }

  216 

  217     operator LPCTSTR() const { return m_ptsz; }

  218 };

  219 

  220 //////////////////////////////////////////////////////////////////////////

  221 //

  222 //  CWtoT

  223 //

  224 //      LPCWSTR to LPCTSTR conversion.

  225 //

  226 class CWtoT

  227 {

  228     LPTSTR m_ptsz;

  229 

  230 public:

  231     CWtoT(LPCWSTR pwsz) : m_ptsz(NULL)

  232     {

  233         ASSERT(pwsz != NULL);

  234 #if defined(_UNICODE) // for UNICODE build

  235         m_ptsz = _wcsdup(pwsz);

  236         ASSERT(m_ptsz != NULL);

  237 #else // for non-UNICODE build

  238         const int size = WideCharToMultiByte(CP_ACP, 0, pwsz, -1,

  239             NULL, 0, NULL, NULL);

  240 #ifdef _DEBUG

  241         if (size == 0) {

  242             ASSERT(!_T("Function \"WideCharToMultiByte\" failed."));

  243         }

  244 #endif // _DEBUG

  245         ASSERT(size > 0);

  246         m_ptsz = (LPSTR) malloc(sizeof(CHAR)*size);

  247         ASSERT(m_ptsz != NULL);

  248 #ifdef _DEBUG

  249         memset(m_ptsz, 0, sizeof(CHAR)*size);

  250 #endif // _DEBUG

  251         VERIFY(WideCharToMultiByte(CP_ACP, 0, pwsz, -1,

  252             m_ptsz, size, NULL, NULL) != 0);

  253 #endif // defined(_UNICODE)

  254     }

  255 

  256     CWtoT(const CWtoT& rhs) : m_ptsz(_tcsdup(rhs.m_ptsz))

  257     {

  258         ASSERT(m_ptsz != NULL);

  259     }

  260 

  261     CWtoT& operator=(const CWtoT& rhs)

  262     {

  263         if (this != &rhs)

  264         {

  265             free(m_ptsz);

  266             m_ptsz = _tcsdup(rhs.m_ptsz);

  267             ASSERT(m_ptsz != NULL);

  268         }

  269         return *this;

  270     }

  271 

  272     ~CWtoT()

  273     {

  274         free(m_ptsz);

  275     }

  276 

  277     size_t GetLength() const { return _tcslen(m_ptsz); }

  278 

  279     operator LPCTSTR() const { return m_ptsz; }

  280 };

  281 

  282 //////////////////////////////////////////////////////////////////////////

  283 //

  284 //  CAtoW

  285 //

  286 //      LPCSTR to LPCWSTR conversion.

  287 //

  288 class CAtoW

  289 {

  290     LPWSTR m_pwsz;

  291 

  292 public:

  293     CAtoW(LPCSTR psz) : m_pwsz(NULL)

  294     {

  295         ASSERT(psz != NULL);

  296         const int size = MultiByteToWideChar(CP_ACP, 0, psz, -1, NULL, 0);

  297 #ifdef _DEBUG

  298         if (size == 0) {

  299             ASSERT(!_T("Function \"MultiByteToWideChar\" failed."));

  300         }

  301 #endif // _DEBUG

  302         ASSERT(size > 0);

  303         m_pwsz = (LPWSTR) malloc(sizeof(WCHAR)*size);

  304         ASSERT(m_pwsz != NULL);

  305 #ifdef _DEBUG

  306         memset(m_pwsz, 0, sizeof(WCHAR)*size);

  307 #endif // _DEBUG

  308         VERIFY(MultiByteToWideChar(CP_ACP, 0, psz, -1, m_pwsz, size) != 0);

  309     }

  310 

  311     CAtoW(const CAtoW& rhs) : m_pwsz(_wcsdup(rhs.m_pwsz))

  312     {

  313         ASSERT(m_pwsz != NULL);

  314     }

  315 

  316     CAtoW& operator=(const CAtoW& rhs)

  317     {

  318         if (this != &rhs)

  319         {

  320             free(m_pwsz);

  321             m_pwsz = _wcsdup(rhs.m_pwsz);

  322             ASSERT(m_pwsz != NULL);

  323         }

  324         return *this;

  325     }

  326 

  327     ~CAtoW()

  328     {

  329         free(m_pwsz);

  330     }

  331 

  332     size_t GetLength() const { return wcslen(m_pwsz); }

  333 

  334     operator LPCWSTR() const { return m_pwsz; }

  335 };

  336 

  337 //////////////////////////////////////////////////////////////////////////

  338 //

  339 //  CWtoA

  340 //

  341 //      LPCWSTR to LPCSTR conversion.

  342 //

  343 class CWtoA

  344 {

  345     LPSTR m_psz;

  346 

  347 public:

  348     CWtoA(LPCWSTR pwsz) : m_psz(NULL)

  349     {

  350         ASSERT(pwsz != NULL);

  351         const int size = WideCharToMultiByte(CP_ACP, 0, pwsz, -1,

  352             NULL, 0, NULL, NULL);

  353 #ifdef _DEBUG

  354         if (size == 0) {

  355             ASSERT(!_T("Function \"WideCharToMultiByte\" failed."));

  356         }

  357 #endif // _DEBUG

  358         ASSERT(size > 0);

  359         m_psz = (LPSTR) malloc(sizeof(CHAR)*size);

  360         ASSERT(m_psz != NULL);

  361 #ifdef _DEBUG

  362         memset(m_psz, 0, sizeof(CHAR)*size);

  363 #endif // _DEBUG

  364         VERIFY(WideCharToMultiByte(CP_ACP, 0, pwsz, -1,

  365             m_psz, size, NULL, NULL) != 0);

  366     }

  367 

  368     CWtoA(const CWtoA& rhs) : m_psz(_strdup(rhs.m_psz))

  369     {

  370         ASSERT(m_psz != NULL);

  371     }

  372 

  373     CWtoA& operator=(const CWtoA& rhs)

  374     {

  375         if (this != &rhs)

  376         {

  377             free(m_psz);

  378             m_psz = _strdup(rhs.m_psz);

  379             ASSERT(m_psz != NULL);

  380         }

  381         return *this;

  382     }

  383 

  384     ~CWtoA()

  385     {

  386         free(m_psz);

  387     }

  388 

  389     size_t GetLength() const { return strlen(m_psz); }

  390 

  391     operator LPCSTR() const { return m_psz; }

  392 };

  393 

  394 //////////////////////////////////////////////////////////////////////////

  395 

  396 #endif // !defined(_STRCONV_H_)

The usage of the classes are realy simple, here is an example:

    1 ...

    2 

    3 #include "strconv.h"

    4 

    5 BOOL IsWireless(LPCTSTR pszAdapter)

    6 {

    7     ...

    8 }

    9 

   10 int _tmain(int argc, _TCHAR* argv[])

   11 {

   12     char szAdapter[64];

   13 

   14     ...

   15 

   16     BOOL bIsWireless = IsWireless(CAtoT(szAdapter));

   17 

   18     ...

   19 

   20     return 0;

   21 }