[C] Program-launching program

  • marcootje
  • Newbie
  • Newbie
  • marcootje
  • Posts: 5

Post 3+ Months Ago

Hi all,

This week I was trying to make a program which allows you to choose a particular set of programs to launch. A modified form of programs launching at start-up you could say. (For windows.)
I got stuck on a particular feature, namely a default set of programs if you do not choose anything within 10 seconds. (Sort of the same idea as a dual-boot screen.) After searching somewhat on the internet, I came to the conclusion I will not get it to work very easily.

I've got two questions about this, if the first question is answered false, we'll go on with the second.

First, this is the idea of my program:
int main calls a function wait_thread(), which eventually calls scanf() after some initialisation stuff. The function wait_thread() is by the way called in a new thread:
Code: [ Select ]
HANDLE hThread = CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE) wait_thread, NULL, 0, NULL);

Then, in the main thread, I wait for input or the 10 seconds to pass:
Code: [ Select ]
WaitForSingleObject(hThread, secs);

Where secs = 10000.
Two situations can occur:
1) Some input is given in scanf() and the program starts some programs, and eventually quits.
2) The 10 seconds have passed and the default set is started. Here the problems start to occur, because there is no normal way to shut down the created thread. If I do not do anything with the scanf()-thread, the scanf() still wants the input, and the thread doesn't quit until this input is given. When input is given, the program quits. When I call terminateThread():
Code: [ Select ]
TerminateThread(hThread, 0);

The program does not do anything any more after the last printf(). This printf(), by the way, is called after the terminateThread. The program sort of freezes.

Can anyone help me out of this?

If not, I got a second question, with which I do not need the scanf(), I think. I thought it might be a good idea to make something like the dual-boot screen, where you have to select some OS, and then press enter. You can navigate with your arrow keys, etc. Can something like this be made in C, and if so, what functions would I need?

Thanks in advance,
Marco

Oh by the way, the source is attached to this post. This is my first real program in C, so it can be sort of really badly written :P . Sorry for the lot of commented code, it is just for testing, etc. If you come across something that looks like Dutch, that can be possible indeed ;) .
  • Anonymous
  • Bot
  • No Avatar
  • Posts: ?
  • Loc: Ozzuland
  • Status: Online

Post 3+ Months Ago

  • casablanca
  • Proficient
  • Proficient
  • User avatar
  • Posts: 481

Post 3+ Months Ago

Combining stdio functions with the Win32 API can be tricky and as in your case, cause problems. Try to eliminate scanf() and instead use the Win32 console functions (GetStdHandle, ReadConsole) to read user input.
  • marcootje
  • Newbie
  • Newbie
  • marcootje
  • Posts: 5

Post 3+ Months Ago

Aren't those functions C++-only? I can't get this to work:
Code: [ Select ]
HANDLE consoleHandle = getStdHandle(STD_INPUT_HANDLE);
LPDWORD lpdword_out = NULL;
ReadConsole(consoleHandle, a, 1, lpdword_out, NULL);
  1. HANDLE consoleHandle = getStdHandle(STD_INPUT_HANDLE);
  2. LPDWORD lpdword_out = NULL;
  3. ReadConsole(consoleHandle, a, 1, lpdword_out, NULL);


as a replacement of:
Code: [ Select ]
scanf("%s", a);


I do include windows.h:
Code: [ Select ]
#include <windows.h>


But I still get these errors:
Quote:
In function `manualChoosing':
(line 216) [Warning] initialization makes pointer from integer without a cast
(line 216) [Linker error] undefined reference to `getStdHandle'
(line 216) ld returned 1 exit status
[Build Error] [startUp.exe] Error 1


line 216:
Code: [ Select ]
HANDLE consoleHandle = getStdHandle(STD_INPUT_HANDLE);


Msdn (why am I nog allowed to post external links? :? ) says it is a C++ function, and I am compiling it as C. Can this cause the problem, or is it something else?

EDIT:
I came to the conclusion getStdHandle had to be GetStdHandle. It still does not work perfectly, but I'm going to look into it first for myself.
  • marcootje
  • Newbie
  • Newbie
  • marcootje
  • Posts: 5

Post 3+ Months Ago

After some difficulties I've made it to a working program :) .
I still need to optimize some things, but the basis now works.

Thanks a lot!
  • casablanca
  • Proficient
  • Proficient
  • User avatar
  • Posts: 481

Post 3+ Months Ago

Glad you got it working. The Win32 API is purely C, nothing is C++-only. (of course, wrapper libraries like MFC are built using C++, but there's nothing you can't do using just the C API)

Post Information

  • Total Posts in this topic: 5 posts
  • Users browsing this forum: No registered users and 63 guests
  • You cannot post new topics in this forum
  • You cannot reply to topics in this forum
  • You cannot edit your posts in this forum
  • You cannot delete your posts in this forum
  • You cannot post attachments in this forum
 
 

© 1998-2014. Ozzu® is a registered trademark of Unmelted, LLC.