BPSoftware.com
Home   Utilities   Purchase   FAQ   Support   Contact        
Shareware Utilities
 APrintDirect
 AIconExtract
 AFile Attribute Manager
Freeware Utilities
 AddrMon
 AFileSync
 ASysIcon
 B&P Table Utilities
 BPACLer
 BPSNMPMon
 BPSNMPUtil
 CharCount
 Delphi® Components
 MacAddr
Miscellaneous
 BPSoftware Blog
 Purchase Shareware
 Support

 


Sunday, January 28, 2007
PInvoke WIN32 and .NET

The WIN32 API has been around for quite sometime and has a large number of 'calls' and 'mechanisms' that have not yet been ported over to the .NET world. There are also still quite a few valuable WIN32 libraries/exports that have yet to be 'upgraded'. When integration is needed with existing WIN32 applications or APIs from a .NET application the unmanaged WIN32 DLL exports can be accessed using Platform Invoke (also referred to as PInvoke or P/Invoke). I am in the process of moving pieces of code to use the .NET framework. One of the first pieces on my list was the About Box class that I use in my personal applications. This base About Box class I created and use through out most (if not all) of my personal applications. The setting of a few properties has the About Box up and running for a new application in moments. In this About Box class I use the GlobalMemoryStatus function to display virtual and physical memory information. In order to still use this function in my new and 'improved' .NET About Box I needed to use PInvoke.

C#

using System.Runtime.InteropServices;

// define the structure
public struct _MEMORYSTATUS //http://msdn2.microsoft.com/en-us/library/aa366772.aspx
{
public uint dwLength;
public uint dwMemoryLoad;
public uint dwTotalPhys;
public uint dwAvailPhys;
public uint dwTotalPageFile;
public uint dwAvailPageFile;
public uint dwTotalVirtual;
public uint dwAvailVirtual;
}


[DllImport("kernel32.dll", SetLastError = true, CharSet = CharSet.Auto)]
public static extern void GlobalMemoryStatus(out _MEMORYSTATUS memorystatus);


// for simplicity of demonstration
_MEMORYSTATUS memorystatus = new _MEMORYSTATUS();
GlobalMemoryStatus(out memorystatus);
ulong totalVirtualMemory = memorystatus.dwTotalVirtual;
ulong availablePhysicalMemory = memorystatus.dwAvailPhys;
ulong availableVirtualMemory = memorystatus.dwAvailVirtual;
ulong totalPhysicalMemory = memorystatus.dwTotalPhys;




VB.NET

Imports System.Runtime.InteropServices

Public Structure _MEMORYSTATUS
Dim dwLength As Integer
Dim dwMemoryLoad As Integer
Dim dwTotalPhys As Integer
Dim dwAvailPhys
As Integer
Dim dwTotalPageFile As Integer
Dim dwAvailPageFile As Integer
Dim dwTotalVirtual As Integer
Dim dwAvailVirtual As Integer
End Structure

'In VB.NET there are two ways to call this

'<DllImport("kernel32.dll")> Public Shared Sub _
' GlobalMemoryStatus1(ByVal memorystatus As _MEMORYSTATUS)
'End Sub

Declare Auto Sub GlobalMemoryStatus Lib "kernel32" (ByRef lpBuffer As _MEMORYSTATUS)



Dim memorystatus As New _MEMORYSTATUS
GlobalMemoryStatus(memorystatus)

Dim totalVirtualMemory As Integer = memorystatus.dwTotalVirtual
Dim availablePhysicalMemory As Integer = memorystatus.dwAvailPhys
Dim availableVirtualMemory As Integer = memorystatus.dwAvailVirtual
Dim totalPhysicalMemory As Integer = memorystatus.dwTotalPhys



Side Note: After having problems with Windows Live Writer after I 'upgraded' to the 'new' Blogger I published this blog entry using Google Docs & Spreadsheets. Curiosity got the best of me and I wanted to see how it worked.

Labels: , , ,

posted by Brad Prendergast at 12:16:00 AM (0 comments)
Links to this post
Permalink
Saturday, January 27, 2007
ASP.NET upload a file HtmlInputFile Control

When developing a web application it often may be necessary to allow a user to ‘send’ (upload) a file. With HtmlInputFile control the this seemingly complicated task is much easier than one would expect. The HtmlInputFile control is not listed in the toolbox by default but can be easily added using HTML in your page. In order to use the HtmlInputFile control the following form code must be added to your .aspx page:



<!-- Add enctype="multipart/form-data" to the form declaration -->
<form id="Form1" method="post" runat="server" enctype="multipart/form-data">

<!-- the following line was manually entered to display the file control -->
<input id="btnSelectFile" type="file" runat="server" style="Z-INDEX: 101; LEFT: 24px; WIDTH: 470px; POSITION: absolute; TOP: 48px; HEIGHT: 22px" size="59">
<!-- -->

<asp:Label id="lblStatus" style="Z-INDEX: 103; LEFT: 176px; POSITION: absolute; TOP: 88px" runat="server" Width="224px">Status:</asp:Label>
<asp:Button id="btnUpload" style="Z-INDEX: 102; LEFT: 96px; POSITION: absolute; TOP: 88px" runat="server" Text="Upload"></asp:Button>
</form>


The size and position of the controls can be what ever you desire. Once the HtmlInputFile code is added you can visually design its size and position. In the above code I have also included a label for the displaying of status information and a button that will actually execute the code for the uploading of the file selected with the HtmlInputFile.
The code to upload the file is simple:

' Root Data path
‘ ensure the proper permissions are set on this folder default is the ASP.NET account
Const ROOTPATH = "C:\Data\"

Private Sub btnUpload_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnUpload.Click

Dim filename As String

' Extract the selected filename
filename = System.IO.Path.GetFileName(btnSelectFile.PostedFile.FileName)

' Set the label to display where the file will be saved – this could also be used to display the file size uploaded
lblStatus.Text = "Status: " & ROOTPATH & filename

' Save the file
btnSelectFile.PostedFile.SaveAs(ROOTPATH & filename)

' This calls a procedure that I had created that will display the contents of a directory in a table
ListDirectory()

End Sub


Private Sub ListDirectory()

Dim currentdir As New DirectoryInfo(ROOTPATH)
Dim files As FileInfo
Dim tr As TableRow
Dim tcFile As TableCell
Dim tcSize As TableCell

For Each files In currentdir.GetFiles()
tr = New TableRow
tcFile = New TableCell
tcSize = New TableCell
tcFile.Text = files.Name
tcSize.HorizontalAlign = HorizontalAlign.Right
tcSize.Text = FormatNumber(files.Length, 0, TriState.False, TriState.True, TriState.True)
tr.Cells.Add(tcFile)
tr.Cells.Add(tcSize)
tblFiles.Rows.Add(tr)
Next

End Sub



In reviewing the previous code sample one can see how easy it is to upload a file through a web application.

Labels: , , ,

posted by Brad Prendergast at 3:54:00 PM (0 comments)
Links to this post
Permalink
Sunday, January 21, 2007
PPCTL.DLL is damaged and could not be repaired.

Well, it was that time again for my good old Anti-Virus & Anti-Spyware software subscription to expire. Having used the same product for a while, I wanted something that had a low impact and wasn't so invasive to my system. The more and more hardware machines come with, it seems the more and more resources these Anti- programs want to take use. Having decent experience with earlier versions CA's ETrust software I decided to grab the three pack of CA Internet Security Suite. To make a long story short, I ended up having problems installing and running the Anti-Spyware piece of the Suite (a while back CA purchased PestPatrol). Only one out of the three machines I installed the software on went in without any issues. For the two that did give me some grief, fortunately it was the same problem. On these machines I received a message indicating that 'ppctl.dll is damaged and could not be repaired'. This error was displayed during installation and attempted operation of the software. I searched the 'Net' to see if I could dig anything up on this error. No such luck. I then went off on my own little investigation. By default, the ppctl.dll is installed in two locations; the C:\Program Files\CA\CA Internet Security Suite\CA Anti-Spyware and C:\Program Files\Common Files\Scanner folders. My first step was to verify the permissions that were set on these folders; to ensure that the Administrators Group and SYSTEM Account had full control over these folders. After this was verified (ie changed) I was hoping to be out of the woods-- well I wasn't. After digging a bit further I noticed there is also registry key (HKLM\SOFTWARE\Classes\ppctl) that references the ppctl.dll. Just as I had the folders I verified the permissions on the registry key. In reviewing the permissions I noticed the Administrators Group and SYSTEM Account had Read-Only access to the key. Getting creative I decided to give both accounts Full-Control to the registry key. Once I granted full control to the Administrators Group and the SYSTEM account on the HKLM\SOFTWARE\Classes\ppctl registry key all my headaches went away.

Labels: ,

posted by Brad Prendergast at 6:17:00 PM (18 comments)
Links to this post
Permalink
Friday, January 19, 2007
Google Mobile SMS

Having recently picked up a new phone and turning on unlimited SMS, I have a new fascination with 'texting'. SMS has been around for a while, but I never really got into it. After using it for a while it is difficult to imagine being without IM while 'out and about'. If you need to get a quick 'ping' texting often is a bit easier and quicker than a phone conversation. Another cool SMS tool I came across is Google Mobile SMS. This is a neat little way to get weather, directions, phone numbers and other results while you are away from your computer.

Labels: , ,

posted by Brad Prendergast at 10:02:00 PM (0 comments)
Links to this post
Permalink
Monday, January 01, 2007
Running a .NET Framework application from a network location

If you're anything like me, when deploying certain applications in a corporate environment you like to run your applications from a network resource. This often allows for the easy application maintenance of updates and changes. When deploying a dotNET application it isn't always as easy as placing a shortcut on the workstation or running the application from a network drive. With the additional security found in the .NET Framework it is often necessary to configure your application to run from a network location. This MSDN article explains how. The Grant permission to the application section is the part of interest.

Labels: , ,

posted by Brad Prendergast at 11:47:00 AM (1 comments)
Links to this post
Permalink
Recent Posts
 Command Line: Visual Source Safe
 SQL: Remove / Delete Orphan Users
 SQL Delete/Drop a User from each Database
 Is there an 'I' in phone?
 SQL Optimization: Am I missing any indexes? - Part...
 Meaningful Signature File Quotes
 My Shared RSS Items
 Edit those XML files
 A little System.Diagnostics
 Wi-Fi Detector Shirt


Labels



Archives
 October 2005
 November 2005
 December 2005
 January 2006
 February 2006
 March 2006
 April 2006
 May 2006
 June 2006
 July 2006
 August 2006
 September 2006
 December 2006
 January 2007
 February 2007
 March 2007
 September 2007
 October 2007
 November 2007
 July 2008
 November 2008
Powered by Blogger