Importing my first C++ DLL

Just starting out? Need help? Post your questions and find answers here.
tutiplain
User
User
Posts: 43
Joined: Wed Jun 30, 2010 3:00 am

Importing my first C++ DLL

Post 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.
User avatar
chi
Addict
Addict
Posts: 1090
Joined: Sat May 05, 2007 5:31 pm
Location: Austria

Re: Importing my first C++ DLL

Post 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()
Et cetera is my worst enemy
tutiplain
User
User
Posts: 43
Joined: Wed Jun 30, 2010 3:00 am

Re: Importing my first C++ DLL

Post by tutiplain »

Thanks, I'll try it!
Marlin
Enthusiast
Enthusiast
Posts: 406
Joined: Sun Sep 17, 2006 1:24 pm
Location: Germany

Re: Importing my first C++ DLL

Post 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?
User avatar
Shield
Addict
Addict
Posts: 1021
Joined: Fri Jan 21, 2011 8:25 am
Location: 'stralia!
Contact:

Re: Importing my first C++ DLL

Post 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.
Image
Blog: Why Does It Suck? (http://whydoesitsuck.com/)
"You can disagree with me as much as you want, but during this talk, by definition, anybody who disagrees is stupid and ugly."
- Linus Torvalds
Marlin
Enthusiast
Enthusiast
Posts: 406
Joined: Sun Sep 17, 2006 1:24 pm
Location: Germany

Re: Importing my first C++ DLL

Post 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()?
Post Reply