UDTs in dlls

General FreeBASIC programming questions.
Post Reply
deltarho[1859]
Posts: 4310
Joined: Jan 02, 2017 0:34
Location: UK
Contact:

UDTs in dlls

Post by deltarho[1859] »

This may qualify as a daft question but it is something that I have never done before, here and elsewhere.

Suppose we have:

Code: Select all

#Include Once "SomeCode.bas"
 
Dim c as clr
'blah,,blah, blah
where clr is defind in SomeCode.bas.

Now, if SomeCode was converted to a dll and we called that dll how do we reference clr?

I cannot find anything in Help to get me started.
fxm
Moderator
Posts: 12110
Joined: Apr 22, 2009 12:46
Location: Paris suburbs, FRANCE

Re: UDTs in dlls

Post by fxm »

Very short example:

Code: Select all

'UDT.bi
'Type declaration

Type UDT
  Dim As Integer I
  Declare Function returnI () As Integer
End Type

Code: Select all

'UDT.bas
'Type declaration + definition
'to be compiled with option -dll

#Include "UDT.bi"

Function UDT.returnI () As Integer Export
  Return This.I
End Function

Code: Select all

'Main.bas

#Include "UDT.bi"

#Inclib "UDT"

Dim As UDT u
u.I = 123
Print u.returnI()

Sleep
dodicat
Posts: 7983
Joined: Jan 10, 2006 20:30
Location: Scotland

Re: UDTs in dlls

Post by dodicat »

You have to replicate the type.
example
colour.bas
compile to dll

Code: Select all

 
'colour.bas  compile with -dll
union colour
    as ulong c
    type
    as ubyte blue
    as ubyte green
    as ubyte red
    as ubyte alph
end type

declare operator let(as ulong)
declare constructor(as ulong=0) 
end union

operator colour.let(u as ulong) export
c=u
end operator

constructor colour(u as ulong) export
c=u
end constructor



  
here is the test code with the replica udt.

Code: Select all

 'test.bas

#inclib "colour"

union colour
    as ulong c
    type
    as ubyte blue
    as ubyte green
    as ubyte red
    as ubyte alph
end type

declare operator let(as ulong)
declare constructor(as ulong=0) 
end union

dim as colour c=rgb(200,100,50) 'use constructor
print c.red,c.green,c.blue

dim as colour z
z=rgba(100,101,102,103)          'use let
print z.red,z.green,z.blue,z.alph
sleep
 
deltarho[1859]
Posts: 4310
Joined: Jan 02, 2017 0:34
Location: UK
Contact:

Re: UDTs in dlls

Post by deltarho[1859] »

fxm wrote:Very short example:
And simple when you know how.

You came back so quick I just had time to put the kettle on for a cup of tea. <smile>

That is awesome. Thanks fxm

Actually, your example would be a helpful addition to the Help file.
deltarho[1859]
Posts: 4310
Joined: Jan 02, 2017 0:34
Location: UK
Contact:

Re: UDTs in dlls

Post by deltarho[1859] »

@dodicat

Another swift response.

When I was looking at this I asked myself "How do I replicate the UDT" figuring that replication was needed.

However, fxm is not using replication and yet you say "You have to replicate the type." or this a case of there is more than one way to skin a cat?
dodicat
Posts: 7983
Joined: Jan 10, 2006 20:30
Location: Scotland

Re: UDTs in dlls

Post by dodicat »

When I posted I didn't see fxm's post.
But it is the same essentially.
The main code includes udt.bi which contains the type declaration.
Replicate is perhaps too strong a word.
For instance you can call the type fields different names when (replicating) the type in the running code.
Also any private: or protected: in a type (compiled to dll) is ignored in the dll call unless you replicate private/protected in the calling udt (replica)
deltarho[1859]
Posts: 4310
Joined: Jan 02, 2017 0:34
Location: UK
Contact:

Re: UDTs in dlls

Post by deltarho[1859] »

@dodicat

Magic! Thank you.
Tourist Trap
Posts: 2958
Joined: Jun 02, 2015 16:24

Re: UDTs in dlls

Post by Tourist Trap »

deltarho[1859] wrote:@dodicat

Magic! Thank you.
This topic is rather important at the intermediate level. Some documentation page would be great.
coderJeff
Site Admin
Posts: 4326
Joined: Nov 04, 2005 14:23
Location: Ontario, Canada
Contact:

Re: UDTs in dlls

Post by coderJeff »

A few topics that if read together, may give insight:
Source Files
Header Files
Static Libraries
Shared Libraries
Glossary
Glossary wrote:declaration
A source code statement that introduces a symbol, constant, variable, procedure, data type, or similar, to the compiler but not necessarily allocate any space for it. See Dim, Declare, Extern, Type.

definition
A source code statement (or statements) that allocates space for data or code. For example, Sub defines a procedure by allocating space for the program code it will contain. Some statements can be both a declaration and a definition. For example, Dim both declares and defines a variable.
Post Reply