Click to See Complete Forum and Search --> : Mixing WINAPI and C++?


volt9000
September 17th, 2006, 01:46 PM
I was wondering if it's possible to use C++ code in WINAPI (C) code. I've gotten the hang of programming in the C-language WINAPI but I'd like to incorporate the benefits of C++ (objects and inheritance) into my code without using MFC. (Not to mention the fact that I don't have Visual Studio, I'm using Dev-CPP.)
I've tried a small test by writing a basic WINAPI application (just display a window) and writing a very simple class. But this is not working out very well. I know I'm supposed to specify that the class file is compiled as C++, but then my WndProc file is complaining it doesn't understand the class file. If I set THAT to compile as a CPP file, I get yet other errors.
Is there a way to do this without radically restructuring a WINAPI program?

Calculator
September 17th, 2006, 01:57 PM
Show us how you set it up. This is a really good tutorial about WinAPI - C++ programming.

http://www.relisoft.com/win32/index.htm

volt9000
September 17th, 2006, 02:20 PM
Ok, well I have a template set up which I use as a base for all my Windows applications. I have attached it here, along with the Dev-CPP project file, as a RAR.

This works just fine if compiled as a C project. However, when compiled as a C++ project, I get several compile-time errors, as follows:


multiple definition of `hWndMain'
first defined here
multiple definition of `WndMainClassName'
first defined here
multiple definition of `WndMainTitle'
first defined here
multiple definition of `hWndMain'
first defined here


Of course, Dev-CPP doesn't provide any line numbers with these errors, it just complains about them.
What am I doing wrong?

Calculator
September 17th, 2006, 02:26 PM
You should not define variables in headers, you should declare them. Like

extern HWND hwndMain;

There shouldn't really be a need for such shananigans though. Work on modularity, those tutorials are highly recommended.

gecka
September 17th, 2006, 02:29 PM
This compiles fine for me, when I compile it as c++ file.

volt9000
September 17th, 2006, 02:31 PM
I have seen this "extern" keyword used many times elsewhere, but what does it actually mean/do? And how do I know when to use it?

This compiles fine for me, when I compile it as c++ file.
What compiler are you using? I tried compiling it as a C++ project and I get those aforementioned errors.

gecka
September 17th, 2006, 02:49 PM
Sorry, now I reproduced it. I quickly threw this together (sorry for the mess I made), it compiles for me.

Calculator
September 17th, 2006, 07:36 PM
You've got definitions in your header file (winmain.hpp, wndmain.hpp) gecka

This is for the extern linkage specifier is used: http://msdn2.microsoft.com/en-us/library/0603949d.aspx

volt9000
September 17th, 2006, 08:01 PM
Ok, after reading through that Relisoft link, I change the structure of my program. I have again attached the source files.

This time, I'm getting the following errors:

wndmain.hpp, line 6: expected init-declarator before "WndMainProc"
wndmain.hpp, line 6: expected `,' or `;' before "WndMainProc"
main.cpp, line 9: `WndMainProc' undeclared (first use this function)


So apparently, the compiler doesn't like the window callback function declaration in wndmain.hpp, but why? This is similar to how I've seen it done elsewhere.

Calculator
September 17th, 2006, 08:04 PM
class WinClass
{
public:
WinClass(HINSTANCE hInstance, const char *ClassName, const char *Name, WNDPROC WndProc);
BOOL Register();
HWND MakeWindow(int xSize, int ySize, int nCmdShow);
private:
WNDCLASSEX _wcx;
const char *_name;
}

class WinClass
{
public:
WinClass(HINSTANCE hInstance, const char *ClassName, const char *Name, WNDPROC WndProc);
BOOL Register();
HWND MakeWindow(int xSize, int ySize, int nCmdShow);
private:
WNDCLASSEX _wcx;
const char *_name;
};

Yup.

volt9000
September 17th, 2006, 08:17 PM
OH CRIPES. I can't believe I missed that semicolon.
head->BrickWall();

Well thanks a bunch! I fixed a few other minor errors, and now it compiles!

volt9000
September 17th, 2006, 10:42 PM
Although, I would still like to know why the original code, the C code, won't compile as CPP.

greg_dolley
September 18th, 2006, 05:36 AM
volt9000,

For my own programs I use C++ and the C Win API. It's totally possible to mix them together. I've made my own classes as wrappers around event handling, common function calls, etc. So, yes, if I can do it, you can do it too.

Greg Dolley

greg_dolley
September 18th, 2006, 05:49 AM
I have seen this "extern" keyword used many times elsewhere, but what does it actually mean/do? And how do I know when to use it?


When you declare a variable as "extern" it means that the variable exists in a different source file that is not in scope with the source file declaring the extern.

For example, say you have a source file a.cpp which looks like:


int some_global = 1;

void function_a(void)
{
}


And you have source file b.cpp which looks like:


extern int some_global;

void function_b(void)
{
some_global = 2;
}


If you compile a.cpp and b.cpp together you will get no errors. But if you removed the line with "extern" in source file b.cpp, the compiler will give an error saying that some_global is not declared. Global variables in a.cpp are not seen in b.cpp, so that's why you need to use extern - to let the compiler know that some variable will be declared somewhere later in the compilation process.

Greg Dolley