|
Hi Sohail,
I have another questions, which I have not found answer for a long time. I saw you are a COM Guru here. Not sure you know the answer.
The question is, then we use __uuid of macro to convert from Coclass name or interface name in IDL to GUID, why we have a prefix attribute __declspec?
For example, __declspec(uuidof("SomeCoClassName")?
regards,
George
|
|
|
|
|
I am no COM Guru. There are many Guru's who have published in depth articles on COM on CP
Well __declspec is used to apply Microsoft specific extended attributes, one of them being uuid which attached a GUID to a class. For example - novtable is one such attribute which when attached to a class will stop the compiler from generating a vtable.
For more details see the following link -
__declspec[^]
uuid[^]
__uuidof[^]
Sohail
modified 21-Apr-21 21:01pm.
|
|
|
|
|
Thanks Sohail,
I like your style of providing many links.
My confusion is, in the MSDN page of uuid, what means "attaches a GUID to a class or structure declared or defined (full COM object definitions only) with the uuid attribute.".
The input for uuid is a string format of CLSID, the output is what? The most confusion is what means "attaches".
regards,
George
|
|
|
|
|
hi i have a code which was supposed to read data from serial com (bluetooth) and save in a file.
but when i send a audio file from my mobile to pc the program will not read the port i have
configured the bluetooth connection for serial communication (i.e from system stray selecting bluetooth icon and from quick connect selecting serial connection). instead it shows that connection is succesfull for PIM transfer, so do i need to write a application in my mobile to send data serially.. if u have any details about it please post me.. (right now i dont have any application in mobile iam directly sending file via bluetooth)...
|
|
|
|
|
Are you sure your mobile supports the Bluetooth Serial Port Profile?
Try to use Hyperterminal, or similar, to open a serial connection to the mobile and read data. When you get that working you can worry about your code on the PC side.
"It's supposed to be hard, otherwise anybody could do it!" - selfquote "High speed never compensates for wrong direction!" - unknown
|
|
|
|
|
hi the at command works correctly it echos ok.
this is my read data code
<br />
bool ReadPort()<br />
{<br />
long int dwSize = 0;<br />
bool hResult = false;<br />
std::string sb = "";<br />
DWORD dwEventMask;<br />
<br />
if(!SetCommMask(hComm, EV_RXCHAR)) <br />
{ <br />
return hResult;<br />
}<br />
<br />
if(WaitCommEvent(hComm, &dwEventMask, NULL)) <br />
{<br />
char szBuf[1024];<br />
DWORD dwIncommingReadSize;<br />
<br />
do<br />
{<br />
if(ReadFile(hComm, &szBuf, 1024, &dwIncommingReadSize, NULL) != 0)<br />
{<br />
if(dwIncommingReadSize > 0)<br />
{<br />
dwSize += dwIncommingReadSize;<br />
sb.append(szBuf);<br />
}<br />
hResult = true;<br />
}<br />
<br />
else<br />
{<br />
unsigned long error = ::GetLastError();<br />
hResult = false;<br />
printf("the error while reading error is %dl\n", error);<br />
break;<br />
}<br />
} while(dwIncommingReadSize > 0);<br />
<br />
*readData = new char[dwSize];<br />
strcpy(*readData, sb.c_str());<br />
return hResult;<br />
}<br />
else<br />
{<br />
return hResult;<br />
}<br />
<br />
}<br />
it stops to WaitCommEvent function i feel i need to do some settings for my phone and pc to transfer the data serially but now it is PIM iam totally blank if u have any such settings for the transfer of data via serial com of bluetooth plz send in me... my phone is nokia 6288 i know this is not the forum for phone settings but please help me...
|
|
|
|
|
When using the "Bluetooth Serial Port Profile" there are three main issues to overcome:
1. Setting up the Bluetooth connection to make it behave like a serial device and
2. Setting up the serial device
3. Read and write from/to the port
When you say you have tried to open the serial device, but it presents itself as "PIM" instead of a serial device, you probably have a problem with the first issue above. To verify this you can try using the Hyperterminal to send and receive the exact same data with the same port settings as you're trying to do in your code.
If that test succeeds you can start worrying about your code.
Regarding programming for serial communication I have done it quite a lot, but I've never used ::SetCommMask() or ::WaitCommEvent() .
I use overlapped mode and I have one worker thread that reads data and another worker thread that sends data. The data to be sent is put in a queue from which the sending thread reads it and calls ::WriteFile() . A semaphore that the sending thread is waiting on with ::WaitForMultipleObjects() is released when a new block of data is added to the send queue.
The reading thread issues a read request with a call to ::ReadFile() . Since overlapped mode is used the call will return immediately and the reading thread can wait on the event in the OVERLAPPED struct that will get signalled when data arrives on the port. The read data will be put in a queue and a user defined windows message gets posted to the main thread in order to inform it about the newly received data so it can take proper action.
Of course the queues are protected with critical sections.
Joseph Newcomer has written a good article about serial port programming here[^].
He uses a slightly different approach than I've described above, but the mind set is very much the same.
"It's supposed to be hard, otherwise anybody could do it!" - selfquote "High speed never compensates for wrong direction!" - unknown
|
|
|
|
|
Hello everyone,
String of STL does not contain trim/toupper/tolower, currently I always implement them manually. For trim, I use find first/last not of space technique, and for toupper/tolower, I always change them manually by adding some value to 'a' to form 'A' or vice versa.
My question is I want to find some best practices to implement them (one reason is I think my private implementation may be error prone and the other reason is I am not sure whether there is existing APIs which could be utilized) and comment about my current implementation issues.
thanks in advance,
George
|
|
|
|
|
George_George wrote: For trim, I use find first/last not of space technique, and for toupper/tolower, I always change them manually by adding some value to 'a' to form 'A' or vice versa.
My question is I want to find some best practices to implement them (one reason is I think my private implementation may be error prone and the other reason is I am not sure whether there is existing APIs which could be utilized) and comment about my current implementation issues.
For tolower and toupper use std::transform .
std::string Str = "nibu babu thomas";
std::transform( Str.begin(), Str.end(), Str.begin(), toupper );
std::cout << Str;
Nibu babu thomas
Microsoft MVP for VC++
Code must be written to be read, not by the compiler, but by another human being.
Programming Blog: http://nibuthomas.wordpress.com
|
|
|
|
|
Thanks Nibu!
How about trim? No built-in APIs?
regards,
George
|
|
|
|
|
George_George wrote: How about trim? No built-in APIs?
I couldn't find one for std::string , but here is a simple routine using std functions you mentioned in your first post...
void Trim( const std::string& StrToTrim, std::string& TrimmedString )
{
std::string::size_type First = StrToTrim.find_first_not_of( ' ' );
std::string::size_type Last = StrToTrim.find_last_not_of( ' ' ) + 1;
TrimmedString = StrToTrim.substr( First, Last - First );
}
int main()
{
std::string StrToTrim = " 32 Nibu babu thomas 2342 2 23 3 ";
std::string TrimmedString = "On return will hold trimmed string";
Trim( StrToTrim, TrimmedString );
return 0;
}
There might be other options that could perform better than this but this one is neat!
Nibu babu thomas
Microsoft MVP for VC++
Code must be written to be read, not by the compiler, but by another human being.
Programming Blog: http://nibuthomas.wordpress.com
|
|
|
|
|
Cool Nibu!
I want to find all possible exception which may be thrown from STL functions, for example, string.substr may throw out of range exception. But such information can not be found from MSDN. Do you have any reference documents?
regards,
George
|
|
|
|
|
George_George wrote: Do you have any reference documents?
Most of them are in the files exception and stdexcept . Note that there is no .h in these file names.
Nibu babu thomas
Microsoft MVP for VC++
Code must be written to be read, not by the compiler, but by another human being.
Programming Blog: http://nibuthomas.wordpress.com
|
|
|
|
|
Thanks Nibu,
1.
No document which describes all possible exceptions may be thrown by a specific STL function, like string.substr?
2.
"the files exception and stdexcept" -- yes, but for a specific STL function like string.substr, how do you check all possible exceptions may be thrown by looking at the file?
regards,
George
|
|
|
|
|
George_George wrote: 1.
No document which describes all possible exceptions may be thrown by a specific STL function, like string.substr?
2.
"the files exception and stdexcept" -- yes, but for a specific STL function like string.substr, how do you check all possible exceptions may be thrown by looking at the file? Smile
I am not sure where you can find them, but here is a more secure version of Trim .
void Trim( const std::string& StrToTrim, std::string& TrimmedString )
{
std::string::size_type First = StrToTrim.find_first_not_of( ' ' );
if( First == std::string::npos )
{
First = 0;
}
std::string::size_type Last = StrToTrim.find_last_not_of( ' ' );
if( Last != std::string::npos )
{
Last = ( Last + 1 ) - First;
}
TrimmedString = StrToTrim.substr( First, Last );
}
Earlier function will throw an out_of_range exception if we gave a string like " " for trimming. This one doesn't.
Nibu babu thomas
Microsoft MVP for VC++
Code must be written to be read, not by the compiler, but by another human being.
Programming Blog: http://nibuthomas.wordpress.com
|
|
|
|
|
Thanks Nibu!
I want to find all possible exception which may be thrown from STL functions, for example, string.substr may throw out of range exception. But such information can not be found from MSDN. Do you have any reference documents?
regards,
George
|
|
|
|
|
As you know that CString have functions for all the above things, I think refering to CString code will help you to understand the best methods.
|
|
|
|
|
Any non-MFC solutions?
regards,
George
|
|
|
|
|
|
Thanks Rajesh,
I want to find all possible exception which may be thrown from STL functions, for example, string.substr may throw out of range exception. But such information can not be found from MSDN. Do you have any reference documents?
regards,
George
|
|
|
|
|
#include <iostream.h>
class Person
{
string name;
int age;
public:
string GetName(void);
int GetAge(void);
Person(string personName,int personAge);
Person(void);
};
int main()
{
Person *person = new Person("Chris",30);
cout << person->GetName() << endl;
cout << person->GetAge() << endl;
Person anotherPerson;
cout << anotherPerson.GetName() << endl;
cout << anotherPerson.GetAge() << endl;
delete person;
return 0;
}
string Person::GetName()
{
return name;
}
int Person::GetAge()
{
return age;
}
Person::Person(string personName,int personAge)
{
name = personName;
age = personAge;
}
Person::Person()
{
name = "Unknown";
age = 0;
} I used to program using C# and learning C++ now. I have some doubts on the above shown program.
1 - Person anotherPerson is declared but not initialized. But still calls to anotherPerson.GetName() worked. How this is happening?
2 - Is there any difference when allocating memory for *person and anotherPerson ?
3 - I know we should delete the pointer variables to claim memory. But AFAIK, there is no need to delete the anotherPerson variable. How it gets removed from memory?
|
|
|
|
|
Christian Flutcher wrote: 1 - Person anotherPerson is declared but not initialized. But still calls to anotherPerson.GetName() worked. How this is happening?
The compiler provides a default constructor for the class. So when you create an object for the class the default constructor gets invoked and the object is initialized.
Christian Flutcher wrote: 2 - Is there any difference when allocating memory for *person and anotherPerson?
Yes. The memory is allocated from heap in case you use new (ie *person), otherwise it is allocated from stack.
Christian Flutcher wrote: 3 - I know we should delete the pointer variables to claim memory. But AFAIK, there is no need to delete the anotherPerson variable. How it gets removed from memory?
The memory allocated is returned to the stack once the object goes out of scope
Somethings seem HARD to do, until we know how to do them.
_AnShUmAn_
modified on Friday, September 5, 2008 1:33 AM
|
|
|
|
|
Thanks
_AnShUmAn_ wrote: Yes. The memory is allocated from heap in case you use new (ie *person), otherwise it is allocated from stack.
Well, so which one is the recommended practice? use pointer variables always and delete after usage or use the normal one which goes to stack?
|
|
|
|
|
Depends really of the situation, sometimes you will need to use pointers so you don't have a choice. I tend to prefer using variables on the stack rather than on the heap when I can. Allocating/deallocating a lot of variables on the heap can lead to memory fragmentation which can be an issue if you tend to do it a lot. Furthermore, the cost (in time) of creating an object on the heap is a bit higher than creating an object on the stack.
|
|
|
|
|
Cedric Moonen wrote: Allocating/deallocating a lot of variables on the heap can lead to memory fragmentation which can be an issue if you tend to do it a lot
Are there any rules which should be considered when instantiating objects? How do you decide where the object should go? I am bit confused
|
|
|
|