use freebasic dll with msaccess

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

Re: use freebasic dll with msaccess

Post by michaelleewebb »

when i type this:
Declare Function addnumbers Lib "C:\testdll\newaddnumbers.dll" Alias "addnumbers" (ByVal x As Integer, ByVal y As Integer) As Integer
and press return, msaccess automatically changes it to:
Declare Function addnumbers Lib "C:\testdll\newaddnumbers.dll" (ByVal x As Integer, ByVal y As Integer) As Integer
MrSwiss
Posts: 3910
Joined: Jun 02, 2013 9:27
Location: Switzerland

Re: use freebasic dll with msaccess

Post by MrSwiss »

michaelleewebb wrote:msaccess understands "integer". is 2 bytes long. we have another call "long" is bigger
This is a mayor difference, therefore, don't ever use "Integer" for this.
2 bytes (in FB = Short)
4 bytes (in FB = Long) but, "Integer" is 4 (FBC 32) or 8 (FBC 64)

DLL code:

Code: Select all

' test.dll

Extern "Windows"    ' FBC x64 (32 bit uses "windows-ms"

Function ADDNUMS(ByVal v1 As Long, ByVal v2 As Long) As Long Export
    Return v1 + v2
End Function

End Extern 
' ----- EOF -----
testing code:

Code: Select all

' test_dll-test.bas

#Inclib "test"

Declare Function AddNumbers Alias "ADDNUMS"(ByVal As Long, ByVal As Long) As Long

Print AddNumbers(2, 7)

Sleep
' ----- EOF -----
jj2007
Posts: 2326
Joined: Oct 23, 2016 15:28
Location: Roma, Italia
Contact:

Re: use freebasic dll with msaccess

Post by jj2007 »

michaelleewebb,

The code I posted further up works perfectly, from MS Access calling the FB dll. Your problem is not whether the dll sits in System32 or Wow64, it is something completely different. As far as I've understood, your MS Access does not even find the DLL file...!

Check if your MS Access and the compiled dll have the same bitness. If MS Access is a 32-bit version, you must compile the FB dll in 32-bit mode, too. Your runtime error is #48, right?

P.S.:
MrSwiss wrote:This is a mayor difference, therefore, don't ever use "Integer" for this
You must use Integer in MS Access.
MrSwiss
Posts: 3910
Joined: Jun 02, 2013 9:27
Location: Switzerland

Re: use freebasic dll with msaccess

Post by MrSwiss »

You must use Integer in MS Access.
No, Long seems also defined (as in QB).
Maybe in Access but, never in FB-dll-code (for Access).
michaelleewebb
Posts: 23
Joined: Jul 09, 2019 21:18

Re: use freebasic dll with msaccess

Post by michaelleewebb »

hummm, am running 64 bit windows 7. 32 bit msaccess (office 2007)
michaelleewebb
Posts: 23
Joined: Jul 09, 2019 21:18

Re: use freebasic dll with msaccess

Post by michaelleewebb »

hmmm, i running freebasic 64 bit verison. should i change to 32 bit verison ??
michaelleewebb
Posts: 23
Joined: Jul 09, 2019 21:18

Re: use freebasic dll with msaccess

Post by michaelleewebb »

error code is 48.
MrSwiss
Posts: 3910
Joined: Jun 02, 2013 9:27
Location: Switzerland

Re: use freebasic dll with msaccess

Post by MrSwiss »

If Access is 32 bit's, then the DLL must have the same bitness ...
(the underlying OS doesn't have anything to do with it)
This means simply, that you'll have to use FBC 32 bit's.
(to compile the DLL)
michaelleewebb
Posts: 23
Joined: Jul 09, 2019 21:18

Re: use freebasic dll with msaccess

Post by michaelleewebb »

jj2007, that worked, downloaded freebasic 32 bit and created a new folder called c:\freebasic32.

created dll add to it:
#include "Windows.bi"
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

added to msaccess module the following:
Declare Function SayHello Lib "C:\FreeBasic32\sayhi.dll" (ByVal pString As String) As Integer

to on click event i added:
Dim Res As Integer
Res = SayHello("Hello")

AND IT WORKED !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
michaelleewebb
Posts: 23
Joined: Jul 09, 2019 21:18

Re: use freebasic dll with msaccess

Post by michaelleewebb »

from my newbie point of view the problem was with the bits and btyes. as long as i was compiling a 64 bit dll and using msaccess in 32 bit then things were never going to work out. from here i would like to take over myself until i hit another snag. it seems i need to learn how to pass and receive strings, integers, and doubles. thats the stuff i normally use.
thanks to everyone for your help. where can i go to make a small donation to freebasic, i can't find anywhere on your web site.
MrSwiss
Posts: 3910
Joined: Jun 02, 2013 9:27
Location: Switzerland

Re: use freebasic dll with msaccess

Post by MrSwiss »

Try to use the provided code tag's when posting code, it makes posts more
readable, as well as it allowes to easily "copy & paste" of the code.

You don't have to make single posts, whenever you want to add something
to a already made post, instead, use the edit button ...

That would be all the donations, we'll gladly accept. ;-))
michaelleewebb
Posts: 23
Joined: Jul 09, 2019 21:18

Re: use freebasic dll with msaccess

Post by michaelleewebb »

mrswiss, you were correct about integers being different sizes in msaccess and fb. i went back to the addnumbers program. if i declare in fb:
Function AddNumbers(byval x as integer, byval y as integer) As integer EXPORT
and then declare in msaccess:
Declare Function AddNumbers Lib "c:\freebasic32\addnumbers.dll" (ByVal x As Long, ByVal y As Long) As Long
everything works, if in msaccess i use integers instead of longs i get an error message about not being able to find the dll entry point.
MrSwiss
Posts: 3910
Joined: Jun 02, 2013 9:27
Location: Switzerland

Re: use freebasic dll with msaccess

Post by MrSwiss »

Beware: this only works as long, as you use FBC 32 (it will fail too, in FBC 64).

Reason:
"Integer" = "Long", in FBC 32 only, NOT so in FBC 64, where "Integer" = "LongInt".
If you use "Long", because it being 32 bit fixed size, it works with both compilers
likewise (aka: portable code).
michaelleewebb
Posts: 23
Joined: Jul 09, 2019 21:18

Re: use freebasic dll with msaccess

Post by michaelleewebb »

good to know mrswiss, i think i've got the numbers thing figured out. i'm looking a strings now. i see pointers (shudder). i used to use them back in the turbo c 2.0 days. lots and lots of fun. do you know a way around using pointers ?? i tried changing this line:
Function SayHello(text As string ptr) As integer EXPORT
to this:
Function SayHello(text As string ) As integer EXPORT

it didn't work.
i understand that a pointer in fb is a place in memory that describes the string. it start location, length,etc. if i must use the string pointer is it wise to try to convert it to a regular string then manipulate the string how i want then convert it back to a string pointer and then send it back to msaccess ? i'm thinking msaccess may be surprised by a string that has changed size.
MrSwiss
Posts: 3910
Joined: Jun 02, 2013 9:27
Location: Switzerland

Re: use freebasic dll with msaccess

Post by MrSwiss »

i'm looking a strings now. i see pointers (shudder).
Sorry, pointers are essential to easily interface with C/C++ stuff (like WIN-API).
A FB-String is actually a Type (not really a base data-type), therefore, mostly not
recognized by other languages, except maybe, if another BASIC dialect.

Code: Select all

Type _String	' very much simplified
	As ZString Ptr data	' actual string's data
	As Integer	  slen	' strings length (in Bytes)
	As Integer     smem	' allocated memory (in Bytes)
End Type
Like already stated before:

Code: Select all

ByVal As ZString Ptr	' should work ...
On the FB side, String can be used (automatic conversion) on the other side, just
the strings data (C-String zero terminated, more precise: Array of Char).
(a Char in C, is simply a Byte in FB)
Post Reply