logo.jpg (4128 bytes)
Home
Main Page
Links
Classical
Popular
Programming
Shareware
Emulation
Opus
Gtr & Fl.
Concerto
Code
Status bar
Math
Perl
Base Address
Meta Script
DHTML
WSC
Programs
Markup
Clipboard

 

Random Base Address Generator

When writing in-process components in Visual Basic you must specify a base address different from the default provided of &H11000000 (285, 212, 672) to avoid memory conflicts. If the memory location used by an in-process component conflict with memory locations used by other in-process components or by the executable, the component must be rebased to another logical memory location in the executable's process space.

Rebasing requires the operating system to dynamically recalculate the logical memory locations where the code and data are loaded. This recalculation slows down the load process, and code that is dynamically relocated generally cannot be shared between executables.

Choose a base address between 16 megabytes (16, 777, 216 or &H1000000) and two gigabytes (2, 147, 483, 648 or &H80000000). The base address must be a multiple of 64K.

A great way to create a base address is with a random number generator. Below is a script for Windows Scripting Host written in Visual Basic Script that generates a random number within the appropriate range. It first prompts for the size of the component then using that figure it creates a random number. It displays the new number in a message box and offers the option to copy it to the Windows Clipboard, making use of Clipboard For Scripting-an ActiveX component of my own creation. Finally it offers to add this number to a text file containing a list previously generated numbers for your records. If this file doesn't exist it will create it for you.

Note: For this script to work as is you must install the ClipboardForScript component. This is for the Windows Clipboard functionality. If you choose not to install it you must remove all references in the script to it. This includes the <reference /> element and the copying segment near the end.

You can download the script below:

arrow.jpg (1679 bytes) Base Address Generator

The code is below:

<job>
<reference object="ClipboardForScript.TextClipboard" />
<script language="VBScript">

	Option Explicit

Public Function GetBaseAddress()

	Const SixteenMB = 16777216
	Const TwoGB = 2147483648
	Const Sixty4K =  65536
	Dim nReturn
	Dim nMultiple
	Dim nSizeOf

	' Ask the User for the size in kilobytes of the component
	nSizeOf = InputBox("Enter the Size of your component in Kilobytes.", "Base Address Generator")

	' Do some simple Error prevention.
	If IsNumeric(nSizeOf) Then
		If nSizeOf > 0 Then
			nSizeOf = nSizeOf * 1024
		Else
			MsgBox "Your component must be larger than 0 kilobytes. Try again smarty-pants.", vbOKOnly + vbExclamation, "Base Address Generator"
			WScript.Quit
		End If
	Else
		MsgBox "Kilobytes are numbers jack ass!", vbOKOnly + vbExclamation, "Base Address Generator"
		WScript.Quit
	End If

	' Generate a random Number between 16 megabytes And two gigabytes minus the size
	' of the memory used by the component.
	Randomize 
	nReturn = Rnd
	nReturn = Int((((TwoGB - nSizeOf) - SixteenMB) + 1) * Rnd + SixteenMB)

	' The Number must be able to round up to a multiple of 64K
	If nReturn > (TwoGB - Sixty4K) Then
		While nReturn > (TwoGB - Sixty4K)
			Randomize
			nReturn = Rnd
			nReturn = Int((((TwoGB - nSizeOf) - Sixty4K) + 1) * Rnd + SixteenMB)
		Wend
	End If
	nMultiple = Int((nReturn / Sixty4K) + 1)
	nReturn = Sixty4K * nMultiple

	GetBaseAddress = "&H" & Hex(nReturn)

End Function

</script>
<script language="VBScript">

'****************************************************************************************
'*	Generate a Random Number used For an in-process component's Base Address	*
'****************************************************************************************

	Option Explicit

	Const ForAppending = 8
	Const TristateUseDefault = -2
	Dim nAddress
	Dim intresponse
	Dim clsClipboard : Set clsClipboard = CreateObject("ClipboardForScript.TextClipboard")
	Dim fsoSys : Set fsoSys = CreateObject("Scripting.FileSystemObject")
	Dim tsoFile : Set tsoFile = fsoSys.OpenTextFile("C:\Windows\Desktop\Scripts\prev_addresses.txt", ForAppending, True, TristateUseDefault)

	' Retrieve the Base address
	nAddress = GetBaseAddress()

	' Display it to the User And ask to Copy it to the Clipboard using the
	' ClipboardForScript.TextClipboard component.
	intResponse = MsgBox("Your new Base address is:" & vbCrLf & nAddress & vbCrLf & "Copy to Clipboard?", vbYesNo + vbQuestion, "Base Address Generator")
	If intResponse = vbYes Then
		clsClipboard.SetText CStr(nAddress), cfsCFText
	End If

	' Ask the User to Add the new address to a list of previously generated addresses.
	intResponse = MsgBox("Do you want to Add this address to your list of previously generated addresses?", vbYesNo + vbQuestion + vbDefaultButton1, "Base Address Generator")
	If intResponse = vbYes Then
		tsoFile.WriteLine CStr(nAddress)
		tsoFile.Close
	End If
	
	Set tsoFile = Nothing
	Set fsoSys = Nothing
	Set clsClipboard = Nothing

</script>
</job>

 

Aaron L. Stephanus Do YOU want YOUR choice of a FREE laptop ?