A Conversation for The H2G2 Programmers' Corner

API and C++

Post 1

Annette

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++

Post 2

Spelugx the Beige, Wizard, Perl, Thaumatologically Challenged

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++

Post 3

Ion the Naysayer

Maybe this is a stupid question but are you including all the correct header files for C?

#include ;

etc.?


API and C++

Post 4

Spelugx the Beige, Wizard, Perl, Thaumatologically Challenged

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++

Post 5

MaW

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++

Post 6

Annette

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++

Post 7

DoctorMO (Keeper of the Computer, Guru, Community Artist)

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++

Post 8

MaW

Annette - putting a call to printf in a Windows application won't do much good - where would the textual output go?


API and C++

Post 9

Dancer (put your advert here)

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.

smiley - hsif
Dancer


API and C++

Post 10

Dogster

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