The MASM Forum Archive 2004 to 2012

General Forums => The Campus => Topic started by: ragdog on March 04, 2011, 05:53:30 PM

Title: Use C++ Library
Post by: ragdog on March 04, 2011, 05:53:30 PM
Hello

I would like use a C++ static Library in masm32
Now is the problem to call a procedur from this lib
How i can call this?

BOOL MYLib::Init

I have try to use

Init PROTO C

Invoke Init

Have your an idea?
Title: Re: Use C++ Library
Post by: redskull on March 04, 2011, 06:04:05 PM
The simple answer is that all the functions must declared extern "C" and not class methods (or at least static ones).  If you don't have the source for the static library, things get tricky.

r
Title: Re: Use C++ Library
Post by: Vortex on March 04, 2011, 06:33:22 PM
Calling C++ functions from MASM (http://www.masm32.com/board/index.php?topic=5594.0)
Title: Re: Use C++ Library
Post by: ragdog on March 04, 2011, 09:13:03 PM
Thanks for your answhere

I comming not further

I create a static Lib with Vc++ 6.0

and add this code


MY_Init()
{
MessageBox(0, "Messages", "info", 0);
return (FALSE);
}


Now compile it to a Lib and it works fine
But in my Asm source can i not call MY_Init from this lib


error LNK2001: unresolved external symbol MY_Init
fatal error LNK1120: 1 unresolved externals

Title: Re: Use C++ Library
Post by: jj2007 on March 04, 2011, 09:41:36 PM
Too lazy to read Vortex' post, ragdog?
Title: Re: Use C++ Library
Post by: ragdog on March 04, 2011, 10:06:33 PM
Yes i have it

but now i use in my MY_Init() procedur more parameter


MY_Init(DWORD lpmess, DWORD lpinfo, DWORD lpmess2, DWORD lpinfo2,
LPTSTR filename)
{
MessageBox(0, lpmess, lpinfo, 0);
MessageBox(0, lpmess2, lpinfo2, 0);
MessageBox(0,filename, 0, 0);
return (FALSE);
}


And i have change the Proto

?MY_Init@@YAHKKKKPAD@Z PROTO SYSCALL
MY_Init EQU <pr5 PTR ?MY_Init@@YAHKKKKPAD@Z>

If i call this in my Masm source crash it
without parameter works this ::)

must i set here the number of parameter? pr5
Title: Re: Use C++ Library
Post by: ragdog on March 05, 2011, 02:31:48 PM
Now have i make a Libary but i have by compiling this error

error LNK2001: unresolved external symbol WriteFile

Why ? I use in my masm32 source

include kernel32.inc
includelib kernel32.lib
Title: Re: Use C++ Library
Post by: jj2007 on March 05, 2011, 04:49:18 PM
How much can we win if we guess the right solution without seeing the code?
Title: Re: Use C++ Library
Post by: drizz on March 05, 2011, 05:41:55 PM
Write a C interface to that C++ library.

// C++ LIB
<?php // just for syntax hl :D
class AwesomeCPPLib
{
bool DoStuff(int abc)
{
}
}
?>


// C PROXY -- COMPILED AS C++!!
<?php // just for syntax hl :D

extern "C" void__stdcall AwesomeCPPLib_Create()
{
AwesomeCPPLib *that = new AwesomeCPPLib();
return (void*)that;
}

extern "C" bool __stdcall AwesomeCPPLib_DoStuff(AwesomeCPPLibthatint abc)
{
return that->DoStuff(abc);
}

extern "C" void __stdcall AwesomeCPPLib_Destroy(AwesomeCPPLibthat)
{
delete that;
}

?>



// MASM HEADER - SuperCPPLib_for_MASM.inc

AwesomeCPPLib_Create proto stdcall
AwesomeCPPLib_DoStuff proto stdcall :ptr, :sdword
AwesomeCPPLib_Destroy proto stdcall :ptr


// BATCH

REM --- Explode original library
polib /explode SuperCPPLib.lib

REM --- Copy compiled proxy obj
copy ../SuperCPPLib_proxy.obj ./SuperCPPLib_proxy.obj

REM --- Make new library
polib /out:SuperCPPLib_for_MASM.lib *.obj


// MASM program includes

include SuperCPPLib_for_MASM.inc
includelib SuperCPPLib_for_MASM.lib

// MASM CODE

invoke AwesomeCPPLib_Create
mov ebx,eax
invoke AwesomeCPPLib_DoStuff,ebx,123
invoke AwesomeCPPLib_Destroy,ebx




OR


you could stop converting everything to masm and just write c++ code (http://www.masm32.com/board/Smileys/mforum/icon_eek.gif)

Title: Re: Use C++ Library
Post by: ragdog on March 05, 2011, 06:33:44 PM
Quoteyou could stop converting everything to masm and just write c++ code

:bg I write a c++ code but i make a lib and Test it in masm32

Thanks i try your soulution