Results 1 to 13 of 13

Thread: 'CreateFileW' : cannot convert parameter 1 from 'const char [13]' to 'LPCWSTR'

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Jan 2007
    Posts
    27

    'CreateFileW' : cannot convert parameter 1 from 'const char [13]' to 'LPCWSTR'

    Hai this is senthil...

    I had tried to write a string which fetched from a database. into a file...

    when i tried to compile the solution the following error occurs like this

    error C2664: 'CreateFileW' : cannot convert parameter 1 from 'const char [13]' to 'LPCWSTR'
    Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast


    Code:
    My codings are below...
    ----------------------------------------------------------------------
    void main()
    {
    
      MYSQL *conn; 
      conn = mysql_init(NULL);
      mysql_real_connect(conn,host,username,password,database,0,NULL,0);     
      MYSQL_RES *res_set; 
      MYSQL_ROW row;
      mysql_query(conn,"SELECT * FROM imagedb2");
      res_set = mysql_store_result(conn);
      unsigned int numrows = mysql_num_rows(res_set);
    	
       //For Creating a file....
      HANDLE hFile; 
      DWORD wmWritten;   
      hFile =CreateFile"d:\\text3.doc",GENERIC_READ|GENERIC_WRITE,             
      FILE_SHARE_READ,NULL,OPEN_ALWAYS,FILE_ATTRIBUTE_NORMAL,NULL); 
      
       while (( row = mysql_fetch_row(res_set)) != NULL)
    {		
    
           char str[255];
           char *tmp = str;
           strcpy(str, row[2]); 
    
    				       
           WriteFile(hFile,str,(DWORD)(sizeof(str)),&wmWritten,NULL); 
           CloseHandle(hFile); 
         
    			        	                   
    		}
               mysql_close(conn);
    getch();
    	
       }
    In Project->Properties->General->Character Set::Use Multi-Byte Character Set

    This configuration proterties i'm using....

    What shall i do for the above error...

    Can any one plzz help me??

  2. #2
    New Member
    Join Date
    Dec 2006
    Posts
    4

    Re: 'CreateFileW' : cannot convert parameter 1 from 'const char [13]' to 'LPCWSTR'

    Hi Senthil,
    Although I can't take the time to research a full solution, you're clearly dealing with a conflict between single byte and multi byte strings. I run into this all the time with CE. The compiler is using the wide char version of CreateFile (CreateFileW) which needs wide char parameters (wchar_t), yet str is a single byte string (char). You'll need to switch one or the other - either get rid of the multi byte compiler option, or make str a multi byte string.

  3. #3
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594

    Re: 'CreateFileW' : cannot convert parameter 1 from 'const char [13]' to 'LPCWSTR'

    The latter is the better option.
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

  4. #4
    Hyperactive Member wiccaan's Avatar
    Join Date
    Apr 2004
    Location
    127.0.0.1
    Posts
    475

    Re: 'CreateFileW' : cannot convert parameter 1 from 'const char [13]' to 'LPCWSTR'

    Not sure if this will fix your error but I did notice you're missing a parenthese in your CreateFile line:

    Code:
      hFile =CreateFile("d:\\text3.doc",GENERIC_READ|GENERIC_WRITE,             
      FILE_SHARE_READ,NULL,OPEN_ALWAYS,FILE_ATTRIBUTE_NORMAL,NULL);
    If my post was helpful please rate it

  5. #5

    Thread Starter
    Junior Member
    Join Date
    Jan 2007
    Posts
    27

    Re: 'CreateFileW' : cannot convert parameter 1 from 'const char [13]' to 'LPCWSTR'

    Hi to all,

    thanks for ur response

    i already found the solution...

    sorry for saying it in forum...

    i just put 'L' in the CreateFile method...

    hFile = CreateFile(L"C:\\tab.doc",GENERIC_READ|GENERIC_WRITE,
    // FILE_SHARE_READ,NULL,OPEN_ALWAYS,FILE_ATTRIBUTE_NORMAL,NULL);

    thanks...
    senthil..

  6. #6
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594

    Re: 'CreateFileW' : cannot convert parameter 1 from 'const char [13]' to 'LPCWSTR'

    No! Wrong!
    You have three valid options. All others are invalid. The third of these options is the best.

    Option 1: Force ANSI (slower on NT, doesn't work on CE, possible problems with internationalization)
    Code:
    hFile = CreateFileA("C:\\tab.doc",GENERIC_READ|GENERIC_WRITE,
     FILE_SHARE_READ,NULL,OPEN_ALWAYS,FILE_ATTRIBUTE_NORMAL,NULL);
    Option 2: Force Unicode (doesn't work on 9x)
    Code:
    hFile = CreateFileW(L"C:\\tab.doc",GENERIC_READ|GENERIC_WRITE,
     FILE_SHARE_READ,NULL,OPEN_ALWAYS,FILE_ATTRIBUTE_NORMAL,NULL);
    Option 3: Make it easily switchable in the project settings (easy to get working and ideal versions for everywhere)
    Code:
    hFile = CreateFile(TEXT("C:\\tab.doc"),GENERIC_READ|GENERIC_WRITE,
     FILE_SHARE_READ,NULL,OPEN_ALWAYS,FILE_ATTRIBUTE_NORMAL,NULL);
    If you include <tchar.h>, you can use _TEXT() or _T() instead of TEXT(), but that is the only permitted variation. Everything else is invalid.
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

  7. #7

    Thread Starter
    Junior Member
    Join Date
    Jan 2007
    Posts
    27

    Re: 'CreateFileW' : cannot convert parameter 1 from 'const char [13]' to 'LPCWSTR'

    Ya okay,

    But in my program that L method works well...

    there is no problem....

    It easily creates any type of file....

    thanks for ur reply...
    senthil.

  8. #8
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594

    Re: 'CreateFileW' : cannot convert parameter 1 from 'const char [13]' to 'LPCWSTR'

    Yeah, sure. Now go into the project settings and change using Unicode to multi-byte character set and see if it still compiles.

    Or take your current compiled executable to a Win98 computer and start it there.
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

  9. #9
    Frenzied Member
    Join Date
    Aug 2000
    Location
    Birmingham, AL
    Posts
    1,276

    Re: 'CreateFileW' : cannot convert parameter 1 from 'const char [13]' to 'LPCWSTR'

    This is off the subject a little but most functions manipulating characters have a _w_ version such as wprintf for printf but what can you use in place of atof to make your code portable?

    EDIT: sorry, I found it. _wtof for anyone interested. Not sure of why the underscore is used though.
    Last edited by wey97; Jan 10th, 2007 at 02:16 PM.

  10. #10
    Frenzied Member
    Join Date
    Aug 2000
    Location
    Birmingham, AL
    Posts
    1,276

    Re: 'CreateFileW' : cannot convert parameter 1 from 'const char [13]' to 'LPCWSTR'

    What's the best way to handle strings and wstrings besides changing between both on a recompile?

  11. #11
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594

    Re: 'CreateFileW' : cannot convert parameter 1 from 'const char [13]' to 'LPCWSTR'

    The underscore is because it's a compiler extension, not part of the standard.

    What exactly do you mean by handle?
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

  12. #12
    Frenzied Member
    Join Date
    Aug 2000
    Location
    Birmingham, AL
    Posts
    1,276

    Re: 'CreateFileW' : cannot convert parameter 1 from 'const char [13]' to 'LPCWSTR'

    Quote Originally Posted by CornedBee
    The underscore is because it's a compiler extension, not part of the standard.

    What exactly do you mean by handle?
    1) I decided to use ostringstream and istringstream for conversion back and forth between numeric and text.

    2) Lets say you are compiling in Unicode and declare some std::wstrings. There is no portable or generic way (that I know of) to switch right to Multi-Byte without changing all of your std::wstring references to std::string.

    Unless you declare something like this:
    Code:
    #ifdef UNICODE
    typedef std::wstring _string;
    #else
    typedef std::string _string;
    #endif
    Then declare only _strings so you don't have to rename your strings to wstrings and vice versa.

  13. #13
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594

    Re: 'CreateFileW' : cannot convert parameter 1 from 'const char [13]' to 'LPCWSTR'

    Nothing simpler. std::string and std::wstring are both just typedefs in the first place. The real stuff is a template called std::basic_string, which gets instantiated (among other things) with the character type. Like this:
    Code:
    namespace std {
    typedef basic_string<char> string;
    typedef basic_string<wchar_t> wstring;
    }
    Getting a switching string therefore is as simply as
    Code:
    typedef std::basic_string<TCHAR> tstring;
    Then just use tstring everywhere and you're all set.

    The same applies to many more things: std::istream is a typedef for std::basic_istream<char>, std::istringstream for std::basic_istringstream<char>. They all have wchar_t versions, and you can instantiate them all with TCHAR.

    I dug up some old threads of mine on the topic.
    http://www.vbforums.com/showthread.p...hlight=tstring
    http://www.vbforums.com/showthread.p...hlight=tstring
    http://www.vbforums.com/showthread.p...hlight=tstring (My goodness, this one's old. Parksie replied in it.)

    Ah, the walk down memory lane. To think that I, too, once wanted to derive from std::string.

    I attached my tcpp.h. Note that the file is a few years old. It violates the C++ standard by introducing new names into the std namespace, so you should change that, but otherwise it's still fine.
    Attached Files Attached Files
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width