我现在急需使用导入的一个dll类型库,但其中的com接口不知如何使用。该组件库有一个VB的例子,使用用了类模块定义用户函数,没有什么问题,可在Delphi下如何使用就不明白。请教一下高手帮忙解决一下,万分感谢!!!(我的Email地址:[email protected])。VB的例子如下:
主程序:
Private iEx As USPEXPRESSPARSER.USPExpression'=========================================================
' Purpose: Parses and Evaluates an expression
'=========================================================
Private Function CalculateExpression(sExpression As String, bIsLinear As Boolean) As Double
Dim sVariables(1 To 3) As String
Dim dValues(1 To 3) As Double
Dim lIndex As Integer
Dim lErrCode As Integer sVariables(1) = "X1"
sVariables(2) = "X2"
sVariables(3) = "X3"
dValues(1) = 35
dValues(2) = 2
dValues(3) = 3
lIndex = iEx.Parse(sExpression, sVariables, lErrCode)
If lErrCode = USPEX_NO_ERROR Then
' find out, if it is linear
bIsLinear = iEx.IsLinear
On Error GoTo EvaluationError_
CalculateExpression = iEx.Evaluate(dValues)
ElseIf lErrCode = USPEX_UNSUPPORTED_IN_TRIAL_VERSION Then
MsgBox "TRIAL version of USPExpress supports multiplication (*) and addition (+) operators only.", vbInformation
Else
' Parsing error handler
MsgBox "Invalid expression, character #" & lIndex & "." & vbCrLf & "Error #: " & lErrCode, vbExclamation
End If
Exit Function
EvaluationError_:
' Evaluation error handler
MsgBox Err.Description
End FunctionPrivate Sub btnCalculate_Click()
Dim bIsLinear As Boolean
Me.txtResult = CalculateExpression(Me.txtExpression, bIsLinear)
lblLinear.Caption = IIf(bIsLinear, "Linear", "Non-linear")
End SubPrivate Sub Form_Initialize()
Set iEx = New USPExpression
' --- Add user defined functions, if any
Dim oMyFunction As New CmyFunction
iEx.UserFunctions.Add oMyFunctionEnd Sub
Private Sub Form_Terminate()
Set iEx = Nothing
End SubPrivate Sub Form_Load()
Me.txtExpression = "IIF(X1, 2*SQRT(X3), 4*SUM(1,X2,X3)*power(x1,x2)) + CircleArea(X2)"
Me.txtResult = ""
End Sub
类模块如下:
Option ExplicitImplements USPEXPRESSPARSER.IUSPUserFunction'============================================================================
' This function calculates the square of the circle
'============================================================================
Private Function IUSPUserFunction_Evaluate(varParameters As Variant) As Double
IUSPUserFunction_Evaluate = 3.14 * varParameters(1) * varParameters(1)
End Function'============================================================================
' The name of the function
'============================================================================
Private Property Get IUSPUserFunction_Name() As String
IUSPUserFunction_Name = "CircleArea"
End Property'============================================================================
' Number of parameters of the function
'============================================================================
Private Property Get IUSPUserFunction_NParameters() As Long
IUSPUserFunction_NParameters = 1
End Property
主程序:
Private iEx As USPEXPRESSPARSER.USPExpression'=========================================================
' Purpose: Parses and Evaluates an expression
'=========================================================
Private Function CalculateExpression(sExpression As String, bIsLinear As Boolean) As Double
Dim sVariables(1 To 3) As String
Dim dValues(1 To 3) As Double
Dim lIndex As Integer
Dim lErrCode As Integer sVariables(1) = "X1"
sVariables(2) = "X2"
sVariables(3) = "X3"
dValues(1) = 35
dValues(2) = 2
dValues(3) = 3
lIndex = iEx.Parse(sExpression, sVariables, lErrCode)
If lErrCode = USPEX_NO_ERROR Then
' find out, if it is linear
bIsLinear = iEx.IsLinear
On Error GoTo EvaluationError_
CalculateExpression = iEx.Evaluate(dValues)
ElseIf lErrCode = USPEX_UNSUPPORTED_IN_TRIAL_VERSION Then
MsgBox "TRIAL version of USPExpress supports multiplication (*) and addition (+) operators only.", vbInformation
Else
' Parsing error handler
MsgBox "Invalid expression, character #" & lIndex & "." & vbCrLf & "Error #: " & lErrCode, vbExclamation
End If
Exit Function
EvaluationError_:
' Evaluation error handler
MsgBox Err.Description
End FunctionPrivate Sub btnCalculate_Click()
Dim bIsLinear As Boolean
Me.txtResult = CalculateExpression(Me.txtExpression, bIsLinear)
lblLinear.Caption = IIf(bIsLinear, "Linear", "Non-linear")
End SubPrivate Sub Form_Initialize()
Set iEx = New USPExpression
' --- Add user defined functions, if any
Dim oMyFunction As New CmyFunction
iEx.UserFunctions.Add oMyFunctionEnd Sub
Private Sub Form_Terminate()
Set iEx = Nothing
End SubPrivate Sub Form_Load()
Me.txtExpression = "IIF(X1, 2*SQRT(X3), 4*SUM(1,X2,X3)*power(x1,x2)) + CircleArea(X2)"
Me.txtResult = ""
End Sub
类模块如下:
Option ExplicitImplements USPEXPRESSPARSER.IUSPUserFunction'============================================================================
' This function calculates the square of the circle
'============================================================================
Private Function IUSPUserFunction_Evaluate(varParameters As Variant) As Double
IUSPUserFunction_Evaluate = 3.14 * varParameters(1) * varParameters(1)
End Function'============================================================================
' The name of the function
'============================================================================
Private Property Get IUSPUserFunction_Name() As String
IUSPUserFunction_Name = "CircleArea"
End Property'============================================================================
' Number of parameters of the function
'============================================================================
Private Property Get IUSPUserFunction_NParameters() As Long
IUSPUserFunction_NParameters = 1
End Property
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货