Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. converting from const char [] to 'LPWSTR'

converting from const char [] to 'LPWSTR'

Scheduled Pinned Locked Moved C / C++ / MFC
c++helpquestion
3 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Offline
    M Offline
    maizhiming
    wrote on last edited by
    #1

    When I compile this code in MSVC++ 2005 express I get the following error. SERVICE_TABLE_ENTRY DispatchTable[]= {{"Service1",serviceMain},{NULL,NULL}}; error C2440: 'initializing' : cannot convert from 'const char [9]' to 'LPWSTR' Pretty much where ever I am using a string I get the same error. Why doesn't the code work?

    M X 2 Replies Last reply
    0
    • M maizhiming

      When I compile this code in MSVC++ 2005 express I get the following error. SERVICE_TABLE_ENTRY DispatchTable[]= {{"Service1",serviceMain},{NULL,NULL}}; error C2440: 'initializing' : cannot convert from 'const char [9]' to 'LPWSTR' Pretty much where ever I am using a string I get the same error. Why doesn't the code work?

      M Offline
      M Offline
      Matthew Faithfull
      wrote on last edited by
      #2

      I would have to see the definition of SERVICE_TABLE_ENTRY to be sure but I suspect it's defined to contain a wide character (UNICODE) string whereas "Service1" is a constant narrow character or (ASCII) string. UNICODE strings, usually, have 16bits per character while ASCII characters have only 8bits each so conversion between the two is non trivial. There's a whole world of pain involved here, code pages, locales, W2A macros, multi-byte character sets, UTF-8 and then some but what you need to do to make the above code work is make use of the _T() macro Microsoft so kindly supply. SERVICE_TABLE_ENTRY DispatchTable[]= {{ **_T(** "Service1" **)** , serviceMain }, {NULL, NULL}}; Enjoy.

      Nothing is exactly what it seems but everything with seems can be unpicked.

      1 Reply Last reply
      0
      • M maizhiming

        When I compile this code in MSVC++ 2005 express I get the following error. SERVICE_TABLE_ENTRY DispatchTable[]= {{"Service1",serviceMain},{NULL,NULL}}; error C2440: 'initializing' : cannot convert from 'const char [9]' to 'LPWSTR' Pretty much where ever I am using a string I get the same error. Why doesn't the code work?

        X Offline
        X Offline
        Xing Chen
        wrote on last edited by
        #3

        Add L before "", it declare it as wide char. SERVICE_TABLE_ENTRY DispatchTable[]= {{L"Service1",serviceMain},{NULL,NULL}}; Best Regards.

        1 Reply Last reply
        0
        Reply
        • Reply as topic
        Log in to reply
        • Oldest to Newest
        • Newest to Oldest
        • Most Votes


        • Login

        • Don't have an account? Register

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • World
        • Users
        • Groups