flat assembler
Message board for the users of flat assembler.

Index > High Level Languages > MS COFF asm calling C++ library

Author
Thread Post new topic Reply to topic
Bao Tran



Joined: 24 Jan 2010
Posts: 2
Bao Tran 29 Jan 2010, 09:00
Hi All,
I am very new to assembler (I took a class in college ~7 yrs ago, but just now getting back into it), so please be patient with me. So far, I've run through most of Iczelion's tutorials (sulaiman's version for fasm), but only they cover more PE stuff and not much COFF stuff. I'm currently trying to integrate a library written in c++ into my asm code, and I was able to convert my PE project into a MS COFF format for linking purposes, but now I'm at a loss on how to call c++ classes from ASM.

I've tried running implib/dll2def on the c++ dll to create a def file, but the name mangling reported by it is abysmal (VC9 mangled names to ??0daeElement@@QAE@AAVDAE@@@Z - not even sure if it's even callable with the ?? in front). Honestly, I'd rather not call them this way since it defeats c++ encapsulation, so I have 2 part questions:

1) How do i include and instantiate a c++ class from asm? is it the same way as c (provide an import address and call using invoke/call)? since HLL creates the concept of encapsulation, I assume I have to "emulate" it in asm by accessing only "public" members/methods?

2) I eventually want to make it a static library and incorporate it into my exe, so is there anything special I should do besides just including the obj files created by VC9 compiler during linking? Would the method of calling change (names could be mangled differently static vs dynamic)?

I've scoured this (and asmcommunity's) board for days now, and all I've come up with are examples of HLL programs calling asm, not vice versa. That, and a small hints here and there of using extern 'c' and __declspec(dllexport) for c++... any help from you gurus would be much appreciated!
Post 29 Jan 2010, 09:00
View user's profile Send private message Reply with quote
revolution
When all else fails, read the source


Joined: 24 Aug 2004
Posts: 20415
Location: In your JS exploiting you and your system
revolution 29 Jan 2010, 09:04
Code:
extrn 'LibraryFunctionName' as MyInternalName
ccall MyInternalName,p1,p2    
Post 29 Jan 2010, 09:04
View user's profile Send private message Visit poster's website Reply with quote
f0dder



Joined: 19 Feb 2004
Posts: 3175
Location: Denmark
f0dder 29 Jan 2010, 09:24
1) you don't Smile - you'll save yourself a lot of headaches if you restrict yourself to C/C++ calling assembly routines, and not the other way around.
Post 29 Jan 2010, 09:24
View user's profile Send private message Visit poster's website Reply with quote
Japheth



Joined: 26 Oct 2004
Posts: 151
Japheth 30 Jan 2010, 19:59
f0dder wrote:
1) you don't Smile - you'll save yourself a lot of headaches if you restrict yourself to C/C++ calling assembly routines, and not the other way around.


A lot of my assembly programs call CRT functions. 'Headaches' are to be expected only if you don't know what you're doing.
Post 30 Jan 2010, 19:59
View user's profile Send private message Reply with quote
f0dder



Joined: 19 Feb 2004
Posts: 3175
Location: Denmark
f0dder 31 Jan 2010, 02:22
Japheth wrote:
f0dder wrote:
1) you don't Smile - you'll save yourself a lot of headaches if you restrict yourself to C/C++ calling assembly routines, and not the other way around.


A lot of my assembly programs call CRT functions. 'Headaches' are to be expected only if you don't know what you're doing.
CRT, yes, but how often has that been C++? There's quite a difference between C and C++...

_________________
Image - carpe noctem
Post 31 Jan 2010, 02:22
View user's profile Send private message Visit poster's website Reply with quote
Japheth



Joined: 26 Oct 2004
Posts: 151
Japheth 31 Jan 2010, 07:42
f0dder wrote:
CRT, yes, but how often has that been C++? There's quite a difference between C and C++...


"Not that often" of course - there's not much to gain. But you explicitely talked about C++ and C ...
Post 31 Jan 2010, 07:42
View user's profile Send private message Reply with quote
f0dder



Joined: 19 Feb 2004
Posts: 3175
Location: Denmark
f0dder 31 Jan 2010, 17:12
Japheth wrote:
f0dder wrote:
CRT, yes, but how often has that been C++? There's quite a difference between C and C++...

"Not that often" of course - there's not much to gain. But you explicitely talked about C++ and C ...

True, true - the OP was pretty clearly about C++, though Smile

For C++ code it's - IMHO - just too much bother to even consider. Name mangling (which differs between compilers, and iirc even different compiler versions as well), memory layout, class instatiation, etc. Much easier to call assembly routines from C++.

_________________
Image - carpe noctem
Post 31 Jan 2010, 17:12
View user's profile Send private message Visit poster's website Reply with quote
Bao Tran



Joined: 24 Jan 2010
Posts: 2
Bao Tran 01 Feb 2010, 17:57
Wow. I didn't expect to such quick answers, especially in less than 5 mins of the OP. kudos! Very Happy

f0dder wrote:
For C++ code it's - IMHO - just too much bother to even consider. Name mangling (which differs between compilers, and iirc even different compiler versions as well), memory layout, class instatiation, etc. Much easier to call assembly routines from C++.


I'm inclined to agree with f0dder here. I ended up recoding my app in C++ because calling the routines from asm would get crazy, especially when I'm working with c++ dll's that aren't mine to control. It's not as easy as linking to C libraries where name mangling is more constant.

I did take a look at the asm to Java example in this post http://board.flatassembler.net/topic.php?t=6370 and was thinking it could be possible to do something similar with a c++ class, but I'm a bit pressed for time, so I never pursued it. Any thoughts?
Post 01 Feb 2010, 17:57
View user's profile Send private message Reply with quote
f0dder



Joined: 19 Feb 2004
Posts: 3175
Location: Denmark
f0dder 01 Feb 2010, 19:23
Bao Tran wrote:
I did take a look at the asm to Java example in this post http://board.flatassembler.net/topic.php?t=6370 and was thinking it could be possible to do something similar with a c++ class, but I'm a bit pressed for time, so I never pursued it. Any thoughts?
Unfortunately that isn't going to help much - it works for java because it's standardized, and because it's a dynamic language with reflection capabilities.

Theoretically, you could set up a framework giving you some of the same possibilities for C++ code, but it's going to be bother to write, bother to support for your classes, and won'tgive you the full level of flexibility anyway.

_________________
Image - carpe noctem
Post 01 Feb 2010, 19:23
View user's profile Send private message Visit poster's website Reply with quote
Display posts from previous:
Post new topic Reply to topic

Jump to:  


< Last Thread | Next Thread >
Forum Rules:
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You can download files in this forum


Copyright © 1999-2024, Tomasz Grysztar. Also on GitHub, YouTube.

Website powered by rwasa.