Thread: a type problem (TCHAR, LPCWSTR)

  1. #1
    Registered User
    Join Date
    Mar 2008
    Location
    slovenia
    Posts
    25

    a type problem (TCHAR, LPCWSTR)

    Hi, I got a small situation that I just can't figure out

    Code:
    hFile = CreateFile ((LPCWSTR) bla ,                 // this parameter bothers me
                           GENERIC_READ,          // open for reading
                           FILE_SHARE_READ,...
    This is a win API, the name kinda says it all, and the first parameter is the name of the file and the type as you can see is LPCWSTR, now, in C everything works fine if you simply type in "d:\sdaf\gfasd.txt" or whichever file you want to access, all neat and fine, but in C++ I got in to a whole lota trouble when I did this, the compiler gives me an error

    //Error 2 error C2664: 'CreateFileW' : cannot convert parameter 1 from 'const char [7]' to 'LPCWSTR'

    And if I typecast it then I get a bug, cuz the function won't open the file, stating it does not exist.

    Then another thing I saw was that an example I was looking used a TCHAR *argv[]
    and took argv[1] into the first parameter and everything worked fine, then when I tried to decleare TCHAR *mystring, I used the sprintf_s and again it can't find the file, and belive me on this one, it is there. + the same process when I tried with the LPCWSTR.

    I know C++ is pretty tough with typecasting, but sheeshh

    Any ideas, solutions would be great, thanks.

    PS: anything unclear?? just say (cuz I'm lousy at explaining things)

  2. #2
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    It means - ou compiling your C++ code as UNICODE
    Add L before the string literals
    L"d:\sdaf\gfasd.txt"

    or use TEXT macro to be able to compile both ways
    TEXT("d:\sdaf\gfasd.txt")
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  3. #3
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Moved to Windows programming forum.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  4. #4
    Registered User
    Join Date
    Mar 2008
    Location
    slovenia
    Posts
    25
    thnx Vart, works well.

  5. #5
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    C++ is strict on typecasting, yes, because you are trying to pass incorrect type.
    In your project, you have unicode defined, yet you are using ANSI-type C-style functions.
    You can look for "TCHAR" specific functions. They'll work fine.
    Never, ever, cast something unless you know what you're doing. In this case, you don't know what you're doing, so don't cast it, okay?
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  6. #6
    Registered User
    Join Date
    Mar 2008
    Location
    slovenia
    Posts
    25
    no problemo, :P & tnx

  7. #7
    Malum in se abachler's Avatar
    Join Date
    Apr 2007
    Posts
    3,195
    Quote Originally Posted by Crazed View Post
    Code:
     
    hFile = CreateFile ((LPCWSTR) bla ,                 // this parameter bothers me
                           GENERIC_READ,          // open for reading
                           FILE_SHARE_READ,...
    try

    Code:
    hFile = CreateFile (L"Somefile.ext",                 // this parameter bothers me
                           GENERIC_READ,          // open for reading
                           FILE_SHARE_READ,...

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. A question related to strcmp
    By meili100 in forum C++ Programming
    Replies: 6
    Last Post: 07-07-2007, 02:51 PM
  2. Using VC Toolkit 2003
    By Noobwaker in forum Windows Programming
    Replies: 8
    Last Post: 03-13-2006, 07:33 AM
  3. pointer to array of objects of struct
    By undisputed007 in forum C++ Programming
    Replies: 12
    Last Post: 03-02-2004, 04:49 AM
  4. Erros in Utility Header File
    By silk.odyssey in forum C++ Programming
    Replies: 4
    Last Post: 12-22-2003, 06:17 AM
  5. qwerty/azerty keyboard type problem + question about loop.
    By Robin Hood in forum C++ Programming
    Replies: 9
    Last Post: 07-22-2002, 01:03 PM