News:

MASM32 SDK Description, downloads and other helpful links
MASM32.com New Forum Link
masmforum WebSite

Re: c++ class and MASM?

Started by ToutEnMasm, December 27, 2010, 09:37:24 PM

Previous topic - Next topic

ToutEnMasm


I return on this subject because it seems i have some success in it.
I have used a library with the compiled class in it (directx )
Fonctions appeared as public symbols (dumpbin c++)
Quote
File Type: LIBRARY
    1840 public symbols

    33B9A ??0CWaveFile@@QAE@XZ
    33B9A ??1CWaveFile@@QAE@XZ
    33B9A ??_C@_0FN@KGGCDDFH@d?3?2program?5files?2microsoft?5direc@
    33B9A ??_C@_17OPFMFDPO@?$AAW?$AAA?$AAV?$AA?$AA@
    33B9A ??_C@_17PBGNJEND@?$AAn?$AAe?$AAw?$AA?$AA@
   .....
    33B9A ?Open@CWaveFile@@QAEJPA_WPAUtWAVEFORMATEX@@K@Z
Some declare must be done in masm,like that:
Quote
Some declare must be done in masm,like that:
;SYSCALL, STDCALL,BASIC, FORTRAN, PASCAL
EXTRN   SYSCALL ??0CWaveFile@@QAE@XZ:PROC
EXTRN   syscall ?Open@CWaveFile@@QAEJPA_WPAUtWAVEFORMATEX@@K@Z:PROC
and code is as follow:
Quote
   lea ecx,wave
   call   ??0CWaveFile@@QAE@XZ
   push   1
   push   0
   lea   eax,wavefile 
   push   eax
   lea   ecx, wave
   call   ?Open@CWaveFile@@QAEJPA_WPAUtWAVEFORMATEX@@K@Z ; CWaveFile::Open
Need more experiments and a little more defines to be more readable.A macro to make the call will be usefull.





BogdanOntanu

Please do not revive an 3 years old topic. Create a New thread if you have to.

About the subject of using an compiled C++ class in your ASM project:

The DirectX interfaces are designed to be used from other languages. They are COM interfaces and this is not exactly the same as C++ classes  as intended in the subject.

In order to use C++ classes in your ASM application one will need a LOT of support from the C++ compiler and because of this this it is much more complicated than using DirectX.  Of course one can cripple the C++ code and write C code under the disguise of C++ "classes" and this can make things easier ;)

It is more simple to understand what the C++ "class" code does and to simply translate it to ASM or normal procedural ANSI C code
Ambition is a lame excuse for the ones not brave enough to be lazy.
http://www.oby.ro

dedndave

good luck, Yves   :U
that stuff all looks like French, to me   :P
i think somebodies "@" key was stuck or something   :lol

redskull

The only reliable way to use C++ classes from ASM is to use a COM object, and the appropriate methods therein.  The problem you'll run into (other than the obvious naming one) is that any non-const member function will require a pointer to the objects data area, i.e. the 'this' pointer; depending on which compiler built the library, it will want it either on the stack as the first parameter (stdcall), or in ECX (thiscall).  Also, if the function has a variable number of arguments, then it switches to callee-cleanup to caller-cleanup (cdecl).

Like B.O. said, it's highly dependent on the compiler that was used.

-r
Strange women, lying in ponds, distributing swords, is no basis for a system of government

ToutEnMasm

Quote
The only reliable way to use C++ classes from ASM is to use a COM object
Are you sure ?
I have more results that show it is possible to use a class with masm.
Some things must be done.
Quote
there is one header .h and a .cpp by class
All used functions must be defined in the .cpp
sample:
if in the .h there is
Quote
    WAVEFORMATEX* GetFormat()
    {
        return m_pwfx;
    };


it must be modified as this
Quote
WAVEFORMATEX* GetFormat();

The function must be added to the .cpp
Quote
WAVEFORMATEX* CWaveFile::GetFormat()
    {
        return m_pwfx;
    };


Important thing also is the init of the class
Quote
      Local wave[148]:BYTE
..
   ;init wave ,init of the class
   lea ecx,wave   
   call   ??0CWaveFile@@QAE@XZ

The size of the wave variable can be known disassembling the c++ code /Fa






redskull

"Possible" is not the same as "reliable".  None of what you post is applicable if the code is written in, say, GCC or Builder (the name mangling will be different).  Your example is a const function, so the 'this' point is a non-issue; find one that does require it, and it will crash.  Similarly, it has no arguments, so the order is unimportant as well.  Also, how did you plan on reserving the space for it?  Somewhere you need a call to HeapAlloc(), and you also have to know the appropriate amount of space.  Also, what about the desctructor when the object is dealloacted?  Also, what happens if the functions are virtual? Etc, Etc, Etc.

-r
Strange women, lying in ponds, distributing swords, is no basis for a system of government

Vortex

Hi Yves,

EXTRN   SYSCALL ??0CWaveFile@@QAE@XZ:PROC
EXTRN   syscall ?Open@CWaveFile@@QAEJPA_WPAUtWAVEFORMATEX@@K@Z:PROC


You don't have to use those complex names in Masm. Why not to convert the external symbols to more simple forms?

Agner Fog's objconv tool does the job :

Calling C++ functions from MASM

ToutEnMasm

Quote
Possible" is not the same as "reliable".  None of what you post is applicable if the code is written in, say, GCC or Builder (the name mangling will be different).  Your example is a const function, so the 'this' point is a non-issue; find one that does require it, and it will crash.  Similarly, it has no arguments, so the order is unimportant as well.  Also, how did you plan on reserving the space for it?  Somewhere you need a call to HeapAlloc(), and you also have to know the appropriate amount of space.  Also, what about the desctructor when the object is dealloacted?  Also, what happens if the functions are virtual? Etc, Etc, Etc.


You have never see a sample who works and solve all this problems,i will post one in my next mail.

To Vortex,
I will try the tool before post the sample



redskull

Quote from: ToutEnMasm on December 28, 2010, 07:39:49 PM
You have never see a sample who works and solve all this problems,i will post one in my next mail.

I'm not saying it can't be done, I'm saying that it won't be a general solution; it will be a specific solution that works for one specific version of one specific class in one specific vendors language, one way, once, which is not macro-izable

-r
Strange women, lying in ponds, distributing swords, is no basis for a system of government

ToutEnMasm

Hello,
Here is a sample that solve all the problems.
It use the cwavefile class c++  (Microsoft DirectX SDK (June 2010))
The link must be made with the object of the class (a library failed to exit)
The sample play a mono wave sound.It is a translate of the:
\Samples\C++\XAudio2\XAudio2BasicSound
You need the "ready to use SDK" to recompile it (dowloadable in the windows.inc subforum)
directx headers files are in zip with the object of the modified class.
The sample use all that can be possible to use.COM,class fonctions,new and delete operator.
There is no one problem.
I have done my test with ml 10 and the libraries of the c++ express 10 and sdk 7.1
There is just to give a better look to the code ( some declare and a macro)

japheth

The sample demonstrates that it's doable to use C++-classes in Masm.
However, it also demonstrates that there are a few disadvantages and - more importantly - that there are no reasons why it should be done.

ToutEnMasm

 :bg
Quote
no reasons why it should be done
It is just an open door to use directx (for example) with no more problem than with the c++.
C++ class are numerous,perhaps have there no use ?  (if i understand you)
Play with bits is better !!  :dazzled:

dedndave

i can think of a reason
i don't want to learn C++   :P

japheth

Quote from: dedndave on December 29, 2010, 03:11:55 PM
i can think of a reason
i don't want to learn C++   :P

Ok, that's a valid reason. But then ... what are the C++ classes which you desperately want to use ... with Masm?  :green

ToutEnMasm


To simplify the define prototype:
a TEXTEQU is enough
Quote
??3@YAXPAX@Z PROTO syscall ??3@YAXPAX@Z :DWORD ; operator delete
delete TEXTEQU <??3@YAXPAX@Z>
Quote
      lea   ecx, wave
      invoke delete,pbWaveData