' ------------------------------------------------------------
' Code generated automatically by Code Architects' VB Migration Partner
' ------------------------------------------------------------

Option Strict Off      ' Code migrated from VB6 has Option Strict disabled by default

Imports VB = Microsoft.VisualBasic

'TransactionType enum
Public Enum FMStocks_TransactionType
	Pending_Buy = 1
	Pending_Sell = 2
	Bought = 3
	Sold = 4
	Insufficient_Funds = 5
	Not_Enough_Shares = 6
End Enum

Friend Module Helpers

	'Debug String Messages
	Public Const cstrStarting_Method As String = "Starting Method call"
	Public Const cstrFinishing_Method As String = "Finishing Method call"
	Public Const cstrError_Method As String = "Error in Method call"
	
	'WIN32 API Functions used to write to EventLog and get the Computername
	Declare Function RegisterEventSource Lib "advapi32.dll" Alias "RegisterEventSourceA" (ByVal lpUNCServerName As String, ByVal lpSourceName As String) As Integer
	Declare Function DeregisterEventSource Lib "advapi32.dll" (ByVal hEventLog As Integer) As Integer
	' IGNORED: Declare Function ReportEvent Lib "advapi32.dll" Alias  "ReportEventA" (ByVal hEventLog As Long, ByVal wType As Integer,  ByVal wCategory As Integer, ByVal dwEventID As Long,  ByVal lpUserSid As Any, ByVal wNumStrings As Integer,  ByVal dwDataSize As Long, plpStrings As Long,  lpRawData As Any) As Boolean
	Declare Function ReportEvent Lib "advapi32.dll" Alias "ReportEventA" (ByVal hEventLog As Integer, ByVal wType As Short, ByVal wCategory As Short, ByVal dwEventID As Integer, ByVal lpUserSid As Integer, ByVal wNumStrings As Short, ByVal dwDataSize As Integer, ByRef plpStrings As Integer, ByRef lpRawData As Integer) As Boolean
	' UPGRADE_INFO (#01F1): References to the 'GetLastError' Declare have been replaced by calls to the System.Runtime.InteropServices.Marshall.GetLastWin32Error method. Check that this Declare is useless and then delete it.
	Declare Function GetLastError Lib "kernel32" () As Integer
	' IGNORED: Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" (  hpvDest As Any, hpvSource As Any,  ByVal cbCopy As Long)
	Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" (ByVal hpvDest As Integer, ByVal hpvSource As String, ByVal cbCopy As Integer)
	Declare Function GlobalAlloc Lib "kernel32" (ByVal wFlags As Integer, ByVal dwBytes As Integer) As Integer
	Declare Function GlobalFree Lib "kernel32" (ByVal hMem As Integer) As Integer
	Private Declare Function GetComputerNameAPI Lib "kernel32" Alias "GetComputerNameA" (ByVal lpBuffer As String, ByRef nSize As Integer) As Integer
	
	'Event Constants
	Public Const EVENTLOG_SUCCESS As Short = 0
	Public Const EVENTLOG_ERROR_TYPE As Short = 1
	Public Const EVENTLOG_WARNING_TYPE As Short = 2
	Public Const EVENTLOG_INFORMATION_TYPE As Short = 4
	Public Const EVENTLOG_AUDIT_SUCCESS As Short = 8
	Public Const EVENTLOG_AUDIT_FAILURE As Short = 10
	
	Public Sub LogNTEvent(ByVal sString As String, ByVal iLogType As Short, ByVal iEventID As Integer, ByVal sEventTitle As String)
		
		Dim bRC As Boolean
		Dim iNumStrings As Short = 1
		Dim hEventLog As Integer
		Dim hMsgs As Integer
		Dim cbStringSize As Integer
		
		hEventLog = RegisterEventSource("", sEventTitle)
		cbStringSize = Len6(sString) + 1
		hMsgs = GlobalAlloc(&H40, cbStringSize)
		
		' UPGRADE_ISSUE (#0218): The ByVal keyword was used to pass the 'hMsgs' argument. Consider creating an overload of the Declare statement.
		' UPGRADE_ISSUE (#0218): The ByVal keyword was used to pass the 'sString' argument. Consider creating an overload of the Declare statement.
		CopyMemory(hMsgs, sString, cbStringSize)

		Call ReportEvent(hEventLog, iLogType, 0, iEventID, 0%, iNumStrings, cbStringSize, hMsgs, hMsgs)
		
		Call GlobalFree(hMsgs)
		Call DeregisterEventSource(hEventLog)
		
	End Sub

	' mp is short for MakeParameter - does typesafe array creation for use with Run* functions
	Public Function mp(ByVal PName As String, ByVal PType As ADODB.DataTypeEnum, ByVal PSize As Integer, ByVal PValue As Object) As Object
		Return Array6(PName, PType, PSize, PValue)
	End Function

	'Converts a variant into a string. If the varaint is null, it is converted to the empty string.
	Public Function ConvertToString(ByRef v As Object) As String
		If IsNull6(v) Then 
			Return ""
		Else
			Return CStr(v)
		End If
	End Function

	'Converts any Null variant to null, otherwise it returns the existing value.
	Public Function NullsToZero(ByRef v As Object) As Object
		If IsNull6(v) Then 
			Return 0
		Else
			Return v
		End If
	End Function

	'Raise an error without changing the transaction state.
	Public Sub RaiseError(ByVal [module] As String, ByVal functionName As String, Optional ByVal strErrDesc As String = "", Optional ByVal lngErrNum As Integer = 0, Optional ByRef bWriteToLog As Boolean = False)
		'log the error to the event for later use.
		Dim strSource As String = ""
		
		If strErrDesc = "" Then  strErrDesc = Err.Description
		If lngErrNum = 0 Then  lngErrNum = Err.Number
		
		strSource = CreateSourceStack([module], functionName)
		
		'add custom logging here... for now we log to application event log
		If bWriteToLog Then  logError(lngErrNum, strSource, strErrDesc)
		
		Err.Raise(lngErrNum, strSource, strErrDesc)
	End Sub

	Public Function GetComputerName() As String
		' Set or retrieve the name of the computer.
		Dim strBuffer As String = ""
		Dim lngLen As Integer
		
		strBuffer = Space(255 + 1)
		lngLen = Len6(strBuffer)
		If CBool(GetComputerNameAPI(strBuffer, lngLen)) Then 
			Return VB.Left(strBuffer, lngLen)
		Else
			Return ""
		End If
	End Function

	Private Function CreateSourceStack(ByVal objectName As String, ByVal procName As String) As String
		' Returns an source stack like message like:  "[FMStocks_DB.Account] VerifyUser [on AHI version 5.21.176]"
		' add tab to each line in the  new source stack
		
		Return Replace(Err.Source & ControlChars.NewLine & "[" & objectName & "]  " & procName & " [" & GetComputerName() & " version " & GetVersionNumber() & "]", ControlChars.NewLine, ControlChars.Tab & ControlChars.NewLine)
	End Function

	'returns the current DLL version number
	Public Function GetVersionNumber() As String
		Return App6.Major & "." & App6.Minor & "." & App6.Revision
	End Function

	'function takes a string with an '=' in it and returns the left part
	'GetKey("test=1") return test
	Public Function GetKey(ByVal strItem As String) As String
		Dim i As Short = InStr(1, strItem, "=", CompareMethod.Text)
		
		If i > 0 Then 
			Return VB.Left(strItem, i - 1)
		Else
			Return ""
		End If
	End Function

	'function takes a string with an '=' in it and returns the right part
	'GetValue("test=1") return 1
	Public Function GetValue(ByVal strItem As String) As String
		Dim i As Short = InStr(1, strItem, "=", CompareMethod.Text)
		
		If i > 0 Then 
			Return Mid(strItem, 1 + i)
		Else
			Return ""
		End If
	End Function

	'this procedure logs an error to the system application log
	Private Sub logError(ByVal errnum As Integer, ByVal source As String, ByVal description As String, Optional ByRef notes As String = "")
		
		' UPGRADE_INFO (#8001): When using the LogEvent method to log to the Windows EventLog, the source name will be '.NET Application' instead of 'VBRuntime' as in Visual Basic 6.
		App6.LogEvent(ControlChars.NewLine & "An error has occured in an FMStocks 2000 component." & ControlChars.NewLine & "Error Number:" & errnum & ControlChars.NewLine & ControlChars.NewLine & "Source: " & ControlChars.NewLine & source & ControlChars.NewLine & ControlChars.NewLine & "Description:" & ControlChars.NewLine & description & ControlChars.NewLine & ControlChars.NewLine & "Notes: " & ControlChars.NewLine & notes)
		
	End Sub
	
	'this procedure logs an error to the system application log
	Private Sub logEvent(ByVal source As String, ByVal description As String, Optional ByVal notes As String = "")
		
		' UPGRADE_INFO (#8001): When using the LogEvent method to log to the Windows EventLog, the source name will be '.NET Application' instead of 'VBRuntime' as in Visual Basic 6.
		App6.LogEvent(ControlChars.NewLine & "FMStocks 2000 Debug event." & ControlChars.NewLine & "Source: " & ControlChars.NewLine & source & ControlChars.NewLine & ControlChars.NewLine & "Description:" & ControlChars.NewLine & description & ControlChars.NewLine & ControlChars.NewLine & "Notes: " & ControlChars.NewLine & notes)
		
	End Sub
	
	Public Sub logDebugMSG(ByVal bWriteMSG As Boolean, ByVal strEvent As String, ByVal strSource As String, Optional ByVal strMSG As String = "")
		
		If bWriteMSG Then  logEvent(strEvent & " -- " & strSource, "Debug event from " & strSource, strMSG)
		
	End Sub

End Module
