CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4
  1. #1
    Join Date
    Feb 2009
    Posts
    24

    Return value of CString[](Very Urgent..........)

    Hi All,

    I have a basic doubt in C++. I have find the available ports. Code is:


    in .h file:

    CString getport();

    in .CPP file:

    CString CAvailablePortDlg::getport()
    {
    char port_name[16];
    static int count=0;
    CString port[16];

    for (int port_number = 1; port_number < 256; ++port_number)
    {
    port_number < 10 ?
    sprintf(port_name, "COM&#37;d", port_number) :
    sprintf(port_name, "\\\\.\\COM%d", port_number);
    HANDLE hFile = ::CreateFile(port_name,
    GENERIC_READ | GENERIC_WRITE, 0, NULL,
    OPEN_EXISTING, 0, NULL);
    if(hFile != INVALID_HANDLE_VALUE)
    {
    //std::cout << port_name << std::endl;
    MessageBox(port_name);
    port[count] = CString(port_name);
    count++;
    }
    else
    {
    DWORD err = GetLastError();
    if (err == ERROR_ACCESS_DENIED ||err == ERROR_SHARING_VIOLATION)
    //{
    std::cout << port_name << std::endl;
    // MessageBox(port_name);
    //}
    }
    CloseHandle(hFile);
    }
    return port[count];
    }


    I need to return all values in port[count].

    I know, this is basic question.

    Please help me.....................................

    Thanks & Regards,
    Priya
    Last edited by priyasubramani; February 17th, 2009 at 02:14 AM.

  2. #2
    Join Date
    Oct 2002
    Location
    Timisoara, Romania
    Posts
    14,360

    Re: Return value of CString[](Very Urgent..........)

    Why do you mingle apples with pears? You don't need that port_name. Use directly CString and its Format() method.

    You should use a container such as std::vector, CList, or CStringList to hold the ports and as return type of your function. I would prefer std::vector<CString>, but since CStringList is built in MFC for strings, you can use that too.
    Marius Bancila
    Home Page
    My CodeGuru articles

    I do not offer technical support via PM or e-mail. Please use vbBulletin codes.

  3. #3
    Join Date
    Feb 2009
    Posts
    24

    Re: Return value of CString[](Very Urgent..........)

    Quote Originally Posted by cilu View Post
    Why do you mingle apples with pears? You don't need that port_name. Use directly CString and its Format() method.

    You should use a container such as std::vector, CList, or CStringList to hold the ports and as return type of your function. I would prefer std::vector<CString>, but since CStringList is built in MFC for strings, you can use that too.
    I need the sample code for std::vector.
    Last edited by priyasubramani; February 17th, 2009 at 02:50 AM.

  4. #4
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,396

    Re: Return value of CString[](Very Urgent..........)

    Quote Originally Posted by priyasubramani View Post
    Code:
         for (int port_number = 1; port_number < 256; ++port_number)
         {
             port_number < 10 ?
                 sprintf(port_name, "COM%d", port_number) :
                 sprintf(port_name, "\\\\.\\COM%d", port_number);
    You could (and should) get rid of the syntax "COMx": it is used for x between 1 and 9 ONLY for backward compatibility with the very old DOS programs. The correct syntax for com ports is "\\.\COMx".
    So you may simplify your code to be just:
    Code:
          sprintf(port_name, "\\\\.\\COM%d", port_number);
    Besides, working with MFC you should forget sprintf exists. Using CString::Format is much easier and secure:
    Code:
    CString strPort;
    strPort.Format("\\\\.\\COM%d", port_number);
    Victor Nijegorodov

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

Featured