a type problem (TCHAR, LPCWSTR)

This is a discussion on a type problem (TCHAR, LPCWSTR) within the Windows Programming forums, part of the Platform Specific Boards category; Hi, I got a small situation that I just can't figure out Code: hFile = CreateFile ((LPCWSTR) bla , // ...

  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
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,484
    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")
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  3. #3
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    22,264
    Moved to Windows programming forum.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    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
    Posts
    23,001
    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,189
    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,...
    Until you can build a working general purpose reprogrammable computer out of basic components from radio shack, you are not fit to call yourself a programmer in my presence. This is cwhizard, signing off.

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, 03: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, 02:03 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21