Skip to content

Using Without Registration

Michael Waite edited this page Mar 16, 2026 · 6 revisions

The ActiveX DLL can be called from your VBA projects without registration. You can find the 32- and 64-bit versions of the DLL's in the https://github.com/GCuser99/SolverWrapper/tree/main/Build folder of this repository or generate your own builds from the .twinproj file.

THIS IS NOT THE RECOMMENDED SETUP, but is shown here just to show the possibilities. If you go the non-registration route, you will not have access to IntelliSense nor the Object Browser from VBA.

These are declarations that are needed in your VBA project to run twinBASIC DLL without registration:

Option Explicit

Public Declare PtrSafe Function New_SolvProblem Lib "[Path to DLL]\SolverWrapper_win64.dll" () As Object
'Public Declare PtrSafe Function New_SolvProblem Lib "[Path to DLL]\SolverWrapper_win32.dll" () As Object

    Public Enum SlvGoalType
        slvMaximize = 1
        slvMinimize = 2
        slvTargetValue = 3
        [_First] = 1
        [_Last] = 3
    End Enum
    
    Public Enum SlvShowTrial
        slvContinue = 0
        slvStop = 1
        [_First] = 0
        [_Last] = 1
    End Enum
    
    Public Enum SlvEstimates
        slvTangent = 1
        slvQuadratic = 2
        [_First] = 1
        [_Last] = 2
    End Enum

    Public Enum SlvDerivatives
        slvForward = 1
        slvCentral = 2
        [_First] = 1
        [_Last] = 2
    End Enum
    
    Public Enum SlvSearchOption
        slvNewton = 1
        slvConjugate = 2
        [_First] = 1
        [_Last] = 2
    End Enum
    
    Public Enum SlvRelation
        slvLessThanEqual = 1
        slvEqual = 2
        slvGreaterThanEqual = 3
        slvInt = 4
        slvBin = 5
        slvAllDif = 6
        [_First] = 1
        [_Last] = 6
    End Enum
    
    Public Enum SlvSolveMethod
        slvGRG_Nonlinear = 1
        slvSimplex_LP = 2
        slvEvolutionary = 3
        [_First] = 1
        [_Last] = 3
    End Enum
    
    Public Enum SlvCallbackReason
        slvShowIterations = 1
        slvMaxTimeLimit = 2
        slvMaxIterationsLimit = 3
        slvMaxSubproblemsLimit = 4
        slvMaxSolutionsLimit = 5
        [_First] = 1
        [_Last] = 5
    End Enum
    
    Public Enum SlvMsgCode
        slvFoundSolution = 0
        slvConvergedOnSolution = 1
        slvCannotImproveSolution = 2
        slvMaxIterReached = 3
        slvObjectiveNotConvergent = 4
        slvCouldNotFindSolution = 5
        slvStoppedByUser = 6
        slvProblemNotLinear = 7
        slvProblemTooLarge = 8
        slvErrorInObjectiveOrConstraint = 9
        slvMaxTimeReached = 10
        slvNotEnoughMemory = 11
        slvNoDocumentation = 12
        slvErrorInModel = 13
        slvFoundIntegerSolution = 14
        slvMaxSolutionsReached = 15
        slvMaxSubProblemsReached = 16
        slvConvergedToGlobalSolution = 17
        slvAllVariablesMustBeBounded = 18
        slvBoundsConflictWithBinOrAllDif = 19
        slvBoundsAllowNoSolution = 20
        [_First] = 0
        [_Last] = 20
    End Enum
    
    Private Enum SolverMode
        SolveMode = 0
        CloseMode = 1
        CancelRestoreMode = 2
        [_First] = 0
        [_Last] = 2
    End Enum

The example below automates solving the problem in SOLVSAMP.XLS on the "Portfolio of Securities" worksheet.

SOLVSAMP.XLS is distributed with MS Office Excel and can be found in:

Application.LibraryPath & "\..\SAMPLES\SOLVSAMP.XLS"

which on many systems can be found here:

C:\Program Files\Microsoft Office\root\Office16\SAMPLES\SOLVSAMP.XLS

Copy the code below into the sample workbook and then save SOLVSAMP.XLS to SOLVSAMP.XLSM.

Sub Solve_Portfolio_of_Securities()
    Dim oProblem As Object
    Dim ws As Worksheet

    Set oProblem = New_SolvProblem
    
    Set ws = ThisWorkbook.Worksheets("Portfolio of Securities")
    
    'Step 1: initialize the problem by passing a reference to the worksheet of interest
    oProblem.Initialize ws
    
    'Step 2: define the objective cell to be optimized
    oProblem.Objective.Define "E18", slvMaximize
    
    'Step 3: add and initialize the decision cell(s)
    oProblem.DecisionVars.Add "E10:E14"
    oProblem.DecisionVars.Initialize 0.2, 0.2, 0.2, 0.2, 0.2
    
    'Step 4: set the constraints
    With oProblem.Constraints
        .AddBounded "E10:E14", 0#, 1#
        .Add "E16", slvEqual, 1#
        .Add "G18", slvLessThanEqual, 0.071
    End With
    
    'Step 5: set the solver engine to use
    oProblem.Solver.Method = slvGRG_Nonlinear
    
    'Step 6: set solver options
    oProblem.Solver.Options.RandomSeed = 7
    
    oProblem.Solver.SaveAllTrialSolutions = True

    'Step 7: solve the optimization problem
    oProblem.SolveIt
    
    'Step 8: save all trial solutions that passed the constraints to the worksheet for post-processing analysis
    If oProblem.Solver.SaveAllTrialSolutions Then
        ws.Range("o2:az10000").ClearContents
        oProblem.SaveSolutionsToRange ws.Range("o2"), keepOnlyValid:=True
    End If
End Sub

Clone this wiki locally