CString conversion for socket send()

This is a discussion on CString conversion for socket send() within the C++ Programming forums, part of the General Programming Boards category; I hate to send this as I have seen so many postings on this subject, but I can not seem ...

  1. #1
    Registered User
    Join Date
    May 2004
    Posts
    164

    Unhappy CString conversion for socket send()

    I hate to send this as I have seen so many postings on this subject, but I can not seem to make this work.

    Ok, so I have a simple socket app that is barking at my send command

    Code:
     error C2664: 'strlen' : cannot convert parameter 1 from 'class CString' to 'const char *'
            No user-defined-conversion operator available that can perform this conversion, or the operator cannot be called
    C:\TRANSFERLAP\MEATPLANT\Mobile\MobileDlg.cpp(187) : error C2664: 'send' : cannot convert parameter 2 from 'class CString' to 'const char *'
            No user-defined-conversion operator available that can perform this conversion, or the operator cannot be called
    Here is the code that I used:
    Code:
    #include <stdio.h>
    #include <winsock.h>
    #include <windows.h>
    #include "string"
    #include "cstring"
    using namespace std;
    {
      
    		CString m_BarCode;
    
    		m_BarCode +=STR;
    		m_BarCode +=CD128;
    		m_BarCode +=m_Barcode;
    		m_BarCode +=CR;
    		
            //accept() will accept an incoming
            //client connection
            client=accept(server,
                (struct sockaddr*)&from,&fromlen);
    
            //send this string to the client
    		send(client,m_BarCode,strlen(m_BarCode),0);
    
           
            //close the client socket and clear variable data for next pass, wait a few
    		//seconds to ensure the network transfer of info was complete.
    		Sleep (3000);
    		m_BarCode ="";
    		m_Barcode ="";
    		UpdateData (false);
            closesocket(client);
    		closesocket(server);
    		break;
    }
    the funny thing is this actually works in regular windows, its my embedded c++ windows ce compiler where I get this error. I don't of any forums for CE but everyone here is always very help, can anyone help explain how I might convert my CString variable to a const char that might be excepted by my send () command?

  2. #2
    Sweet
    Join Date
    Aug 2002
    Location
    Tucson, Arizona
    Posts
    1,801
    Well first off what the heck is a CString. I know what a std::string is but not a CString. If you are using a std::string the you would use the c_str() which returns a c-style string.
    Woop?

  3. #3
    Tropical Coder Darryl's Avatar
    Join Date
    Mar 2005
    Location
    Cayman Islands
    Posts
    503
    Yea that is wierd, a CString is a MFC string, but you are not using MFC... You included the <cstring> header but it just defines functions to be used with a char*/char arrays "string" and not CStrings. You can use the std::string defined in <string>, however those socket functions won't take them either, directly. You'll have to convert them with the member function string.c_str(). It's read only though so you won't be able to write to it. You will have to use a char array/char* for that.

  4. #4
    the hat of redundancy hat nvoigt's Avatar
    Join Date
    Aug 2001
    Location
    Hannover, Germany
    Posts
    3,139
    This seems to be an MFC application. CString is the MFC string class. It has an operator LPCTSTR ( which in normal non-UNICODE programms is just a #define for const char* ).

    Try this:

    send( client, (LPCTSTR)m_BarCode, m_BarCode.GetLength(), 0 );


    I see no differences in string handling between CE and normal windows.

    http://msdn.microsoft.com/library/de...tm/cstring.asp
    hth
    -nv

    She was so Blonde, she spent 20 minutes looking at the orange juice can because it said "Concentrate."

    When in doubt, read the FAQ.
    Then ask a smart question.

  5. #5
    Sweet
    Join Date
    Aug 2002
    Location
    Tucson, Arizona
    Posts
    1,801
    ewwww MFC
    Woop?

  6. #6
    Tropical Coder Darryl's Avatar
    Join Date
    Mar 2005
    Location
    Cayman Islands
    Posts
    503
    It doesn't seem to be MFC at all, look at the errors and the names of the objects and files. MFC would had stuck a C on everything so it would be CMobileDlg.cpp for instance. Unless he didn't use the wizard, but still he's not included any MFC headers either. The only thing remotely MFC is his choice of names for his string CString which is probably an error.

  7. #7
    Registered User
    Join Date
    May 2004
    Posts
    164
    Thanks everyone for your replies, sorry for the late response, It is using MFC, I've been using two separate platforms, Windows XP using MSVisual 6.0 and for Windows CE using MSVisual Embedded 4.0.

    In most cases I've been able to just paste and copy my code from one to the other with no issues.

    I've always used the CString format with <cstring> and it has worked fine for me, apparently I am confused on the classes I am using and their properties.

  8. #8
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,647
    CE is UNICODE only. That means that CString is UNICODE only. That means that all you "const char*" castings of CString will not work under CE.

    In other words, LPCTSTR maps to "const wchar_t*" instead of "const char*".

    In this case, you can use a "std::string" which always uses "char" as the character type. And replace all the implicit calls to "operator const char*" with an explicit call to "c_str()".

    [EDIT]
    BTW - the CE CString docs are wrong
    >> You can freely substitute CString objects for const char* and LPCTSTR function arguments.
    Should say:
    >> You can freely substitute CString objects for const wchar_t* or LPCTSTR function arguments.

    gg
    Last edited by Codeplug; 04-26-2005 at 01:47 PM.

  9. #9
    Registered User
    Join Date
    May 2004
    Posts
    164
    cool, that gives me a good direction, I will work with that and write back, thanks for the info!

  10. #10
    Registered User
    Join Date
    May 2004
    Posts
    164
    Codeplug, let me first start by saying, you rock, your explanation was correct and I have now have code I can compile, Unfortunately since I used the mfc class variable CString, I had to change that to allow user input in my edit box for the user, so used the variable type DWORD, Not sure if thats going to work, I have never seen that variable type before, so I need to research that.

    Thanks for the insight on the class info and the pointer, it helped greatly.

  11. #11
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,647
    >> ....so used the variable type DWORD
    DWORD is basicaly an unsigned int. Aren't Code-128 barcodes alpha-numeric?

    You can use functions like wcstombs() to convert a wchar_t string to a char string. So you can keep using a CString for you edit-box, and convert it to a char string where you need it.

    gg

  12. #12
    Registered User
    Join Date
    May 2004
    Posts
    164
    Hey codeplug, thanks for looking again,
    I attempted to follow MSDN's example of wcstombs() with the following code and got barked at with an error:
    Code:
    string bc;
    		wcstombs (bc, m_Barcode, NULL);
    	m_BarCode += bc;
    m_Barcode is my edit box CString
    bc just being a string I use to store my conversion and then add it to my string m_BarCode.

  13. #13
    Registered User
    Join Date
    May 2004
    Posts
    164
    Sorry codeplug I forgot to send the error message, that might help identify where I am messing up.

    Code:
     error C2664: 'wcstombs' : cannot convert parameter 1 from 'class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >' to 'char *'
            No user-defined-conversion operator available that can perform this conversion, or the operator cannot be called
    C:\TRANSFERLAP\MEATPLANT\Mobile\MobileDlg.cpp(186) : error C2593: 'operator =' is ambiguous

  14. #14
    Registered User
    Join Date
    May 2004
    Posts
    164
    I finally figured out the example on MSDN, and the conversion is working well. Codeplug, Thanks a Million, that worked great!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 02:39 PM
  2. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM
  3. Problem with Visual C++ Object-Oriented Programming Book.
    By GameGenie in forum C++ Programming
    Replies: 9
    Last Post: 08-29-2005, 11:21 PM
  4. Header File Question(s)
    By AQWst in forum C++ Programming
    Replies: 10
    Last Post: 12-23-2004, 10:31 PM
  5. Help with CString class and operator+ overloading
    By skanxalot in forum C++ Programming
    Replies: 2
    Last Post: 10-07-2003, 10:23 AM

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