How to use correct Common Shared syntax?

Windows specific questions.
E Dingsor
Posts: 24
Joined: Nov 15, 2017 9:53
Location: Norway

How to use correct Common Shared syntax?

Post by E Dingsor »

Hi,
Trying to use a variable over multiple modules, but the compiler(1.06 Win) throws error code 4 back: Duplicated definition, g_hInst in 'common shared g_hInst as HINSTANCE

Help file says:
Variable declaration and scope modifier
Syntax
Common [Shared] symbolname[()] [AS DataType] [, ...]

Description

Declares a variable which is shared between code modules. A matching Common statement must appear in all other code modules using the variable.

Common variables cannot be initialized.
Common arrays are always variable-length, and must be defined with an empty parameter list (), and its dimensions set in a later Dim or ReDim statement.
Common variables cannot be instances of a user-defined type having a constructor or a destructor even implicit.

The Shared optional parameter makes the variable global so that it can be used inside Subs and Functions, as well as at module level.
I have a DllMain routine where I want to save the dll handle for later use in another module.

Example code bas1.bas :
common shared g_hInst as HINSTANCE ' this is the global variable

Public Function DllMain stdcall Alias "XLLMAIN" _
( _
Byval hDLL As HINSTANCE, _
Byval fdwReason As DWORD, _
Byval lpvReserved As LPVOID _
) As BOOL


Select Case (fdwReason)
Case DLL_PROCESS_ATTACH

' // The instance handle passed into DllMain is saved
' // in the global variable g_hInst for later use.

g_hInst = hDLL 'save hinstance in global variable

Case DLL_PROCESS_DETACH

Case DLL_THREAD_ATTACH

Case DLL_THREAD_DETACH

End Select

Return 1
End Function


Bas2.bas
common shared g_hInst as HINSTANCE 'declaring as help file says. Correct??

Sub Get_PATH()


dim szFile1 AS WSTRING* 2048

'g_hInst is global handle of dll. Initiated via DLLMAIN
GetModuleFileNameW g_hInst, @szFile1, SIZEOF (szFile1)

end sub

Any hints how to get this going i.e using a global variable over multiple modules.
\E
fxm
Moderator
Posts: 12107
Joined: Apr 22, 2009 12:46
Location: Paris suburbs, FRANCE

Re: How to use correct Common Shared syntax?

Post by fxm »

To define a variable which is shared between code modules (compiled separately):
  • You must declare the variable (with a Common statement) in each module using the variable.:
    Common [Shared] variable As datatype
  • In addition, in one of the concerned module, the variable must of course also be defined::
    Common [Shared] variable As datatype
    Dim [Shared] variable As datatype
E Dingsor
Posts: 24
Joined: Nov 15, 2017 9:53
Location: Norway

Re: How to use correct Common Shared syntax?

Post by E Dingsor »

Hmmm... compiler still throws error code 4:

Here's two example files that generates the error:

Commontest.bas

Code: Select all

#include "test2.bas"
common shared ival  as long
 dim shared ival as long = 10
print mytest(23)
sleep
end
test2.bas

Code: Select all

common shared ival as long

function mytest(inval as long) as long
   
   function  = inval * ival
end function
Whatever I try I get the error 4: Duplicated definition. Have tried common ... in both .bas files. Tried with(and without) dim shared ival as long = 10 first in commontest.bas and then test2.bas. Still error 4.
Where do I go wrong here?
\E
Tourist Trap
Posts: 2958
Joined: Jun 02, 2015 16:24

Re: How to use correct Common Shared syntax?

Post by Tourist Trap »

E Dingsor wrote: Whatever I try I get the error 4: Duplicated definition. Have tried common ... in both .bas files. Tried with(and without) dim shared ival as long = 10 first in commontest.bas and then test2.bas. Still error 4.
Where do I go wrong here?
\E
Hi,

I never used this feature, but one thing is sure to me, when you use #include "...bas", the content of the basic file will be unwrapped right in the same code file before compiling. This means that for FBC, it's just one and same file. In this scenario you will have duplicated definition if you dim twice the same variable in the same scope.

Now, how can we compile the modules all alone and build them in Freebasic. I never tried it. I'm interested by learning the technic.
fxm
Moderator
Posts: 12107
Joined: Apr 22, 2009 12:46
Location: Paris suburbs, FRANCE

Re: How to use correct Common Shared syntax?

Post by fxm »

fxm wrote:To define a variable which is shared between code modules (compiled separately):
.....
If your second module is included in the first module, don't use 'Common', and only define the variable in the included module.
E Dingsor
Posts: 24
Joined: Nov 15, 2017 9:53
Location: Norway

Re: How to use correct Common Shared syntax?

Post by E Dingsor »

Gotcha. Tx!
\E
sero
Posts: 59
Joined: Mar 06, 2018 13:26
Location: USA

Re: How to use correct Common Shared syntax?

Post by sero »

I was having this problem with my own code and in searching for an explanation I came across this recent post. Here is a simple solution by placing shared ival before #include as well as removing it from test2.bas:

Commontest.bas

Code: Select all

dim shared ival  as long = 10
#include "test2.bas"

print mytest(23)
sleep
end
test2.bas

Code: Select all

function mytest(inval as long) as long
   
   function  = inval * ival
end function
fxm
Moderator
Posts: 12107
Joined: Apr 22, 2009 12:46
Location: Paris suburbs, FRANCE

Re: How to use correct Common Shared syntax?

Post by fxm »

I prefer this following:

Commontest.bas

Code: Select all

#include "test2.bas"

ival = 10
print mytest(23)
sleep
test2.bas

Code: Select all

dim shared ival  as long

function mytest(inval as long) as long
   
   function  = inval * ival
end function
sero
Posts: 59
Joined: Mar 06, 2018 13:26
Location: USA

Re: How to use correct Common Shared syntax?

Post by sero »

I think your approach leads to cleaner and better organization. I'm sure I learned some bad habits from my high school days learning VB 6.
fxm
Moderator
Posts: 12107
Joined: Apr 22, 2009 12:46
Location: Paris suburbs, FRANCE

Re: How to use correct Common Shared syntax?

Post by fxm »

It would be best to dispense with global variables.
badidea
Posts: 2591
Joined: May 24, 2007 22:10
Location: The Netherlands

Re: How to use correct Common Shared syntax?

Post by badidea »

namespace would be a good to use here? To keep thing orderly?
E.g.:

Code: Select all

namespace mymath

	dim shared ival as long

	function mytest(inval as long) as long
		return inval * ival
	end function

end namespace

Code: Select all

#include "mymath.bas"

mymath.ival = 10
print mymath.mytest(23)
sleep
sero
Posts: 59
Joined: Mar 06, 2018 13:26
Location: USA

Re: How to use correct Common Shared syntax?

Post by sero »

I just looked up namespace in the FreeBasic help document and I'm sorry I hadn't come across it before. However, I don't feel any of this provides a correct example for using common shared unless the intention was to persuade anyone from using that. I can't get the common examples from the FreeBasic help document to compile. I get an error trying to compile it and I also noticed there is no #include to link the example *.bas files in the help document.
badidea
Posts: 2591
Joined: May 24, 2007 22:10
Location: The Netherlands

Re: How to use correct Common Shared syntax?

Post by badidea »

I see your point, the example on this page (https://freebasic.net/wiki/wikka.php?wakka=KeyPgCommon) is unclear. Not sure myself how it should work.
speedfixer
Posts: 606
Joined: Nov 28, 2012 1:27
Location: CA, USA moving to WA, USA
Contact:

Re: How to use correct Common Shared syntax?

Post by speedfixer »

If all the files will be included into one source, then compiled, only one SHARED is needed.

So, if your 'main' code includes the others, only one SHARED - and that must appear above its use.

If you compile to separate modules, then include those modules, EACH will need the declaration - as a COMMON SHARED to allow the definition to be matched across the modules by the compiler.

david
sero
Posts: 59
Joined: Mar 06, 2018 13:26
Location: USA

Re: How to use correct Common Shared syntax?

Post by sero »

What exactly do you mean by "compile to separate modules?"
Post Reply