admin
This user hasn't shared any biographical information
Posts by admin
Get to the point – Code Snippet: Retrieve logged in Terminal Service username & remote host pc name
Apr 17th
Use this code snippet to retrieve the username & remote host pc name from a Terminal Service connection.
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Security.Principal;
using System.Net;
namespace loginName
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
WindowsPrincipal wp = new WindowsPrincipal(WindowsIdentity.GetCurrent());
MessageBox.Show(wp.Identity.Name); // Username
MessageBox.Show(GetTerminalServerClientNameWTSAPI()); // Remote Host PC Name
}
private static string GetTerminalServerClientNameWTSAPI()
{
const int WTS_CURRENT_SERVER_HANDLE = -1;
IntPtr buffer = IntPtr.Zero;
uint bytesReturned;
string strReturnValue = "";
try
{
WTSQuerySessionInformation(IntPtr.Zero, WTS_CURRENT_SERVER_HANDLE, WTS_INFO_CLASS.WTSClientName, out buffer, out bytesReturned);
strReturnValue = System.Runtime.InteropServices.Marshal.PtrToStringAnsi(buffer);
}
finally
{
buffer = IntPtr.Zero;
}
return strReturnValue;
}
enum WTS_INFO_CLASS
{
WTSInitialProgram,
WTSApplicationName,
WTSWorkingDirectory,
WTSOEMId,
WTSSessionId,
WTSUserName,
WTSWinStationName,
WTSDomainName,
WTSConnectState,
WTSClientBuildNumber,
WTSClientName,
WTSClientDirectory,
WTSClientProductId,
WTSClientHardwareId,
WTSClientAddress,
WTSClientDisplay,
WTSClientProtocolType
}
[System.Runtime.InteropServices.DllImport("Wtsapi32.dll")]
private static extern bool WTSQuerySessionInformation(System.IntPtr hServer, int sessionId, WTS_INFO_CLASS wtsInfoClass, out System.IntPtr ppBuffer, out uint pBytesReturned);
}
}
Get to the point: Detect Internet Connection
Feb 21st
We will be using Apple’s reachability class to detect the state of the Internet connection. Reachability is freely available from Apple’s website: http://developer.apple.com/iphone/library/samplecode/Reachability/
1- Create a View-based Application and name it InternetConnectionState
2- Download Apple’s Reachability App source code and copy reachability.h & reachability.m files to your application’s Classes folder.
3- Add these files to your XCode project. Right click the Classes folder and select Add->Existing Files (see Figure 1). Select the reachability.h & reachability.m files and add them to your XCode project. You should see these file under the Classes folder (see Figure 2).
Figure 1
Figure 2
4- Add the “SystemConfiguration” framework. Right click the Frameworks folder and select Add->Existing Frameworks. Select & Add the SystemConfiguration.framework (see Figure 3).
Figure 3
5- Open the InternetConnectionStateAppDelegate.h and add the following code below the #import <UIKit/UIkit.h> line:
@class Reachability;
Add the following lines inside @interface:
Reachability* hostReach; Reachability* internetReach; Reachability* wifiReach;
Finally, add these lines above the @end line:
- (void) updateInterfaceWithReachability: (Reachability*) curReach; - (void) statusView: (NSString*) statusString;
6- Open the InternetConnectionStateAppDelegate.m and add this line below the last #import:
#import "Reachability.h"
7- Add the following lines of code to the applicationDidFinishLaunching function:
[[NSNotificationCenter defaultCenter] addObserver: self selector: @selector(reachabilityChanged:) name: kReachabilityChangedNotification object: nil]; hostReach = [[Reachability reachabilityWithHostName: @"www.google.com"] retain]; [hostReach startNotifer]; [self updateInterfaceWithReachability: hostReach]; internetReach = [[Reachability reachabilityForInternetConnection] retain]; [internetReach startNotifer]; [self updateInterfaceWithReachability: internetReach]; wifiReach = [[Reachability reachabilityForLocalWiFi] retain]; [wifiReach startNotifer]; [self updateInterfaceWithReachability: wifiReach];
8- Copy these functions below the applicationDidFinishLaunching function:
- (void) networkStatusChanged: (Reachability*) curReach
{
NetworkStatus netStatus = [curReach currentReachabilityStatus];
BOOL connectionRequired= [curReach connectionRequired];
NSString* statusString= @"";
switch (netStatus)
{
case NotReachable:
{
statusString = @"Access Not Available";
connectionRequired= NO;
[self statusView:statusString];
break;
}
case ReachableViaWWAN:
{
statusString = @"Reachable WWAN";
[self statusView:statusString];
break;
}
case ReachableViaWiFi:
{
statusString= @"Reachable WiFi";
[self statusView:statusString];
break;
}
}
if(connectionRequired)
{
statusString= [NSString stringWithFormat: @"%@, Connection Required", statusString];
}
}
- (void) statusView: (NSString *) statusString{
UILabel *titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 20, 320, 50)];
titleLabel.font = [UIFont systemFontOfSize:15];
titleLabel.textColor = [UIColor blackColor];
titleLabel.backgroundColor = [UIColor whiteColor];
titleLabel.opaque = YES;
titleLabel.textAlignment = UITextAlignmentCenter;
titleLabel.text = statusString;
[viewController.view addSubview: titleLabel];
}
- (void) updateInterfaceWithReachability: (Reachability*) curReach
{
if(curReach == hostReach)
{
BOOL connectionRequired= [curReach connectionRequired];
NSString* baseLabel= @"";
if(connectionRequired)
{
baseLabel= @"Cellular data network is available.\n Internet traffic will be routed through it after a connection is established.";
}
else
{
baseLabel= @"Cellular data network is active.\n Internet traffic will be routed through it.";
}
}
if(curReach == internetReach)
{
[self networkStatusChanged: curReach];
}
if(curReach == wifiReach)
{
[self networkStatusChanged: curReach];
}
}
- (void) reachabilityChanged: (NSNotification* )note
{
Reachability* curReach = [note object];
NSParameterAssert([curReach isKindOfClass: [Reachability class]]);
[self updateInterfaceWithReachability: curReach];
}
9- Save & Build your application. If there were no errors you should get something similar to Figure 4.
Figure 4
Get to the point – Code Snippets: Dialing a number, sending SMS & Email from within your app
Feb 8th
These code snippets will allow you to dial a number, send an SMS & send an e-mail from within your application. They will all open up the selected application and close yours.
Dialing a number
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"tel://123456"]];
Sending an SMS
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"sms:123456"]];
Sending an E-mail
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"mailto:amasso@amasso.info?subject=Hello&body=Testing your code snippet."]];
Get to the point – Code Snippets: NSString to NSInteger & vice versa
Feb 7th
NSString to NSInteger code snippet:
NSString *strTest = [NSString stringWithFormat:@"%d", 10];
NSInteger to NSString code snippet:
NSInteger iTest = [strTest integerValue];
clouding.me beta launched!
Aug 26th
The beta version of www.clouding.me is online. Register to beta test this new clouding service. I will soon add an iPhone web version. The iPhone app version is on its way, hopefully Apple will approve it.

Follow me on Twitter




























