A Conversation for The H2G2 Programmers' Corner
API and C++
Annette Started conversation Jan 26, 2003
For the last few weeks, I've been trying to learn to use the Windows API, and I was wondering if there was an easy way to use it in my C programs, for graphics and such. My problem is, Visual C++ (which is what I use) requires that programming with the API be done in a "Win32 application"--otherwise you get an "unresolved external main" error message. The Win32 application, as far as I can tell, ignores all C commands. I've been fiddling with it for a while, and nothing seems to work.
So, um, any help would be appreciated!
Annette
API and C++
Spelugx the Beige, Wizard, Perl, Thaumatologically Challenged Posted Jan 27, 2003
The 'unresolved external main' message is caused by the fact that in a normal program, you aren't linking with the right libraries for GUI programming, the advantage of a Win32 Application is that it already has all the right libraries set up for you.
> The Win32 application, as far as I can tell, ignores all C commands.
This sounds interesting, since it shouldn't be 'ignorning all C commands'.
spelugx -- used to use MSVC
API and C++
Ion the Naysayer Posted Jan 27, 2003
Maybe this is a stupid question but are you including all the correct header files for C?
#include ;
etc.?
API and C++
Spelugx the Beige, Wizard, Perl, Thaumatologically Challenged Posted Jan 27, 2003
If you're doing gui programming you shouldn't really need . (C isn't defined by the header files you know, I often program in an environment with none of the standard headers)
spelugx -- operating systems programmer
API and C++
MaW Posted Jan 27, 2003
On the other hand, you may have the right header files but not be linking with the libraries that go with them...
API and C++
Annette Posted Jan 27, 2003
My main problem is, I'm not sure exactly where to put C commands in my program. Say (to take a very simple example) I want to use "printf" to display a message on the screen, then open a window with the API. I've tried 1)Creating a main function as usual, and putting it either before or after WinMain 2)Calling printf from WinMain or WinProc. This compiles and links just fine, but then runs as if the call to printf weren't there. I remembered to include stdio.h and all that, so I don't think that's the problem, though it could be.
The other thing I can think to try is to call WinMain or WinPRoc from main(). However, I have only a vague idea as to what the parameters (hwnd, lParam and all that) do and no idea as to there actual values, so I'm not quite sure how to do this.
API and C++
DoctorMO (Keeper of the Computer, Guru, Community Artist) Posted Jan 28, 2003
it's all much easyer in Asm... or wait, no it's easyer in C, only because you pass the arguments, putting things on the stack takes mental apt. Oh well. C is on my todo list of things I could do with getting much better at.
-- DoctorMO --
API and C++
MaW Posted Jan 28, 2003
Annette - putting a call to printf in a Windows application won't do much good - where would the textual output go?
API and C++
Dancer (put your advert here) Posted Jan 28, 2003
Youre just complicating it abit.
The difference in the projects ("Win32 Application", "Win32 Console Application" ...) is only the files you start with, I suggest you create a win32 console application (if youre using printf, I'm guessing youre trying to create a console application) and look what youre missing in your other project in terms of libreries or headers or so.
Dancer
API and C++
Dogster Posted Jan 29, 2003
There is a difference between win32 app and win32 console app which isn't to do with the files you start with. That is, in a win32 app the linker specifies /subsystem:windows and in a win32 console app it is /subsystem:console. In the former the entry point is a WinMain function and standard i/o are ignored (in particular, printf will do nothing). In the latter, the entry point is main and a console window is opened and standard i/o are to and from this window.
The problem is, although I know that it is possible to find out (from a console app) what your app's hInstance and hPrevInstance are (which, IIRC, are needed for opening windows and so forth), I don't know how to do it. Normally these are passed to the app as parameters to WinMain, but a console app is called from a main function which doesn't include those parameters. If you can get these two values, then you can have console i/o and use the windows functions.
Alternatively, there are windows API functions to open console windows. Look up the AllocConsole function. A console opened like this has the standard i/o associated with it, so printf, scanf, etc. should work as normal. In other words, you do something like:
int PASCAL WinMain(...)
{
...
AllocConsole();
printf("Hello World.\n");
...
FreeConsole();
...
}
Key: Complain about this post
API and C++
- 1: Annette (Jan 26, 2003)
- 2: Spelugx the Beige, Wizard, Perl, Thaumatologically Challenged (Jan 27, 2003)
- 3: Ion the Naysayer (Jan 27, 2003)
- 4: Spelugx the Beige, Wizard, Perl, Thaumatologically Challenged (Jan 27, 2003)
- 5: MaW (Jan 27, 2003)
- 6: Annette (Jan 27, 2003)
- 7: DoctorMO (Keeper of the Computer, Guru, Community Artist) (Jan 28, 2003)
- 8: MaW (Jan 28, 2003)
- 9: Dancer (put your advert here) (Jan 28, 2003)
- 10: Dogster (Jan 29, 2003)
More Conversations for The H2G2 Programmers' Corner
Write an Entry
"The Hitchhiker's Guide to the Galaxy is a wholly remarkable book. It has been compiled and recompiled many times and under many different editorships. It contains contributions from countless numbers of travellers and researchers."