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
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. CreateFile, error

CreateFile, error

Scheduled Pinned Locked Moved C / C++ / MFC
c++securityhelp
10 Posts 4 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.
  • G Offline
    G Offline
    gunnar66
    wrote on last edited by
    #1

    I try to create a file, the filename i will get from a string. What do I do wrong. It works ok in Visual C++, but in Emededd C++ i get an error.Some one told me that i just have to convert ,char* filnavn1 to a wide char format string Can some one show me how I do that. HANDLE hFile1; char *filename1; char *token; char seps[] = ","; char string[] = "\\windows\\2004-10-T-gr1-tstTag1.csv,\\windows\\2004-10-T-gr1-tstTag2.csv"; token = strtok( string, seps ); filnavn1 = token; hFile1 = CreateFile (filnavn1, // Open .txt GENERIC_READ, // Open for reading 0, // Do not share NULL, // No security OPEN_EXISTING, // Existing file only FILE_ATTRIBUTE_NORMAL, // Normal file NULL); // No template file Get this error: cannot convert parameter 1 from 'char *' to 'const unsigned short *

    C J G 4 Replies Last reply
    0
    • G gunnar66

      I try to create a file, the filename i will get from a string. What do I do wrong. It works ok in Visual C++, but in Emededd C++ i get an error.Some one told me that i just have to convert ,char* filnavn1 to a wide char format string Can some one show me how I do that. HANDLE hFile1; char *filename1; char *token; char seps[] = ","; char string[] = "\\windows\\2004-10-T-gr1-tstTag1.csv,\\windows\\2004-10-T-gr1-tstTag2.csv"; token = strtok( string, seps ); filnavn1 = token; hFile1 = CreateFile (filnavn1, // Open .txt GENERIC_READ, // Open for reading 0, // Do not share NULL, // No security OPEN_EXISTING, // Existing file only FILE_ATTRIBUTE_NORMAL, // Normal file NULL); // No template file Get this error: cannot convert parameter 1 from 'char *' to 'const unsigned short *

      C Offline
      C Offline
      Cedric Moonen
      wrote on last edited by
      #2

      Instead of using char*, try to use TCHAR*. So, for example for string: TCHAR* string = _T("\\windows\\2004-10-T-gr1-tstTag1.csv,\\windows\\2004-10-T-gr1-tstTag2.csv"); And instead of strtok, use wcstok.

      G 1 Reply Last reply
      0
      • C Cedric Moonen

        Instead of using char*, try to use TCHAR*. So, for example for string: TCHAR* string = _T("\\windows\\2004-10-T-gr1-tstTag1.csv,\\windows\\2004-10-T-gr1-tstTag2.csv"); And instead of strtok, use wcstok.

        G Offline
        G Offline
        gunnar66
        wrote on last edited by
        #3

        Still get an error cstok' : cannot convert parameter 2 from 'char [2]' to 'const unsigned short * int ch = ','; TCHAR* token; int i = 0; int ch = ','; TCHAR* filnavn1; TCHAR* string = _T("\\windows\\2004-10-T-gr1-tstTag1.csv,\\windows\\2004-10-T-gr1-tstTag2.csv");

        C 1 Reply Last reply
        0
        • G gunnar66

          Still get an error cstok' : cannot convert parameter 2 from 'char [2]' to 'const unsigned short * int ch = ','; TCHAR* token; int i = 0; int ch = ','; TCHAR* filnavn1; TCHAR* string = _T("\\windows\\2004-10-T-gr1-tstTag1.csv,\\windows\\2004-10-T-gr1-tstTag2.csv");

          C Offline
          C Offline
          Cedric Moonen
          wrote on last edited by
          #4

          Always use TCHAR instead of chars. This error comes because you pass a char array instead of a TCHAR array.

          1 Reply Last reply
          0
          • G gunnar66

            I try to create a file, the filename i will get from a string. What do I do wrong. It works ok in Visual C++, but in Emededd C++ i get an error.Some one told me that i just have to convert ,char* filnavn1 to a wide char format string Can some one show me how I do that. HANDLE hFile1; char *filename1; char *token; char seps[] = ","; char string[] = "\\windows\\2004-10-T-gr1-tstTag1.csv,\\windows\\2004-10-T-gr1-tstTag2.csv"; token = strtok( string, seps ); filnavn1 = token; hFile1 = CreateFile (filnavn1, // Open .txt GENERIC_READ, // Open for reading 0, // Do not share NULL, // No security OPEN_EXISTING, // Existing file only FILE_ATTRIBUTE_NORMAL, // Normal file NULL); // No template file Get this error: cannot convert parameter 1 from 'char *' to 'const unsigned short *

            J Offline
            J Offline
            jan larsen
            wrote on last edited by
            #5

            As I told you in another thread, there seems to be something fishy about the way your compiler handles the typedefs for TCHAR, PSTR, LPSTR and so on. When you invoke CreateFile, you actually invoke either CreateFileA or CreateFileW based on whether UNICODE is defined or not. Based on the error message, it seems that you are calling CreateFileW, which takes a unsigned short * as the filename parameter. This indicates that UNICODE is defined. I have no idea about what you mean when you mention "Emededd C++", but somewhere UNICODE is defined either explicit or implicit. You have some considerations to do: 1. If you are sure that you only need to build for unicode, then you could start to use the proper functions explicit like this:

            HANDLE hFile1;
            unsigned short *filename1;
            unsigned short *token;
            unsigned short seps[] = L",";
            unsigned short string[] = L"\\windows\\2004-10-T-gr1-tstTag1.csv,\\windows\\2004-10-T-gr1-tstTag2.csv";
            token = wcstok( string, seps );
            filnavn1 = token;
            hFile1 = CreateFile (filnavn1, // Open .txt
            GENERIC_READ, // Open for reading
            0, // Do not share
            NULL, // No security
            OPEN_EXISTING, // Existing file only
            FILE_ATTRIBUTE_NORMAL, // Normal file
            NULL); // No template file

            2. Or you could find out why your compiler insists on using the *W functions, but in the same breath refuses to translate TCHAR to unsigned short. I would go for #2 :-) By the way, for runtime translation of string types: look up MultiByteToWideChar(), but as I said, you main problem is the confusion of string types. "After all it's just text at the end of the day. - Colin Davies "For example, when a VB programmer comes to my house, they may say 'does your pool need cleaning, sir ?' " - Christian Graus

            A 1 Reply Last reply
            0
            • J jan larsen

              As I told you in another thread, there seems to be something fishy about the way your compiler handles the typedefs for TCHAR, PSTR, LPSTR and so on. When you invoke CreateFile, you actually invoke either CreateFileA or CreateFileW based on whether UNICODE is defined or not. Based on the error message, it seems that you are calling CreateFileW, which takes a unsigned short * as the filename parameter. This indicates that UNICODE is defined. I have no idea about what you mean when you mention "Emededd C++", but somewhere UNICODE is defined either explicit or implicit. You have some considerations to do: 1. If you are sure that you only need to build for unicode, then you could start to use the proper functions explicit like this:

              HANDLE hFile1;
              unsigned short *filename1;
              unsigned short *token;
              unsigned short seps[] = L",";
              unsigned short string[] = L"\\windows\\2004-10-T-gr1-tstTag1.csv,\\windows\\2004-10-T-gr1-tstTag2.csv";
              token = wcstok( string, seps );
              filnavn1 = token;
              hFile1 = CreateFile (filnavn1, // Open .txt
              GENERIC_READ, // Open for reading
              0, // Do not share
              NULL, // No security
              OPEN_EXISTING, // Existing file only
              FILE_ATTRIBUTE_NORMAL, // Normal file
              NULL); // No template file

              2. Or you could find out why your compiler insists on using the *W functions, but in the same breath refuses to translate TCHAR to unsigned short. I would go for #2 :-) By the way, for runtime translation of string types: look up MultiByteToWideChar(), but as I said, you main problem is the confusion of string types. "After all it's just text at the end of the day. - Colin Davies "For example, when a VB programmer comes to my house, they may say 'does your pool need cleaning, sir ?' " - Christian Graus

              A Offline
              A Offline
              Antony M Kancidrowski
              wrote on last edited by
              #6

              jan larsen wrote: I have no idea about what you mean when you mention "Emededd C++" FYI, Microsoft eMbedded C++[^] Is a development IDE for writing code for Windows CE based devices. It instists on UNICODE. Ant. I'm hard, yet soft.
              I'm coloured, yet clear.
              I'm fruity and sweet.
              I'm jelly, what am I? Muse on it further, I shall return!
              - David Walliams (Little Britain)

              J 1 Reply Last reply
              0
              • A Antony M Kancidrowski

                jan larsen wrote: I have no idea about what you mean when you mention "Emededd C++" FYI, Microsoft eMbedded C++[^] Is a development IDE for writing code for Windows CE based devices. It instists on UNICODE. Ant. I'm hard, yet soft.
                I'm coloured, yet clear.
                I'm fruity and sweet.
                I'm jelly, what am I? Muse on it further, I shall return!
                - David Walliams (Little Britain)

                J Offline
                J Offline
                jan larsen
                wrote on last edited by
                #7

                Ah, "Embedded", I had a vague idea that this was probably the case :-), I didn't know it insisted on UNICODE. "After all it's just text at the end of the day. - Colin Davies "For example, when a VB programmer comes to my house, they may say 'does your pool need cleaning, sir ?' " - Christian Graus

                A 1 Reply Last reply
                0
                • G gunnar66

                  I try to create a file, the filename i will get from a string. What do I do wrong. It works ok in Visual C++, but in Emededd C++ i get an error.Some one told me that i just have to convert ,char* filnavn1 to a wide char format string Can some one show me how I do that. HANDLE hFile1; char *filename1; char *token; char seps[] = ","; char string[] = "\\windows\\2004-10-T-gr1-tstTag1.csv,\\windows\\2004-10-T-gr1-tstTag2.csv"; token = strtok( string, seps ); filnavn1 = token; hFile1 = CreateFile (filnavn1, // Open .txt GENERIC_READ, // Open for reading 0, // Do not share NULL, // No security OPEN_EXISTING, // Existing file only FILE_ATTRIBUTE_NORMAL, // Normal file NULL); // No template file Get this error: cannot convert parameter 1 from 'char *' to 'const unsigned short *

                  J Offline
                  J Offline
                  jan larsen
                  wrote on last edited by
                  #8

                  Thanks to Antony for clearing up some things here. Embedded C++ insists on UNICODE, this means that if you want to build for both platforms, you should use the proper typedefs when working with strings. Michael Dunn has written a very good article on this: The Complete Guide to C++ Strings[^] So it seems that the compiler confusion springs from: When compiling for the embedded environment, it will allways link to wide character functions, this means that you can't use char, and that, when you compile for the embedded environment, have to define UNICODE. "After all it's just text at the end of the day. - Colin Davies "For example, when a VB programmer comes to my house, they may say 'does your pool need cleaning, sir ?' " - Christian Graus

                  1 Reply Last reply
                  0
                  • G gunnar66

                    I try to create a file, the filename i will get from a string. What do I do wrong. It works ok in Visual C++, but in Emededd C++ i get an error.Some one told me that i just have to convert ,char* filnavn1 to a wide char format string Can some one show me how I do that. HANDLE hFile1; char *filename1; char *token; char seps[] = ","; char string[] = "\\windows\\2004-10-T-gr1-tstTag1.csv,\\windows\\2004-10-T-gr1-tstTag2.csv"; token = strtok( string, seps ); filnavn1 = token; hFile1 = CreateFile (filnavn1, // Open .txt GENERIC_READ, // Open for reading 0, // Do not share NULL, // No security OPEN_EXISTING, // Existing file only FILE_ATTRIBUTE_NORMAL, // Normal file NULL); // No template file Get this error: cannot convert parameter 1 from 'char *' to 'const unsigned short *

                    G Offline
                    G Offline
                    gunnar66
                    wrote on last edited by
                    #9

                    TCHAR* filnavn1 did it. Did not know that it was UNICODE. All works fine now. Thanks to all

                    1 Reply Last reply
                    0
                    • J jan larsen

                      Ah, "Embedded", I had a vague idea that this was probably the case :-), I didn't know it insisted on UNICODE. "After all it's just text at the end of the day. - Colin Davies "For example, when a VB programmer comes to my house, they may say 'does your pool need cleaning, sir ?' " - Christian Graus

                      A Offline
                      A Offline
                      Antony M Kancidrowski
                      wrote on last edited by
                      #10

                      Sorry, I think it is rather that CE requires UNICODE therefore the API implementations are all UNICODE. Ant. I'm hard, yet soft.
                      I'm coloured, yet clear.
                      I'm fruity and sweet.
                      I'm jelly, what am I? Muse on it further, I shall return!
                      - David Walliams (Little Britain)

                      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