-
February 17th, 2009, 02:11 AM
#1
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%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.
-
February 17th, 2009, 02:14 AM
#2
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.
-
February 17th, 2009, 02:48 AM
#3
Re: Return value of CString[](Very Urgent..........)
Originally Posted by cilu
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.
-
February 17th, 2009, 04:13 AM
#4
Re: Return value of CString[](Very Urgent..........)
Originally Posted by priyasubramani
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|