Inside and Out…

An attempt to understand technology better…

Archive for the ‘.NET Framework’ Category

Enumerating AppDomains – I

Posted by Gaurav Khanna on June 9, 2007

Surprisingly, enumerating AppDomains is not that straightforward. The System.AppDomain type does not expose functionality to enumerate them at all. The way to enumerate them will be to use the V1 Hosting interface, ICorRuntimeHost and then invoke the EnumDomains and NextDomain methods against it to get the list. If you are using CLR 2.0, you needn’t worry as CLR 2.0 does implement the V1 interfaces as well.

 

Below is the snippet that exemplifies the concept (note: AppDomains will only be enumerated in the current process – I will discuss AppDomain enumeration in remote processes later). You can download the source code (VS Solution) from http://www.wintoolzone.com/ListDotNET.aspx?Listtype=5

 

// EnumAppDomains.cpp : Defines the entry point for the console application.
// 
#include "stdafx.h"
#include <windows.h>
#include "mscoree.h"
#import "C:\\Windows\\Microsoft.NET\\Framework\\v2.0.50727\\mscorlib.tlb" raw_interfaces_only
using namespace mscorlib; 
#define EXITONERROR(hr, mesg) \
if (FAILED(hr)) \
{ \
printf("%s\n", mesg); \
ExitProcess(1); \
} 
#define SUCCESSFUL(hr) (SUCCEEDED(hr) && hr != S_FALSE) 
int _tmain(int argc, _TCHAR* argv[])
{
ICorRuntimeHost * pV1HostPtr = NULL;
HRESULT hr = CorBindToRuntimeEx(NULL,
NULL, 0, 
CLSID_CorRuntimeHost,
IID_ICorRuntimeHost,
(LPVOID *)&pV1HostPtr);
EXITONERROR(hr, "Unable to get ICorRuntimeHost*"); 
// Start the runtime
hr = pV1HostPtr->Start();
EXITONERROR(hr, "Unable to start the runtime"); 
// Enumerate the AppDomains
HDOMAINENUM adEnum;
hr = pV1HostPtr->EnumDomains(&adEnum);
EXITONERROR(hr, "Unable to enumerate AppDomains");
// Loop thru the domains
IUnknown * pDomainUnk = NULL;
hr = pV1HostPtr->NextDomain(adEnum, &pDomainUnk);
while(SUCCESSFUL(hr))
{
// Got the IUnknown* to the AppDomain - convert it to AppDomain pointer
_AppDomain * pCurDomain = NULL;
hr = pDomainUnk->QueryInterface(__uuidof(_AppDomain), (VOID**)&pCurDomain);
if (SUCCESSFUL(hr))
{
// Display the name of the AppDomain
BSTR str;
if (SUCCESSFUL(pCurDomain->get_FriendlyName(&str)))
{
wprintf(L"AppDomain: %s\n",str);
}
else
{
printf("AppDomain: unable to get the name!\n");
}
} 
// Loop onto the next Domain
hr = pV1HostPtr->NextDomain(adEnum, &pDomainUnk);
} 
// Stop the runtime
if (pV1HostPtr)
{
pV1HostPtr->Stop();
}
return 0;
} 
Advertisements

Posted in .NET Framework, CLR, Development | Leave a Comment »

[Download] TransNTFS v1.0.0.1 – support for folders, symbolic and hard links and more

Posted by Gaurav Khanna on March 18, 2007

I have updated TransNTFS, my managed implementation for the transactional NTFS APIs that have been introduced with Windows Vista. TransNTFS now also supports the following operations under a transaction:

It comes with complete documentation (as always) and can be downloaded from http://www.wintoolzone.com/ListDotNET.aspx?Listtype=3

Posted in .NET Framework, Development, Downloads, Managed C++, Windows API | Leave a Comment »

[Download] TransNTFS – Managed implementation of Transactional NTFS (TxF) APIs

Posted by Gaurav Khanna on March 15, 2007

Sometime back, I had made a post regarding a native C++ class I had authored for using some of the Transactional NTFS (TxF)APIs introduced in Windows Vista.

Almost two weeks from that post, I just finished my managed implementation, TransNTFS, that enables you to use the TxF APIs from managed code. As always, Managed C++ enabled the implementation with excellent ease. It comes with complete documentation and can be downloaded from http://www.wintoolzone.com/ListDotNET.aspx?Listtype=3.

Below is an example usage of how to copy a file under a transaction:

using System;
using System.Collections.Generic;
using System.Text;
using WinToolZone;
namespace ManagedTransactionCopyDemo
{
class Program
{
static void Main(string[] args)
{
TransNTFS refTC = new TransNTFS();
// Init the transaction
bool fSuccess = refTC.BeginTransaction();
if (fSuccess)
{
fSuccess = refTC.CopyFile("d:\\transcopy.pdb", "d:\\t.pdb", false);
if (fSuccess)
{
fSuccess = refTC.Commit();
if (fSuccess)
{
Console.WriteLine("Copy successful!");
}
else
{
ShowErrorAndExit("Commit failed!", refTC.LastError);
}
}
else
{
ShowErrorAndExit("Copy failed!", refTC.LastError);
}
}
else
{
ShowErrorAndExit("Unable to start the transaction!", refTC.LastError);
}
}
private static void ShowErrorAndExit(string p, int p_2)
{
Console.WriteLine("ERROR: {0}", p);
Console.WriteLine("CODE: {0}", p_2);
System.Environment.Exit(1);
}
}
}

Posted in .NET Framework, Development, Downloads, Windows, Windows API | 1 Comment »

Programming the Transactional NTFS (TxF)

Posted by Gaurav Khanna on March 1, 2007

One of the key new features of Windows Vista is a component called the Kernel Transaction Manager (KTM) that brings inherent support for transactional development in not just the kernel-mode but also for user mode. Infact, NTFS has been enhanced to use and support transactions such that couple of new APIs (e.g. CopyFileTransacted, MoveFileTransacted just to name a few) have surfaced up. You can get more details on Transactional NTFS here.

To demonstrate the power of the new APIs, I wrote a C++ class library (unmanaged), CTransCopy, that allows you to:

  • Copy files under a transaction
  • Move files under a transaction
  • Commit or Rollback the transaction
  • Let you wire up a callback handler for copy/move progress

You can download it from http://www.wintoolzone.com/ListWin32.aspx?Listtype=5. The zipped archive also contains a sample client source code. BTW, since the .LIB file containing the class was compiled using VC++ 2005 compiler, you will need the same to link against it and write an application. You can use VC++ 2005 Express Edition. Below is an example usage of the same:

CTransCopy tcopy;

if (tcopy.IsOSSupported() == FALSE)
{
printf(“This application requires Windows Vista or later.”);
return -1;
}

if (tcopy.Init() == FALSE)
{
printf(“Init failed with error %08X\n”,tcopy.LastError());
return -1;
}

// Setup a callback for progress
tcopy.SetCopyCallback(ProgressCallback);

if (tcopy.CopyFileW(L”D:\\KGK\\Development\\VS\\Windows Vista\\VC\\TransCopy\\debug\\transcopy.pdb”,
L”D:\\transcopy.pdb”,FALSE) == FALSE)
{
printf(“Copy/move failed with error %08X\n”,tcopy.LastError());
tcopy.Rollback();
return -1;
}

if (tcopy.Rollback() == FALSE)
{
printf(“Rollback failed with error %08X\n”,tcopy.LastError());
return -1;
}

if (tcopy.Commit() == FALSE)
{
printf(“Commit failed with error %08X\n”,tcopy.LastError());
return -1;
}

printf(“Copy/move successful!\n”);

The ProgressCallback function is implemented as shown below:

void ProgressCallback(LARGE_INTEGER total, LARGE_INTEGER transferred)
{
double percent = (transferred.QuadPart*100.00)/total.QuadPart;
printf(“%f%% over\n”,percent);
}

Posted in .NET Framework, Development, Downloads, Windows, Windows API | 1 Comment »

Article: Customizing AppDomain Creation

Posted by Gaurav Khanna on February 26, 2007

Did you have a scenario where you wished you could customize the creation of AppDomains? Or, you could control how many AppDomains any code that runs in your application context (e.g. if you are writing a plugin load framework) could create? Or, how about reusing AppDomains for various assemblies?

I have just finished an article on how any of the above scenarios can be accomplished using the System.AppDomainManager type, introduced in .NET Framework 2.0, by intercept AppDomain creation requests.

You can read the article at http://www.wintoolzone.com/articles/Customizing_AppDomain_Creation.aspx and download associated source code from http://www.wintoolzone.com/downloads/Customizing_AppDomain_Creation_src.zip

Posted in .NET Framework, Articles, CLR, Development, Downloads | Leave a Comment »

DesktopSMS – Send, Receive and Reply to SMS from the desktop

Posted by Gaurav Khanna on July 31, 2006

Ever wanted to compose SMS from the ease of desktop instead of the phone? Wanted to be notified on the desktop, while you are working, when a SMS is received and reply to it from there itself?

DesktopSMS, for Windows Mobile 5.0 based SmartPhone and PocketPC devices, will let you do just that! Based upon .NET Framework 2.0, .NET Compact Framework 2.0 and Windows Mobile 5.0, it allows you to compose, receive and reply to SMS while the phone is connected to your machine via ActiveSync. Below are some screenshots of the same:

Main window to send SMS

Likewise, when a SMS is received, a popup comes on your screen that will allow you to reply as well:

Popup on message receipt

You can even select recepients from Microsoft Outlook’s Contact folder as message receipient, provided they have mobile phone number specified. It comes with complete documentation and installer.

Download DesktopSMS from http://www.wintoolzone.com/showpage.aspx?url=listdotnet.aspx?Listtype=1

Technorati : , , , , , ,
Del.icio.us : , , , , , ,

Posted in .NET Compact Framework, .NET Framework, Downloads, Windows API, Windows CE/Windows Mobile | Leave a Comment »

[WebService] CAPTCHA image generator for spam control

Posted by Gaurav Khanna on July 24, 2006

As part of the revamp efforts of www.wintoolzone.com, I have added the [Web]Services section where I have made available, for free use in your applications, various SOAP based services, built using the .NET Framework.

The first service I have added is a CAPTCHA image generation service. If you are running a website or a blog where you take input from the user in a HTML form, you would know about bots who can spam the form and send invalid details. One of the ways to handle this problem is challenge-response where you throw a challenge to the entity filling up the form and if their response is valid, only then the HTML form’s details are taken for subsequent processing.

One of the ways websites and various blog engines are trying to combat spam is by dynamically generating an image which shall containing a randomly generated text – and then prompting the user to enter the text. Only if there is a match, will the HTML form be processed.

The CAPTCHA image generation service can be used to generate this random text and create an image out of it, which can then be displayed on your webpage for verification. It is Basic Profile 1.1 specification compliant. Its free for use in your applications – you simply need to be registered with WinToolZone.

Complete service documentation is at http://www.wintoolzone.com/showpage.aspx?url=Services/Help/Help_CaptchaGenerator.aspx

Posted in .NET Framework, Development | Leave a Comment »

Working with properties in Managed C++?

Posted by Gaurav Khanna on July 6, 2006

Then read this article by someone who knows Managed C++ very well. His blog is here.

Posted in .NET Framework, Articles, Managed C++, Tips | Leave a Comment »

WinToolZone.Bluetooth – Added device enumeration support

Posted by Gaurav Khanna on July 6, 2006

WinToolZone.Bluetooth, the managed Bluetooth API for the desktop that I am working on, now has the support for enumerating devices as well. The snippet below exemplifies it:

    Bluetooth bth = new Bluetooth();
            
    // EnumRadios(bth);

    if (bth.RefreshDevices(false, true, true, true, true, null) == false)
    {
         Console.WriteLine("Unable to enum devices");
         return;
    }

    foreach (BluetoothDevice device in bth.Devices)
    {
         Console.WriteLine("Devicename: {0}", device.Name);
         Console.WriteLine("LastSeen: {0}", device.LastSeen.ToString());
         Console.WriteLine("LastUsed: {0}", device.LastUsed.ToString());
         Console.WriteLine("Connected: {0}", device.IsConnected.ToString());
         Console.WriteLine("Remembered: {0}", device.IsRemembered.ToString());
         Console.WriteLine("Authenticated: {0}", device.IsAuthenticated.ToString());
         Console.WriteLine("DeviceClass: {0}", device.DeviceClassID);
         Console.WriteLine("Address: {0}\n", device.Address.ToString());
                
    }

And below is an output screenshot:

Bluetooth device enumeration output using WinToolZone.Bluetooth

Technorati : , , , ,
Del.icio.us : , , , ,
Ice Rocket : , , , ,
43 Things : , , , ,

Posted in .NET Framework, Development, Managed C++, Windows API | 1 Comment »

WinToolZone.Bluetooth – Managed Bluetooth API for the desktop

Posted by Gaurav Khanna on July 5, 2006

One of the communication protocols not present in .NET Framework class libraries is Bluetooth. So today, I started to work on my implementation of a managed API for Bluetooth programming – WinToolZone.Bluetooth. I am authoring it using Managed C++ and leveraging the Microsoft Bluetooth stack APIs.

I just completed implementing the support for enumerating the Bluetooth radios on a machine. Below is a C# program that exemplifies how they can be enumerated:

using System;
using System.Collections.Generic;
using System.Text;
using WinToolZone;

namespace BTHCSClient
{
    class Program
    {
        static void Main(string[] args)
        {
            Bluetooth bth = new Bluetooth();
            if (bth.RefreshRadios())
            {
                foreach (BluetoothRadio radio in bth.Radios)
                {
                    Console.WriteLine("RadioName: {0}", radio.Name);
                    Console.WriteLine("Address: {0}", radio.Address.ToString());
                    Console.WriteLine("ManuID: {0}", radio.ManufacturerID);
                    Console.WriteLine("LMPSubversion: {0}", radio.LMPSubversion);
                    Console.WriteLine("DeviceClass: {0}", radio.DeviceClass);
                }
            }
            else
            {
                Console.WriteLine("Unable to enumerate BTH radios");
            }
        }
    }
}

Technorati : , , , , , ,
Del.icio.us : , , , , , ,
Buzznet : , , , , , ,
43 Things : , , , , , ,

Posted in .NET Framework, Development, Distributed Communications, Managed C++ | 1 Comment »