News:

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

Use C++ Library

Started by ragdog, March 04, 2011, 05:53:30 PM

Previous topic - Next topic

ragdog

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?

redskull

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
Strange women, lying in ponds, distributing swords, is no basis for a system of government


ragdog

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


jj2007

Too lazy to read Vortex' post, ragdog?

ragdog

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

ragdog

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

jj2007

How much can we win if we guess the right solution without seeing the code?

drizz

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

The truth cannot be learned ... it can only be recognized.

ragdog

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