Checking available system resources

Visual Basic


  • 1. collection and classes
    "greg" < XXXX@XXXXX.COM > wrote in message news: XXXX@XXXXX.COM ... >I am seeing a problem in my code. And I think I know the answer. But want > to confirm. > > I have a very complicated set of classes. Classes within classes. > Classes > that hold other classes. > And I have a collection of the top classes. > I am trying to set this collection in another class. > And I see the data in the collection keep changing on me. > So, > now I am thinking, that I would have to do some kind of deep copy of this > collection. > I currently have in a new class a set method. which you pass this > collection into. > But I am thinking it does not do a deep copy. > does this seem about right? I'm not sure I understand exactly what you are trying to do but doing a Set doesn't do any sort of copy at all, it simply adds another reference to the same object.
  • 2. Deleting a repetative line in a text file
    Hi all, I am trying to delete a repetative line from a text file. I am able to delete a single line, but I am unable to compare the two lines and keep the latest find and then delete the previous one. My text file is in a format below IDnumber~1345435 Package1~false Package2~false Package3~False Package4~True IDnumber~1235353 I need to delete the first IDnumber and retain the second one. Right now I am spliting the line at "~" and storing the value in a string and then trying to compare by the rest of the lines. but I am unable to do this. How will I be able to do this. Thanking you all Best Regards, Nitin
  • 3. Here's one for you rick
    Can you make this into a 1 liner. It calculates the amount that 2 ranges overlap by. Public Function RangesOverlapAmount(ByVal RangeA1 As Long, ByVal RangeA2 As Long, ByVal RangeB1 As Long, ByVal RangeB2 As Long) As Long Dim lngI As Long, lngJ As Long If RangeA1 < RangeB1 Then lngI = RangeB1 Else lngI = RangeA1 End If If RangeA2 < RangeB2 Then lngJ = RangeA2 Else lngJ = RangeB2 End If If lngJ > lngI Then RangesOverlapAmount = lngJ - lngI End If End Function
  • 4. SAP Exchange ?
    Does anyone here have any experience using VB6 to send data to a SAP ERP system using SAP's "Exchange" functionality? I may be having to develop an app that does this and would like to get some idea of what I'm up against. Thanks.
  • 5. How to determine the order that files were put into a folder??
    <snip> > Also, not really a good idea to crosspost to VB scripting and VB newsgroups. > There are enough differences between VB and VBScript that answers you get > may not be applicable. > > -- > Mike > Microsoft Visual Basic MVP I figure since the FileSystemObject class may come into play then the VBScript and VB newsgroups would be appropriate. I've decided to create a queue in memory and continue polling the directory to see if anything's been added. If there are new files I add them into the queue. Thanks, Jeff

Checking available system resources

Postby Jeff » Wed, 19 Nov 2003 04:50:56 GMT

Is there a way to check available system resources prior 
to executing an operation in an application.

I have a mapping application that has an export to JPEG 
function. There are a number of settings with regards to 
resolution and output color palette etc. Often there is a 
limit to what the user can specify before the application 
will crash due to insufficient sytem resources.

Is there anyway to catch this?

Re: Checking available system resources

Postby Veign » Wed, 19 Nov 2003 06:11:07 GMT

Drop a Command button on a Form (Form1) named Command1 and try this;

      dwLength As Long
      dwMemoryLoad As Long
      dwTotalPhys As Long
      dwAvailPhys As Long
      dwTotalPageFile As Long
      dwAvailPageFile As Long
      dwTotalVirtual As Long
      dwAvailVirtual As Long
End Type

Private Enum MemoryType
End Enum

Private Declare Sub GlobalMemoryStatus Lib "kernel32" _
            (lpBuffer As MEMORYSTATUS)

Private Function SystemMemory(ByVal enMemType As MemoryType) As Double

Dim lngMemory As Long

'Retrieve all memory values
GlobalMemoryStatus memsts

Select Case enMemType
    Case MemoryType.AvailablePhysical: lngMemory = memsts.dwAvailPhys
    Case MemoryType.AvailableVirtual:  lngMemory = memsts.dwAvailVirtual
    Case MemoryType.AvailablePageFile: lngMemory = memsts.dwAvailPageFile
    Case MemoryType.TotalPhysical:     lngMemory = memsts.dwTotalPhys
    Case MemoryType.TotalVirtual:      lngMemory = memsts.dwTotalVirtual
    Case MemoryType.TotalPageFile:     lngMemory = memsts.dwTotalPageFile
End Select

'Return the memory
SystemMemory = lngMemory

End Function

Public Function AvailableMemory() As Double
    'Return Value in Megabytes
     AvailableMemory = SystemMemory(AvailablePhysical) +
End Function

Public Function TotalMemory() As Double
    'Return Value in Megabytes
    TotalMemory = SystemMemory(TotalPageFile) + SystemMemory(TotalPhysical)
End Function

Public Function PercentMemoryFree() As Double

    PercentMemoryFree = Format(AvailableMemory / TotalMemory * 100, "0#")

End Function

Private Function BytesToMegabytes(Bytes As Double) As Double

  Dim dblAns As Double
  dblAns = (Bytes / 1024) / 1024
  BytesToMegabytes = Format(dblAns, "###,###,##0.00")

End Function

Public Function DisplayMemory()

Dim strMemory As String
strMemory = "Total Physical: " &
BytesToMegabytes(SystemMemory(TotalPhysical)) & "M" & vbNewLine & _
            "Available Physical: " &
BytesToMegabytes(SystemMemory(AvailablePhysical)) & "M" & vbNewLine & _
            "Total Memory: " & BytesToMegabytes(TotalMemory) & "M" &
vbNewLine & _
            "Available Memory: " & BytesToMegabytes(AvailableMemory) & "M" &
vbNewLine & vbNewLine & _
            "Percent Free: " & PercentMemoryFree & "%"

Form1.Print strMemory

End Function

Private Sub Command1_Click()


End Sub

Chris Hanscom
MVP (Visual Basic)
Application Design Section

Re: Checking available system resources

Postby Larry Serflaten » Wed, 19 Nov 2003 06:41:50 GMT

"Veign" < XXXX@XXXXX.COM > wrote

I just thought I would mention something that struck me as odd:

What are the # and commas for in the Format command?

FWIW, this does the same thing...

Private Function BytesToMegabytes(Bytes As Double) As Double
Const mega = 2 ^ 20
  BytesToMegabytes = Format(Bytes / mega, "0.00")
End Function


Re: Checking available system resources

Postby Ben Taylor » Wed, 19 Nov 2003 20:45:11 GMT

For a start, you're returning a double, yet you're 
returning the return value of the Format function! WHY?!
Secondly, Format(Bytes / mega, "0.00") does NOT put commas 

Either use

Private Function BytesToMegabytes(Bytes As Double) As 
   Dim dblAns As Double
   dblAns = (Bytes / 1024) / 1024
   BytesToMegabytes = Format$(dblAns, "###,###,##0.00")
End Function


Private Function BytesToMegabytes(Bytes As Double) As 
   BytesToMegabytes = (Bytes / 1024) / 1024
End Function

you're just crap.

as odd:

Re: Checking available system resources

Postby Ben Taylor » Wed, 19 Nov 2003 20:46:12 GMT

>What are the # and commas for in the Format command?

They are necessary because some people like to see 
thousands separators.

Re: Checking available system resources

Postby Larry Serflaten » Wed, 19 Nov 2003 21:31:25 GMT

"Ben Taylor" < XXXX@XXXXX.COM > wrote

Do you know what look before you leap means?  In this context it means
you should test your theories and verify their results before you offer them
up as fact.  What I posted returns the exact same results as the originally
posted function.  Both of your suggestions return different results than the
original routine.  In that respect:

Is completely correct, regaurdless of whether or not the returned value
was the desired result.

There is no reason to respond favorably to someone who continually
puts out bad information.  No amount of correction seems to get you
to stop your folly.  As a result, I have no desire to offer advice to
someone who has no willingness to learn from his mistakes.  I can
only hope that the people that read your replies will know enough to
consider the source.


Re: Checking available system resources

Postby Al Reid » Wed, 19 Nov 2003 21:51:10 GMT



Since the function is returning a Double, the conversion will remove the formatting.

Try both of the following in the immediate window.


How many commas are there in the results?  Neither is formatting the results to display the thousands separator.

"It ain't what you don't know that gets you into trouble. It's what you know
for sure that just ain't so."  --- Mark Twain

Re: Checking available system resources

Postby Veign » Wed, 19 Nov 2003 23:57:25 GMT

    Your right - Should be returning a string to maintain the comma's or
change the format.....

Chris Hanscom
MVP (Visual Basic)
Application Design Section

Re: Checking available system resources

Postby Ben Taylor » Thu, 20 Nov 2003 21:04:53 GMT

So it was Serflaten's returning of a double that was 
wrong, not my assertion of the reason behind the ###,##.
The function should have returned a string, as I indicated.

Re: Checking available system resources

Postby Ben Taylor » Thu, 20 Nov 2003 21:14:00 GMT

I would have corrected that, had I been in your shoes. 
Posting an edit of a function that tries to return a 
string when it actually returns a double is downright 
sloppy. Posting an edit of a function that tries to return 
a string when it actually returns a double, when you're a 
respected member of the newsgroup whose posts are taken as 
gospel, is downright CRAP, Larry, I'm sorry. 
The fact that it doesn't error doesn't make a blind bit of 
differene - it's {*filter*}code.
No matter how many of your silly sayings you utter and how 
much you put me down, that was CRAP, *total* crap, utter 
{*filter*}sloppiness that would only have been expected of a 
third-rate programmer that doesn't know one datatype from 
Your slagging off of my reputation in an attempt to defend 
your own sloppiness is the act of a coward.

context it means
before you offer them
as the originally
different results than the
returned value
to get you
offer advice to
mistakes.  I can
know enough to

Re: Checking available system resources

Postby Al Reid » Thu, 20 Nov 2003 21:29:01 GMT


WRONG AGAIN.  It was Veign's original routine that returned a Double and Larry made a change to it that still returned a Double.  In
that context the Format statement was irrelevant and the formatting was lost during the conversion to a double.

That being said, within the context of Veign's post, a String should have been returned by the BytesToMegabytes Function.

"It ain't what you don't know that gets you into trouble. It's what you know
for sure that just ain't so."  --- Mark Twain

Re: Checking available system resources

Postby Larry Serflaten » Fri, 21 Nov 2003 00:55:32 GMT

"Ben Taylor" < XXXX@XXXXX.COM > wrote

You didn't.  I pointed it out because it seemed out of place.
Until Veign indicated that it could have returned a string, you
or I had no way of knowing that it was to be used to convert
a number to a string.  As written it could have been used in
other numerical expressions because it returned a value type.

It could have had a valid purpose in using Format and still
returning a double.  There were no comments to indicate what
its real purpose was.

The purpose of my post was to get Veign to revisit that function
to better align it with its own intended purpose.  That is what I
set out to do, and it worked.  What was your purpose in trying to
belittle my reply?


Re: Checking available system resources

Postby Veign » Fri, 21 Nov 2003 02:08:36 GMT

    Not worth the energy to keep replying to him....

I appreciate your comments.....

Chris Hanscom
MVP (Visual Basic)
Application Design Section

Re: Checking available system resources

Postby Karl E. Peterson » Fri, 21 Nov 2003 06:22:08 GMT

In the interest of prolonging this argument <g>, and thinking maybe someone here
could tighten this a bit, here's one I use:

   Public Function FormatSize(ByVal Size As Variant, Optional ByVal LongDisplay As
Boolean = False, Optional SignCoercion As Boolean = True) As String
      Dim sRet As String
      Dim nSize As Currency
      Const KB& = 1024
      Const MB& = KB * KB
      Const GB@ = MB * KB
      Const TB@ = GB * KB

      ' ETC: Our own EVIL TYPE COERCION, for use when you *know* your
      ' own data very well, and that the sign-bit should be ignored!
      ' Comment this block out, if coercion shouldn't be supported.
      If Size < 0 Then
         Select Case VarType(Size)
            Case vbInteger
               Size = CLng(Size) + &H10000       '(2^16)
            Case vbLong
               Size = CDbl(Size) + 4294967296#   '(2^32)
         End Select
      End If

      ' Return size of file in kilobytes.
      If Size < KB Then
         sRet = Format(Size, "#,##0") & " byte"
         If Size <> 1 Then sRet = sRet & "s"
         ' Divide to obtain base number.
         If Size >= TB Then
            nSize = Size / TB
            sRet = " TB"
         ElseIf Size >= GB Then
            nSize = Size / GB
            sRet = " GB"
         ElseIf Size >= MB Then
            nSize = Size / MB
            sRet = " MB"
            nSize = Size / KB
            sRet = " KB"
         End If
         ' Display with suitable number of decimals.
         If nSize >= 100 Then
            sRet = Format(nSize, "0") & sRet
         ElseIf nSize >= 10 Then
            sRet = Format(nSize, "0.0") & sRet
            sRet = Format(nSize, "0.00") & sRet
         End If
      End If

      ' Return more detailed string on request.
      If LongDisplay Then
         If Size >= KB Then
            sRet = sRet & " (" & Format(Size, "#,##0") & " bytes)"
         End If
      End If
      FormatSize = sRet
   End Function

Improvements, anyone?  (Substantive improvements, at least. <g>)

Thanks...   Karl
[Microsoft Basic: 1976-2001, RIP]

Re: Checking available system resources

Postby Randy Birch » Sat, 22 Nov 2003 02:16:10 GMT

: Improvements, anyone?  (Substantive improvements, at least. <g>)

 http://www.**--****.com/ (?)


Randy Birch
MVP Visual Basic
Please respond only to the newsgroups so all can benefit. 

Similar Threads:

1.Resource Properties Not Available - Common Resource Pool

A new project is created when you select Open All Sharer files while opening a common resource pool.  With Project 98, this new project behaved like any other project and you could, for example use "ActiveProject.Resources.Count" to return the number of resources associated with the new project

I find that with Project 2002, I cannot seem to get any resource properties for the new project created as described above.  For example, even though I have over 200 resources that show up on the resource sheet for Project 2, the VBA statement "ActiveProject.Resources.Count" returns 0

Any idea how to get the resource and assignment properties when working within the summary project that is created when you open a common resource pool with all sharer files (under Project 2002)

Thanks in advance

2.Resource Properties Not Available - Common Resource Pool - Answer

Answered my own question (finally
needed to change from

For Each res In ThisProject.Resource
        For Each asgn In res.Assignment


For Each subprj In ThisProject.Subprojects    'subprj has been delcared as a Subprojcet typ
    For Each res In subprj.SourceProject.Resource
        For Each asgn In res.Assignment

Takes much longer and is not as clean.  I suppose there must be some benefit to the added abstration but I have yet to discover any such benefit

I hope this helps someone else

3.Failed to load resources from resource file, please check your set up - repost

I have written an application in VB.Net which runs well on my development
PC. However on a few clients machines, at apparently random intervals, they
get the above message and the programme crashes out. The error isn't
occurring during set-up, but during use of the programme.
I am having trouble trapping for this as I don't know what it means and
therefore what might be causing it.
As far as I know, I am not using any resource files (though that may be my
lack of knowledge!!)

I have searched the web and found a few examples of this but none of them
seem to fit my issue.

The programme displays stock charts and analyses the data within them.

I am running VS2003 with Windows XP service pack 2 with 1gb Ram
The clients are a similar spec.

Any help would be much appreciated


4.Excel (VBA) 2007 workbook in 2003 - available resources??

I have created from scratch a new main workbook and several 'satellite' 
dynamically populated/'built' report templates - in 2007 (as, 2003 to 2007 
seemed to fraught with danger).
All runs well, I have saved versions of the 2007 workbooks as 97-2003 
workbooks with a couple of compatability 'fixes' around colour formats and 
shape formats.  In 2003, I can run these (both the master workbook and the 
report templates )independantly and all works fine.
But - if I call the report via the parameter selection main workbook 
reporting FORM - the report is found, opened, parameters populated, list 
boxes filled and report dynamically structured - all well.  THe code then 
seeks to return to the main workbook to wrap up there - - and, alas, XL 
either falls over OR I get the following message (ad-infinitum):
"Excel cannot complete this task with available resources. Choose less data 
or close other applications."

Note - if the report is already open - and NOT opened via the procedure - 
then, all is fine again - however, that's not really sustainable - to open 
all report templates first...but - even though there ARE not links, is there 
some silly order of opening issue here?

----any ideas??  

There ARE pictures and shapes and activeX combo boxes on the report template.
The main workbook has pictures, shapes (acting as buttons), forms galore.

I've already found that using a 'shape' in 2007 to close a workbook is 
dangerous and use activeX controls for that.  I'm just a tad nervous about 
this whole 2003/2007 thing.

5.Retrieving a list of available fonts on system.


How would I return a list of available fonts. I'm writing a simple word
processor, and have got the font dialog working, but wanted to create a
toolbar dropdown with a list of fonts in, instead.

I'm not a 'hardened programmer'   ;-)    so any simple examples out there??

Thanks in antipation


6. System.OutOfMemoryException with plenty of memory available

7. all the number formats available on the system

8. CR 7.0 : some fonts are not available in font combo in 2k system

Return to Visual Basic


Who is online

Users browsing this forum: No registered users and 76 guest