Page 1 of 1

Importing my first C++ DLL

Posted: Tue Jan 11, 2011 11:42 pm
by tutiplain
Hi all,

Ok, so I managed to compile a DLL in C++, and even managed to open it with OpenLibrary() and saw the functions inside using NextLibraryFunction(). Now... how do I call these functions? I noticed that there are some functions for this (CallFunction() and CallCFunction()). However, I have seen some examples here on the forums where people "declare" the import functions in PB code. How does this work? Thanks for any info on this.

Re: Importing my first C++ DLL

Posted: Wed Jan 12, 2011 4:26 pm
by chi
You mean something like this?

Code: Select all

;////////////////////////////////////////////////////
; dll only

Prototype _Alert()
Prototype _MsgBox( text.s )

If OpenLibrary( 0, "MyMsgBox.dll" )
  Global Alert._Alert = GetFunction( 0, "Alert")  
  Global MsgBox_._MsgBox = GetFunction( 0, "MsgBox")
EndIf

Procedure MsgBox( text.s="txt" )
  ProcedureReturn MsgBox_( text )
EndProcedure

Alert()
MsgBox()

;////////////////////////////////////////////////////
; lib + dll

Import "MyMsgBox.lib"
  Alert()
  MsgBox( text.s="txt" )
EndImport

Alert()
MsgBox()

Re: Importing my first C++ DLL

Posted: Wed Jan 26, 2011 9:50 pm
by tutiplain
Thanks, I'll try it!

Re: Importing my first C++ DLL

Posted: Thu Jan 27, 2011 3:04 am
by Marlin
This looks very promissing, but:
help wrote:Once declared, the imported functions are directly available for use in the program, like any other commands. The compiler doesn't check if the functions really exists in the imported file, so if an error occurs, it will be reported by the linker.
This implies, it would be statically linked to the library, doesn't it?

If I understand that correctly, this means, the calls would be fast,
but you would require to recompile the calling program,
once the library itself has been (changed and) newly built, right?

Re: Importing my first C++ DLL

Posted: Fri Jan 28, 2011 9:07 am
by Shield
You don't have to recompile your program to use a new version of the library,
if the procedures and parameters didn't change that is.

If they changed, then you have to recompile your application,
but you would have to do that anyway, no matter if you use Import or if you load the DLL dynamically.

Re: Importing my first C++ DLL

Posted: Fri Jan 28, 2011 3:49 pm
by Marlin
Shield wrote:You don't have to recompile your program to use a new version of the library,
if the procedures and parameters didn't change that is.
I just tried that with removing an exported procedure before the one, I was importing,
and yes, it actually continued to work without recompiling the caller.
(The imported procedure was a simple example with a message requester,
the library I built with PB under Linux; using CDECL.)

When I changed the name of the imported procedure in the library,
the caller program ceased to show the message requester of course,
but without any sign of error while being called via click in the gui.

Calling it via commandline, I saw the following output:
./TestSo.bin: symbol lookup error: ./TestSo.bin: undefined symbol: InString
This indicates to me, that the linking is aktually done using the name,
not like a (silly) speculation of mine, somehow using an address or possition
inside the library.

As I understand that, there should be at least one lookup
of the name of the called procedure before/while calling it the first time.

Do the subsequent calls to the same procedure also require that lookup
or are those done in a manner like CallCFunctionFast()/CallFunctionFast()?