[comp.windows.ms.programmer] Solution to BC++ Petzold problem, and mild bug report

rich@wiley.uucp (Rich Messenger) (05/09/91)

I recently posted a question about wierd behavior trying to get
Petzold's first sample program from _Programming Windows_ to run under
BC++.  Although there were no reposnses from this news group, I
eventually tracked down the problem and thought I might be of general
interest to other BC++ users.

(1) When you are compiling as C++, you *cannot* use the EXPORTS section
in the .DEF file, since the name is mangled by C++ before the linker
ever sees it.  This tidbit is not mentioned in the documentation.

(2) You can use "smart callbacks" and thigs work great as long as
you're not writing a DLL or using the huge memory model.

(3) You can tag exportable functions with "_export".  You *must* put
this keyword in both the function declaration and function definition.
MILD BUG: BC++ does not complain if you declare the function without
_export, and define it with _export; it simply exhibits wierd behavior
on execution.

Example of bad code which BC++ does not complain about but which
causes erroneous execution:

	long FAR PASCAL WndProc(HWND, WORD, WORD, LONG);

	...definition of WinMain()...

	long FAR PASCAL _export WndProc(HWND hwnd, ...

Hope this helps the next guy who naively tries to type in the Petzold
examples straight from the book but uses C++ instead of C.
     _ __
    ' )  )      /                      ... Rich Messenger
     /--' o _. /_                          rich@wilbur.coyote.trw.com
    /  \_(_(__/ /_                         {uunet,cit-vax,trwrb}!wiley!rich

cjones@isis.cs.du.edu (Charles J. Jones) (05/09/91)

In article <2828D392.40DD@wilbur.coyote.trw.com> rich@wiley.uucp (Rich Messenger) writes:
>(1) When you are compiling as C++, you *cannot* use the EXPORTS section
>in the .DEF file, since the name is mangled by C++ before the linker
>ever sees it.  This tidbit is not mentioned in the documentation.

Sure you can.  But you have to change the function definition re:

long FAR PASCAL WndProc(HWND hwnd ...

becomes

extern "C" long FAR PASCAL WndProc(HWND hwnd ...

the same as always in C++ when you want ot prohibit the compiler
from mangling the name.

Charles

--
---------------------------------------------------------------------------
Charles J. Jones        | If builders built buildings the way programmers
cjones@nyx.cs.du.edu    | write programs, then the first woodpecker that
cjones@copper.denver.colorado.edu  | came along would destroy civilization.

butch@geohub.gcn.uoknor.edu (H. G. "Butch" Walker Jr.) (05/10/91)

In article <2828D392.40DD@wilbur.coyote.trw.com>, rich@wiley.uucp (Rich
Messenger) writes:

|>I recently posted a question about wierd behavior trying to get
|>Petzold's first sample program from _Programming Windows_ to run under
|>BC++.  Although there were no reposnses from this news group, I
|>eventually tracked down the problem and thought I might be of general
|>interest to other BC++ users.
|>
|>(1) When you are compiling as C++, you *cannot* use the EXPORTS section
|>in the .DEF file, since the name is mangled by C++ before the linker
|>ever sees it.  This tidbit is not mentioned in the documentation.
|>
|>(2) You can use "smart callbacks" and thigs work great as long as
|>you're not writing a DLL or using the huge memory model.
|>
|>(3) You can tag exportable functions with "_export".  You *must* put
|>this keyword in both the function declaration and function definition.
|>MILD BUG: BC++ does not complain if you declare the function without
|>_export, and define it with _export; it simply exhibits wierd behavior
|>on execution.
|>

Would someone please explain this to me.  I recently copied the SysMets 
program from the book and it worked just fine.  I didn't use the _export
tag but simply used the EXPORTS section of the DEF.  It seemed to work
fine.  Was it really messed up but I just didn't notice?  
Response by e-mail is fine.

Thanks in advance,

Butch

rich@wiley.uucp (Rich Messenger) (05/10/91)

In article <1991May9.151819.3190@mnemosyne.cs.du.edu> cjones@isis.UUCP (Charles J. Jones) writes:
>In article <2828D392.40DD@wilbur.coyote.trw.com> rich@wiley.uucp (Rich Messenger) writes:
>>(1) When you are compiling as C++, you *cannot* use the EXPORTS section
>>in the .DEF file, since the name is mangled by C++ before the linker
>>ever sees it.  This tidbit is not mentioned in the documentation.
>
>Sure you can.  But you have to change the function definition re:
>
>long FAR PASCAL WndProc(HWND hwnd ...
>
>becomes
>
>extern "C" long FAR PASCAL WndProc(HWND hwnd ...
>
>the same as always in C++ when you want ot prohibit the compiler
>from mangling the name.
>
>Charles

Correct.  The purpose of my posting was to warn the unwary that some
special care must be taken when using C++, even though Borland does
not alert you to the fact.  If you are aware of the name mangling
problem, you can choose several "fixes" which all work.  Thank you for
adding another alternative to those I mentioned.
     _ __
    ' )  )      /                      ... Rich Messenger
     /--' o _. /_                          rich@wilbur.coyote.trw.com
    /  \_(_(__/ /_                         {uunet,cit-vax,trwrb}!wiley!rich

poffen@sj.ate.slb.com (Russ Poffenberger) (05/14/91)

In article <1991May9.160511@geohub.gcn.uoknor.edu> butch@geopix.gcn.uoknor.edu writes:
>In article <2828D392.40DD@wilbur.coyote.trw.com>, rich@wiley.uucp (Rich
>Messenger) writes:
>
>|>I recently posted a question about wierd behavior trying to get
>|>Petzold's first sample program from _Programming Windows_ to run under
>|>BC++.  Although there were no reposnses from this news group, I
>|>eventually tracked down the problem and thought I might be of general
>|>interest to other BC++ users.
>|>
>|>(1) When you are compiling as C++, you *cannot* use the EXPORTS section
>|>in the .DEF file, since the name is mangled by C++ before the linker
>|>ever sees it.  This tidbit is not mentioned in the documentation.
>|>
>|>(2) You can use "smart callbacks" and thigs work great as long as
>|>you're not writing a DLL or using the huge memory model.
>|>
>|>(3) You can tag exportable functions with "_export".  You *must* put
>|>this keyword in both the function declaration and function definition.
>|>MILD BUG: BC++ does not complain if you declare the function without
>|>_export, and define it with _export; it simply exhibits wierd behavior
>|>on execution.
>|>
>
>Would someone please explain this to me.  I recently copied the SysMets 
>program from the book and it worked just fine.  I didn't use the _export
>tag but simply used the EXPORTS section of the DEF.  It seemed to work
>fine.  Was it really messed up but I just didn't notice?  
>Response by e-mail is fine.
>

What was the name of the file? sysmets.c? or sysmets.cpp? If the file has a
".c" extension, then it is compiled as straight "C" code, and the name mangling
problem doesn't appear.

Russ Poffenberger               DOMAIN: poffen@sj.ate.slb.com
Schlumberger Technologies       UUCP:   {uunet,decwrl,amdahl}!sjsca4!poffen
1601 Technology Drive		CIS:	72401,276
San Jose, Ca. 95110             (408)437-5254