use freebasic dll with msaccess

New to FreeBASIC? Post your questions here.
michaelleewebb
Posts: 23
Joined: Jul 09, 2019 21:18

use freebasic dll with msaccess

Postby michaelleewebb » Jul 09, 2019 21:26

the problem here is probably sitting right in front of me but i don't see it.
i'm using the example program found under the examples/dll progam. i moved the files to the c:\freebasic folder where the fbc.exe program sits. i compiled the mydll.dll file and compiled the test program. the test program works perfect. but when i try to use the dll in msaccess i get the following error message: file not found: c:\freebasic\mydll.dll. i dont see how it cant find it. its sitting right there in the c:\freebasic folder. this is the code i'm using in msaccess to call the dll,

Option Compare Database
Private Declare Function AddNumbers Lib "c:\freebasic\mydll.dll" (ByVal x As Integer, ByVal y As Integer) As Integer

Private Sub Command1_Click()
Dim x As Integer
x = AddNumbers(1, 2)
MsgBox x
End Sub

what i'm i missing ???
dodicat
Posts: 6024
Joined: Jan 10, 2006 20:30
Location: Scotland

Re: use freebasic dll with msaccess

Postby dodicat » Jul 10, 2019 12:27

A dll should be in the same folder as the source code calling it.
Otherwise it should be the system32 or sysWOW64 folder, with the associated .dll.a file (import library file) in the freebasic lib folder (along with all the other .a files)
michaelleewebb
Posts: 23
Joined: Jul 09, 2019 21:18

Re: use freebasic dll with msaccess

Postby michaelleewebb » Jul 10, 2019 14:06

moved both files to their own folder c:\testdll. i changed the msaccess code to:

Private Declare Function AddNumbers Lib "c:\testdll\mydll.dll" (ByVal x As Integer, ByVal y As Integer) As Integer

so there are 2 files in the testdll folder.
dlltest.accdb
mydll.dll

the dlltest.accdb is the msaccess program.
i would attach a screen shot of the folder but i can't find the attach button. still getting error message : file not found: c:\testdll\mydll.dll.

maybe the dll needs to be registered ? i don't see why, the test program works and the dll is not registered. maybe the dll needs something more ?
srvaldez
Posts: 2157
Joined: Sep 25, 2005 21:54

Re: use freebasic dll with msaccess

Postby srvaldez » Jul 10, 2019 14:35

try leaving out .dll in the declaration

Code: Select all

Private Declare Function AddNumbers Lib "c:\testdll\mydll" (ByVal x As Integer, ByVal y As Integer) As Integer
michaelleewebb
Posts: 23
Joined: Jul 09, 2019 21:18

Re: use freebasic dll with msaccess

Postby michaelleewebb » Jul 10, 2019 14:55

tried:
Private Declare Function AddNumbers Lib "c:\testdll\mydll" (ByVal x As Integer, ByVal y As Integer) As Integer

still no joy.
michaelleewebb
Posts: 23
Joined: Jul 09, 2019 21:18

Re: use freebasic dll with msaccess

Postby michaelleewebb » Jul 10, 2019 15:16

tried placing the dll into c:\windows\system32
tried Private Declare Function AddNumbers Lib "mydll.dll" (ByVal x As Integer, ByVal y As Integer) As Integer
tried Private Declare Function AddNumbers Lib "c:\windows\system32\mydll.dll" (ByVal x As Integer, ByVal y As Integer) As Integer

no luck.
MrSwiss
Posts: 3307
Joined: Jun 02, 2013 9:27
Location: Switzerland

Re: use freebasic dll with msaccess

Postby MrSwiss » Jul 10, 2019 15:45

You cannot put "private" into a declaration, its only allowed in the "implementation".
(within the .DLL in this particular case)

Try: Declare Function AddNumbers Lib "mydll.dll" (ByVal x As Integer, ByVal y As Integer) As Integer

I'm very "rusty" in Access, but the "data-type" from Access might be
different than "Integer" (this is a FB type).
You might have to consult Access documentation, for that sort of thing.
(finding the correct "data-type" from Access's point of view)
michaelleewebb
Posts: 23
Joined: Jul 09, 2019 21:18

Re: use freebasic dll with msaccess

Postby michaelleewebb » Jul 10, 2019 16:42

yes from this i learned alot
viewtopic.php?t=2878

access and excel need dlls to be setup different more like this in the dll part:
Function cube alias "cube" ( ByVal arg As Double) As Double Export
cube=arg^3
End Function

then in access, in a module more like this:
Declare Function cube Lib "C:\testdll\fb_2_xls.dll" Alias "cube@8" (ByVal arg As Double) As Double

note the cube@8. i need to add up the btyes being sent to msaccess, little bit more than i want to get into right now. thanks to all for your help.
dodicat
Posts: 6024
Joined: Jan 10, 2006 20:30
Location: Scotland

Re: use freebasic dll with msaccess

Postby dodicat » Jul 10, 2019 16:43

step 1
Call this file add.bas and compile with -dll switch to get add.dll

Code: Select all

function AddNumbers( byval a as integer, byval b as integer) as integer export
   function = a + b
end function
'compile as fbc -dll add.bas
 

step 2
In the same folder as add.dll (from the above compile) use this test.bas (don't use switch -dll)

Code: Select all

#inclib "add" 

declare function AddNumbers( byval a as integer, byval b as integer) as integer
print addnumbers(6,4)
sleep

 


Once you get one success at this in it's simplest form, you can experiment with things like cdecl and alias for more complex dll's and dll's from other languages.
deltarho[1859]
Posts: 2102
Joined: Jan 02, 2017 0:34
Location: UK

Re: use freebasic dll with msaccess

Postby deltarho[1859] » Jul 11, 2019 8:19

When compiling a FreeBASIC dll for use with another language the .a file, for static libraries, is not used for shared (dynamic) libraries; even though it is generated.

If the other language runs in Windows then we need to generate a standard Windows dll. To avoid the need for appending "@N" we use ' Extern "Windows-MS" '.

For example in FreeBASIC we should use

Code: Select all

#include once "windows.bi" ' To access Windows data types
Extern "Windows-MS"
Function CryptoDW Alias "CryptoDW" As Dword Export
'blah, blah, blah
End Function
End Extern

To use "CryptoDW" in PowerBASIC, for example, we would use

Code: Select all

Declare Function CryptoDW Lib "CryptoRndIII.dll" Alias "CryptoDW" As Dword ' [1]

Obviously, CryptoRndIII.dll needs to be in the same directory as the PowerBASIC calling code.

I do not know how to Declare in msaccess but I suspect that [1] may well work.
jj2007
Posts: 1263
Joined: Oct 23, 2016 15:28
Location: Roma, Italia
Contact:

Re: use freebasic dll with msaccess

Postby jj2007 » Jul 11, 2019 10:56

This works fine with Ms Access (special thanks to deltarho - the Extern did the job):

Code: Select all

Declare Function SayHello Lib "C:\FreeBasic\Whatever.dll" (ByVal pString As String) As Integer
Sub SayHi()
    Dim Res As Integer
    Res = SayHello("Hello")
End Sub

Code: Select all

#include "Windows.bi"   ' for e.g. MessageBox(0, "Hello string", "Hello title", MB_OK)

Extern "Windows-MS"
Function SayHello(text As string ptr) As integer EXPORT
  return MessageBox(0, text, "Gimme an answer:", MB_YESNOCANCEL)
End function
End Extern

Note that the DLL does not have to be in the same folder as the exe. You just need to give the full path. This should work with any language that can load a Windows DLL, for example:

Code: Select all

include \masm32\MasmBasic\MasmBasic.inc
  Init
  Dll "C:\FreeBasic\Whatever.dll"
  Declare SayHello, 1
  MsgBox 0, Str$("The result is %i", SayHello("Gimme a string")), "Hi", MB_OK
EndOfCode
dodicat
Posts: 6024
Joined: Jan 10, 2006 20:30
Location: Scotland

Re: use freebasic dll with msaccess

Postby dodicat » Jul 11, 2019 11:42

Thanks for that tip deltarho.
I don't have visual basic or microsoft office, but the dll can be created in fb with extern "windows-MS" and recalled in fb with extern "windows-MS"

e.g. (IN FREEBASIC)
extern "windows-MS"
declare function AddNumbers lib "add.dll" alias "AddNumbers"( byval a as integer, byval b as integer) as integer
end extern

However the path ( lib "add.dll" ) in the declaration doesn't work with a full path in fb
e.g.
lib "C:\Users\User\Desktop\testdll\add.dll" produces an error
It looks like in freebasic the dll must be in the same folder as the calling source code, or in the system folder with the import .dll.a file in the fb lib folder.
michaelleewebb
Posts: 23
Joined: Jul 09, 2019 21:18

Re: use freebasic dll with msaccess

Postby michaelleewebb » Jul 11, 2019 12:54

well if everyone else is going to keep trying so am i.
i did all of the following in the c:\freebasic folder.
i created a bas file named sayhello.bas in it i copied and pasted:

#include "Windows.bi" ' for e.g. MessageBox(0, "Hello string", "Hello title", MB_OK)

Extern "Windows-MS"

Function SayHello(text As string ptr) As integer EXPORT
return MessageBox(0, text, "Gimme an answer:", MB_YESNOCANCEL)
End function
End Extern

i used this command to create the dll:
fbc -dll sayhello.bas

i got a warning, but it created the dll:
sayhello.bas(5) waring 3(1): passing different pointer types. at parameter 2 of MESSAGEBOX()

i copied the dll to the folder c:\testdll, this is where the msaccess program is.
i started the msaccess program with a short cut thats set to start in c:\testdll.
msaccess doesn't like the decare statement unless you put it in a module, so i created a module name dlls and copied this into it:

Declare Function SayHello Lib "C:\testdll\sayhello.dll" (ByVal pString As String) As Integer

then in my form on the command1 button event "on click" i added the following:

Private Sub Command1_Click()

Dim Res As Integer
Res = SayHello("Hello")

End Sub

i'm still getting the error message: file not found: c:\testdll\sayhello.dll
i tried variations in the declare statement such as:
declare Function SayHello Lib "sayhello.dll" (ByVal pString As String) As Integer
declare Function SayHello Lib "sayhello" (ByVal pString As String) As Integer

still same error message.
thank you all for your support, anyone have an idea ??
srvaldez
Posts: 2157
Joined: Sep 25, 2005 21:54

Re: use freebasic dll with msaccess

Postby srvaldez » Jul 11, 2019 12:59

perhaps you need to use alias

Code: Select all

Declare Function SayHello Lib "C:\testdll\sayhello.dll" Alias "SayHello" (ByVal pString As String) As Integer
michaelleewebb
Posts: 23
Joined: Jul 09, 2019 21:18

Re: use freebasic dll with msaccess

Postby michaelleewebb » Jul 11, 2019 13:04

tried:
Declare Function SayHello Lib "C:\testdll\sayhello.dll" Alias "SayHello" (ByVal pString As String) As Integer
still same error message

tried placing dll in c:\windows\system32
same error message
removed dll from c:\windows\system32

Return to “Beginners”

Who is online

Users browsing this forum: No registered users and 1 guest