|
li zhiyuan wrote: int frequency[arraySize] = {0};
Initialize all the elements of the array with zero.
li zhiyuan wrote: srand(time(0))
Initialize the random number seed. Initializing with current time provides the random numbers always different. If the seed it not set with time, random numbers will always be same. For example, suppose first number you got is 1562 and the second is 7645, you will get same values in the the same order for each statting of the program. If we initialize the seed with time, first number depends on this seed and so on, since the time is incrementing...
- NS -
|
|
|
|
|
Hi, after programming in c++ for some time, I've decided to start to code in windows environment.
The question is, should I read code until I fully understand windows internals or should I start to make
my own programs and learn on the fly?
I've read Petzold's book but it seems to me that it its not enough to make my own app.
|
|
|
|
|
freeheap wrote: I've read Petzold's book but it seems to me that it its not enough to make my own app.
Do you mean "Programming Windows 5th Edition"? If so...
If you have read and understand everything in that book then you should
have the fundamentals every Windows programmer should know.
It's also the core fundamentals of MFC (and any other framework which wraps the Win32 APIs).
If you intend to write GUI applications and you haven't programmed for an
event/message-driven OS like Windows, then that's probably the most important
concept to understand from the book.
I would recommend starting with a Visual Studio generated Win32 application.
Look at the code produced by the wizard and make sure you understand everything
that's there.
From there you have a basic shell of an application that you can modify and extend
as you learn new APIs.
Just my 2 cents.
Have fun!
Mark
Mark Salsbery
Microsoft MVP - Visual C++
|
|
|
|
|
I would suggest a blend of both approaches.
Steve
|
|
|
|
|
My approch was doing something, then study...
- NS -
|
|
|
|
|
I suggest for start see create a simple dialog.
|
|
|
|
|
freeheap wrote: I've read Petzold's book but it seems to me that it its not enough to make my own app.
His Windows books are more than enough. I started with his Windows v3.1 book.
"A good athlete is the result of a good and worthy opponent." - David Crow
"To have a respect for ourselves guides our morals; to have deference for others governs our manners." - Laurence Sterne
|
|
|
|
|
Are you planning on using MFC or not? If not, I can recommend this one (although I hear Petzold's is pretty good too)... Clickety[^]
|
|
|
|
|
I am creating a thread in OnInitDialog() of dialog based application as:
hThread = CreateThread(NULL,0,&ThreadAudioProcess,this,CREATE_SUSPENDED,0); and starting it in "Start" button click by ResumeThread(hThread); . The function ThreadAudioProcess() is called this time.
But after I stop it when user click "Stop" by: SuspendThread(hThread); and start it again by ResumeThread(hThread); it is not calling the function. How to start the thread again and call the function?
Thanks & Regards,
Suman
|
|
|
|
|
SuspendThread() will stop the thread at an arbitrary location in the thread's code.
When you call ResumeThread(), execution will resume at the location the thread was
suspended. The thread procedure is NOT a function that gets re-called.
Because of this, SuspendThread() shouldn't be used like this - unless you can figure
out a way to completely control the execution location in the thread.
You really should be doing this another way - communicating with the thread in some
fashion to ensure it's in the state you expect when you pause and restart it.
Also, if you're using any MFC stuff in your thread, you should be using AfxBeginThread()
instead of the CreateThread() API.
If you are not using MFC but you're using any CRT functions in your thread you should be
using _beginthread[ex]() instead of CreateThread().
Mark
*edited for grammar/spleling*
Last modified: 22mins after originally posted --
Mark Salsbery
Microsoft MVP - Visual C++
|
|
|
|
|
Mark is correct; you shouldn't use SuspendThread /ResumeThread in this way!
The following quote is from MSDN on SuspendThread (the underlines are mine):
This function is primarily designed for use by debuggers. It is not intended to be used for thread synchronization. Calling SuspendThread on a thread that owns a synchronization object, such as a mutex or critical section, can lead to a deadlock if the calling thread tries to obtain a synchronization object owned by a suspended thread. To avoid this situation, a thread within an application that is not a debugger should signal the other thread to suspend itself. The target thread must be designed to watch for this signal and respond appropriately.
The following link may also be of interest:
http://blogs.msdn.com/oldnewthing/archive/2003/12/09/55988.aspx[^]
I suggest you review the documentation more carefully in future.
Steve
|
|
|
|
|
Thank you very much for help from everybody!!
Sure, I will read them and redesign the application.
Best Regards,
Suman
|
|
|
|
|
Hi all. I made some code that i just cant seem to figure out why its not working like intended.
For some strange reason it was working the way i had hoped until i did a final test and it just seemed to do what it wanted, this may sound strange believe me because i think its strange myself, seeing how it worked one minute and then another it doesnt.
I dont need to post the entire code but just the block thats giving me trouble.
<br />
if(connect(dataSock, (sockaddr*)&structSock, sizeof(sockaddr)) == -1){<br />
cout << "Connecting to backup server1: " << backupservers[0] << endl;<br />
ServerAddr1 = gethostbyname(backupservers[0]);<br />
structSock1.sin_family = AF_INET; <br />
structSock1.sin_port = htons(port); <br />
structSock1.sin_addr.s_addr = *((unsigned long*)ServerAddr1->h_addr_list[0]); <br />
memset(structSock.sin_zero, 0, 8); <br />
connect(dataSock, (sockaddr*)&structSock1, sizeof(sockaddr));<br />
}<br />
if(connect(dataSock, (sockaddr*)&structSock1, sizeof(sockaddr)) == -1){<br />
cout << "Connecting to backup server2: " << backupservers[1] << endl;<br />
ServerAddr1 = gethostbyname(backupservers[1]);<br />
structSock1.sin_family = AF_INET; <br />
structSock1.sin_port = htons(port); <br />
structSock1.sin_addr.s_addr = *((unsigned long*)ServerAddr1->h_addr_list[0]); <br />
memset(structSock.sin_zero, 0, 8); <br />
connect(dataSock, (sockaddr*)&structSock2, sizeof(sockaddr));<br />
}<br />
The idea here is that if the first connection doesnt work try another, and if that doesnt work try another. The first if function works fine. But the second executes the same time the first if block, which isnt what i want. I want it to wait until the if standards have been met, ie: no connection (-1). But its executing when its not wanted. This is really strange considering i took a short break to come back for one last test just to find out that it wasnt working. Any suggestions? Thanx in advance!
|
|
|
|
|
Assuming all your addresses are correct...
What happens if the second connect() call (of 4 you show)
succeeds? Then you try to connect again(?).
Take a look at your logic - why check for error on connect in some calls
and not on others?
Mark
Mark Salsbery
Microsoft MVP - Visual C++
|
|
|
|
|
Ok so picture a program trying to connect to a Domain
1: DNS
2: Connect
But somethings wrong with the site/domain/server. All it can do is DNS the domain and retrieve a IP address but cannot connect to it.
My code does just that. DNS, Connect. But if it cant connect the first server, switch to backup server1, if backupserver1 DNS's properly, but cannot connect, then switch to backup server2. Does this make sense?
|
|
|
|
|
Makes sense but the code shown has logic like this:
connect
If failed
connect
connect <font color="Red"><-- shouldn't connect here if previous connect succeeded</font>
If failed
connect when it should be
connect
if failed
connect
if failed
connect
if failed
connect
That's what I was referring to
Mark
Mark Salsbery
Microsoft MVP - Visual C++
|
|
|
|
|
Yes! Thats exactly what i was refering to. And thats how it was. I maybe typed an extra character when i shouldnt have. Any suggestions?
|
|
|
|
|
The key point I was trying to make is that you need to check for success/fail on
all connect() calls.
You can code it any way you want. The simplest, least elegant way is nested "if"s
if (SOCKET_ERROR == connect(..))
{
... try next address
if (SOCKET_ERROR == connect(..))
{
... try next address
if (SOCKET_ERROR == connect(..))
{
... try next address
if (SOCKET_ERROR == connect(..))
{
// all four connect() attempts failed!
}
}
}
}
You can get as fancy as you want.
For example, a more elegant solution would be a configurable
list of addresses/server names that you loop through until
you get a successful connection.
Mark
Mark Salsbery
Microsoft MVP - Visual C++
|
|
|
|
|
Do you have a blocking or non-blocking socket?
"A good athlete is the result of a good and worthy opponent." - David Crow
"To have a respect for ourselves guides our morals; to have deference for others governs our manners." - Laurence Sterne
|
|
|
|
|
No Mark Salsbery nested if's wont work either. They all seem to execute as long as connect() is in there.
DavidCrow, no i dont believe i have a blocking socket. This is really getting under my skin because i've been working on it too long. Any more ideas? If i put the if in a for loop then it would loop, maybe once, but i dont think it would still accomplish the goal. I'll test it to see.
|
|
|
|
|
dellthinker wrote: DavidCrow, no i dont believe i have a blocking socket.
So did you read the documentation on how connect() behaves with non-blocking sockets?
"A good athlete is the result of a good and worthy opponent." - David Crow
"To have a respect for ourselves guides our morals; to have deference for others governs our manners." - Laurence Sterne
|
|
|
|
|
I take that back, the nested if's worked. Thanks. Sorry for the mistake
|
|
|
|
|
I am providing CD/DVD burning option for one of my applications. I have purchased Magic CD/DVD burning component. Using this component, I am able to write the data onto CD/DVD, but I am not able to write files with large filename onto the disc.
Is there anyone who has used that component earlier. Can anyone give me any suggestion so that I can write the files with large filename onto CD-DVD.
Regards,
Vijay.
God may not give us what we 'want', but he surely gives us what we 'need'.
|
|
|
|
|
vijay7173 wrote: Can anyone give me any suggestion so that I can write the files with large filename onto CD-DVD.
Assuming you're burning in ISO-9660, I would suggest either level 2 or Joliet extensions or both. Level 1 and/or no Joliet extensions will mean you can only support 8.3 (DOS style) filenames. I'd like to think this library would give you the option to choose the file system extensions to use, so I'd say check their docs for it.
|
|
|
|
|
Jeremy Falcon wrote: I'd like to think this library would give you the option to choose the file system extensions to use, so I'd say check their docs for it.
yes, this is exactly what i am searching for at this moment. But there is no enough documentation given on this by them. I am trying to ask them but most of the links on their site is down. Anyways, I have sent a mail to their support team and hoping to get a reply soon. In the meanwhile, I wanted to know if any of the CPians here have used that component and wanted to know if they can help in anyway.
well, thanks for the reply, sir. Hope I can get more help here.
Regards,
Vijay.
God may not give us what we 'want', but he surely gives us what we 'need'.
|
|
|
|