Page 1 of 1

A macro for exporting functions!

Posted: Thu Nov 15, 2007 1:27 pm
by srod
Hi,

here are 2 simple macros which I just knocked up to get me out of a hole!

Basically, one of my own user libraries contains a whole bunch of exported functions -as per usual! :) A second library in development needs to use these same functions and for various reasons I need to use the original library as a source code include file rather than simply appealing to the original user library and import the functions etc. Indeed I only ever use source code includes where possible anyhow! :)

No problem you say.

Well, in order to avoid compiler errors when both libraries are installed on a machine, I have to ensure that the previously exported functions being included into the source of the new library are NOT exported again, i.e. they do NOT use ProcedureDLL.

Well, no great deal doing a search and replace to replace the offending ProcedureDLL's with Procedure etc. except that I will effectively be using two versions of the same source with this method and that's not good.

The macros below sort this mess out and without me having to maintain two different versions of the original source etc.

I know that it is unlikely that anyone else will find themselves in this position, but, well if it's useful for someone else then all well and good! :)

***EDIT : my amateur macros have been replaced by Tinman's far more slicker versions. Thanks Tinman!

Code: Select all

;The following two macros allow you to easily switch between creating a user library in 
;which certain procedures must be exported and using your source as an include file in 
;which you probably do not wish to export any procedures.

;Useful if you are building a second user library based on source ADAPTED from an existing user 
;library in which of course you do not want two versions of the same exported function
;in two different user libraries. This would cause compiler errors.

;Thanks to Tinman.

;******Uncomment the following constant in order to export the targeted procedures******.
  ;#MySourceCode_BuildUserLibrary = 1


CompilerIf Defined(MySourceCode_BuildUserLibrary, #PB_Constant) 
  Macro DeclareProc : DeclareDLL : EndMacro 
  Macro Proc : ProcedureDLL : EndMacro 
CompilerElse 
  Macro DeclareProc : Declare : EndMacro 
  Macro Proc : Procedure : EndMacro 
CompilerEndIf 



;============================================TEST 1===================================
;No return from procedure so .l is assumed.

;The procedure 'TestProc(a, b)' will be exported only if the constant 
;#MySourceCode_BuildUserLibrary is defined by uncommenting the line at the top of this file.
;Otherwise, it will remain as an internal function.

  DeclareProc TestProc(a, b)
  Debug TestProc(100, 50)

  Proc TestProc(a ,b)
    ProcedureReturn a+b
  EndProcedure
;=====================================================================================

;============================================TEST 2===================================
;String return.

;The procedure 'StringProc.s(a$, b$)' will be exported only if the constant 
;#MySourceCode_BuildUserLibrary is defined by uncommenting the line at the top of this file.
;Otherwise, it will remain as an internal function.

  DeclareProc.s StringProc(a$, b$)
  Debug StringProc("Hello", "Matey!")

  Proc.s StringProc(a$ ,b$)
    ProcedureReturn a$ + " " + b$
  EndProcedure
;=====================================================================================
If you need to use c style functions then you just need to adjust the macros as appropriate.

Regards.

Posted: Thu Nov 15, 2007 2:19 pm
by tinman
Would it not have been possible to do something like this, a lot like yours but cleaner (IMHO):

Code: Select all

CompilerIf Defined(MySourceCode_BuildUserLibrary, #PB_Constant)
  Macro DeclareImpExpProc : DeclareDLL : EndMacro
  Macro ImpExpProc : ProcedureDLL : EndMacro
CompilerElse
  Macro DeclareImpExpProc : Declare : EndMacro
  Macro ImpExpProc : Procedure : EndMacro
CompilerEndIf 

;#MySourceCode_BuildUserLibrary = 1

DeclareImpExpProc.s StringProc(a$, b$)
Debug StringProc("Hello", "Matey!") 

ImpExpProc.s StringProc(a$ ,b$)
  ProcedureReturn a$ + " " + b$ 
EndProcedure 
More like the original syntax, but I can't test whethe rit works just now.

Posted: Thu Nov 15, 2007 2:33 pm
by srod
That's a damn sight better than mine! :wink:

That's my inexperience with macro's shining through!

I'll remove my code and point people to yours.

Thanks a lot Tinman.