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

' 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.



   

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:




<form id="Form1" method="post" runat="server" enctype="multipart/form-data">


<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.



   

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.