Here is a simple example of calling a decorated C++ function from MASM.  The problem with C++ functions is that they have a complicated decoration scheme. An easy way to solve the problem is to look at the assembly listing produced by the C++ compiler. Of course, you can use a tool like dumpbin to view the decorated symbols :

\masm32\bin\dumpbin /SYMBOLS sum.obj


Microsoft (R) COFF Binary File Dumper Version 5.12.8078
Copyright (C) Microsoft Corp 1992-1998. All rights reserved.


Dump of file sum.obj

File Type: COFF OBJECT

.
.
008 00000000 SECT3  notype ()    External    | ?sum@@YGHHH@Z (int __stdcall sum(int,int))
.
.

The example project :

.386
.model flat, stdcall
option casemap :none

include    \masm32\include\windows.inc
include    \masm32\include\kernel32.inc
include    \masm32\include\user32.inc
include    \masm32\include\masm32.inc

includelib  \masm32\lib\kernel32.lib
includelib  \masm32\lib\user32.lib
includelib  \masm32\lib\masm32.lib

?sum@@YGHHH@Z PROTO SYSCALL

sum EQU <pr2 PTR ?sum@@YGHHH@Z>


.data
format1 db  '10 + 20 = %d',0
x      dd  10
y      dd  20

.data?
buffer  db  100 dup(?)

.code

start:

    invoke  sum,x,y
    invoke  wsprintf,ADDR buffer,ADDR format1,eax
    invoke  StdOut,ADDR buffer
    invoke  ExitProcess,0

END start


Sum.cpp ( C++ source file compiled with VC++ Express 2005 ) :


int __stdcall sum(int x , int y)
{
  return(x+y);
}


Building the project :

cl /c /Oty2 /Zl /Gs /GS- /FoSum.obj /Fa Sum.cpp
\masm32\bin\ml /c /coff Sumtest.asm
\masm32\bin\polink /SUBSYSTEM:CONSOLE Sumtest.obj Sum.obj
Attachments:
Posted on 2006-08-26 05:49:02 by Vortex
Normally you'd use
extern "C" { /* stuff here */ }
if you want C++ code to be linkable with other languages - that way you get rid of the complex name decoration.
Posted on 2006-08-26 12:27:40 by f0dder
yeah, but that method is good to know if you wanna call class member functions
or write class member functions in ASM, because you cant extern __thiscall funcs.
i dont want to argue, i know that you can do better, but it was nice when i figured
that i can make member functions in assembly :)
Attachments:
Posted on 2006-08-26 14:19:00 by drizz
Hi drizz,

Thanks for the example. It's a good demonstation on how to use decorated names in Masm.
Posted on 2006-08-26 16:19:52 by Vortex


Here is a simple example of calling a decorated C++ function from MASM.  The problem with C++ functions is that they have a complicated decoration scheme. An easy way to solve the problem is to look at the assembly listing produced by the C++ compiler. Of course, you can use a tool like dumpbin to view the decorated symbols


Even better, for the singol symbol, use UNDNAME.EXE (or maybe if you want to undecorate a symbol at a time via a script).
Posted on 2006-08-27 01:41:47 by Maverick
Hi Fabio,

Nice to see you here. It has been a long time, how are you?

Thanks for your message, I will have a look at UNDNAME.EXE
Posted on 2006-08-27 03:32:41 by Vortex

Hi Vortex  :)

Thanks dude, I'm fine. :) Been around here for some weeks, it felt natural to make a post after I had just used UNDNAME.EXE some minutes before reading your post, by coincidence. :shock:

Mine (UNDNAME) is from the VC2005 bin directory, by the way. I don't know if it's in the MASM 6.xx package, being it so much C++ related.

I can attach it if you/any_other want/s, it's from the free VC2005 Express edition (no limitations here apply, though).

PS: in my previous post, with "singol" I really meant "single".. my Maccheroni English stroke back.  :lol:

Take care,
FabI/O

Posted on 2006-08-27 03:43:08 by Maverick
I am glad to get your news, you should visit the board more frequently.

I have VC2005 Express installed on my system, UNDNAME works fine :

C:\Program Files\VCExpress\VC\bin>undname ?sum@@YGHHH@Z
Microsoft (R) C++ Name Undecorator
Copyright (C) Microsoft Corporation. All rights reserved.

Undecoration of :- "?sum@@YGHHH@Z"
is :- "int __stdcall sum(int,int)"


Take care Fabio,

Vortex
Posted on 2006-08-27 03:48:26 by Vortex
UnDecorateSymbolName() - interesting, it exists since win95, in imagehlp.lib.
The dumpbin approach is new to me, thanks - I know I'll need to mix compiled C++ classes with my asm classes sooner or later :).
Posted on 2006-08-28 00:08:43 by Ultrano