Real Software Forums

The forum for Real Studio and other Real Software products.
[ REAL Software Website | Board Index ]
It is currently Tue Nov 12, 2019 12:38 am
xojo

All times are UTC - 5 hours




Post new topic Reply to topic  [ 3 posts ] 
Author Message
 Post subject: Working with .dll's
PostPosted: Tue Aug 21, 2012 8:17 pm 
Offline

Joined: Wed May 12, 2010 8:05 am
Posts: 8
Can anyone point me to a good explanation/example code/etc of how best to interface/work with Windows .DLL files.
I'm a physical computing guy and a lot of devices I work with provide a .DLL file with which you're supposed to
interface in order for "the magic" to happen. I have a good deal of experience programming in RealStudio but
no experience working with .DLL files and no idea how to get RealBasic to talk with one. I can't be the only
one who could use a good solid example of how to get a RealStudio program and a .DLL to play nice together.
Any pointers hugely appreciated.


Top
 Profile  
Reply with quote  
 Post subject: Re: Working with .dll's
PostPosted: Tue Aug 21, 2012 9:41 pm 
Offline
User avatar

Joined: Mon Apr 02, 2007 2:08 am
Posts: 1225
Location: San Francisco, CA, USA
kjenkins wrote:
Any pointers hugely appreciated.


Pointer 1: Get familiar with Ptr's :D
Pointer 2: Pray that the DLL's developer believes in documentation.

All jokes aside, it's pretty straightforward most of the time. A familiarity with the source language of the DLL is very handy when determining equivalency between the DLL's datatypes and RB's datatypes.

Consider the GetLastError function in Windows' kernel32.dll. Lots of libraries have something like this, a function that gets the most recent error, so it's a handy example.

Microsoft's docs define it like this:

Quote:
DWORD WINAPI GetLastError(void);


Obviously this is not RB; this is C++. The equivalent declaration in RB would be:

Function GetLastError() As Integer


But we need to indicate that this is an external function and also where to find it:

Declare Function GetLastError Lib "Kernel32.dll" () As Integer


Once that's done, you call it much like any other function:

Declare Function GetLastError Lib "Kernel32.dll" () As Integer
Dim LastError As Integer
LastError = GetLastError()


This is about as specific as you can get with regard to using external libraries since each and every library developer has a different idea about how things ought to be done. But, for the sake of example, let's continue with Kernel32.dll.

Kernel32 also exports the FormatMessage function (AKA, FormatMessageW for Unicode), which can be used to get error messages for system error numbers.
Declare Function FormatMessageW Lib "Kernel32" (dwFlags As Integer, lpSource As Integer, dwMessageId As Integer, dwLanguageId As Integer, lpBuffer As ptr, nSize As Integer, Arguments As Integer) As Integer


This function takes a lot more parameters, but is otherwise very similar to GetLastError. Since it needs parameters, we need to set them all up before calling the external function (assume the LastError is still in scope):

Declare Function FormatMessageW Lib "Kernel32" (dwFlags As Integer, lpSource As Integer, dwMessageId As Integer, dwLanguageId As Integer, lpBuffer As ptr, nSize As Integer, Arguments As Integer) As Integer

Const FORMAT_MESSAGE_FROM_SYSTEM = &H1000
Dim buffer As New MemoryBlock(2048)
If FormatMessageW(FORMAT_MESSAGE_FROM_SYSTEM, 0, LastError, 0 , Buffer, Buffer.Size, 0) <> 0 Then
MsgBox(Buffer.WString(0))
Else
MsgBox("An unknown error was reported by Win32. Error number: " + Str(LastError))
End If


Did you see the Ptr? I wasn't kidding about them!

_________________
Boredom Software


Top
 Profile  
Reply with quote  
 Post subject: Re: Working with .dll's
PostPosted: Thu Aug 23, 2012 6:13 am 
Offline

Joined: Sun Nov 26, 2006 11:01 am
Posts: 58
That's my favorite page for calling DLL-functions wihin RS:
http://www.declaresub.com/iDeclare/Declares/


Top
 Profile  
Reply with quote  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 3 posts ] 

All times are UTC - 5 hours


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
Jump to:  
cron
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group