Sunday, July 24, 2005

Using reflection to access private data

One day I ordered an expensive SDK to use additional functionality with previously purchased application. I was promised I could access certain additional features. This was not true. The method we needed to use was private.No big deal. Using reflection, we can see / view / access a private member, private variable, private method. You can also call a private method of another class. This is great to take a peek at some private data for the curious. Some people may say this is not a solution, dangerous, and also complain your messing up the laws of encapsulation. It worked for me. Here is an example that may help you. Take special note of the ORed bindingflags NonPublic and Instance because you wont find the private methods without them.

This is also useful for unit testing private methods.


using System;
using System.Reflection;
public class PrivateReflection
{
public static int Main()
{
ClassA a = new ClassA();
Console.WriteLine("before:" + readPrivateVar(a));
changePrivateVar(a, "I am changed!");
Console.WriteLine("after:" + readPrivateVar(a));
Console.WriteLine("executing:" + execPrivMethod(a, "haxx0red", 1234));
return 0;
}
public static string readPrivateVar(ClassA a)
{
Type t = Type.GetType("ClassA");
FieldInfo Myfieldinfoa = t.GetField("privateString",
BindingFlags.NonPublic | BindingFlags.Instance);
return (string)Myfieldinfoa.GetValue(a);
}
public static void changePrivateVar(ClassA a, string ChangeTo)
{
Type t = a.GetType();
FieldInfo f = t.GetField("privateString",
BindingFlags.NonPublic | BindingFlags.Instance);
f.SetValue(a, ChangeTo);
}
public static string execPrivMethod(ClassA a, string str, int integ)
{
Type t = a.GetType();
Object[] paramz = { str, integ };
MethodInfo m = t.GetMethod("privateCalculation",
BindingFlags.NonPublic | BindingFlags.Instance);
return (string)m.Invoke(a, paramz);
}
}
public class ClassA
{
private string privateString = "I am a private string.";
private string privateCalculation(string x, int y)
{
return x + "SECRET" + y.ToString();
}
}


Result:
before:I am a private string.
after:I am changed!
executing:haxx0redSECRET1234

Tuesday, July 19, 2005

Printing differently

Here is instructions how you can print a web page differently than viewed on screen by using CSS stylesheets. You can hide your header / footer, web page menus, preventing credit card numbers from printing, tables or passwords from printing. This is good for omitting certain information. You can also print differently depending on the device that the document is being output to (or printed). This can be used to easily print alternate content on your page depending on if your viewing it or printing it. To do this, we will use the media attribute. I think of it as an if else statement depending upon the output device. Some of these outputs include printer, projection, screen, tty, tv and more. For a full list see this. Below is an example where you can to hide certain text from printing and also print text differently out the printer.

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
  <head>
    <style> 
      .name { FONT-SIZE:10px}
      @media screen { 
      .doNotPrint{ display: all}
      .creditCardNumber {COLOR: #111111;}
      }
      @media print { 
      .name{FONT-SIZE: 20px;}
      .creditCardNumber {  display: none;}
      .doNotPrint{display: none; }
      }
    </style>
  </head>
  <body>
    <P>
      <TABLE id="Table1" cellSpacing="1">
        <TR class="doNotPrint">
          <TD>Welcome(WONT PRINT)</TD>
        </TR>
        <TR class="creditCardNumber">
          <TD>4522668955551234(WONT PRINT)</TD>
        </TR>
        <TR class="name">
          <TD>John Smith(PRINTS LARGE)</TD>
        </TR>
      </TABLE>
    </P>
  </body>
</html>

Sunday, July 10, 2005

P2P Client Port Numbers

Here is a list of ports to forward when using file shaing programs ( P2P )
Kazaa ( fasttrack ) uses 1214
Gnutella ( LimeWire, Bearshare, Morpheus and others ) 6346
OpenFt 1215,1216
Ares ( Ares ? ) 59049
Edonkey (emule) 4662 4665

The open source P2P program KCEasy Application supports all these protocols in one and contains no string and no adware.

http://kceasy.com/
you have to search for the kazaaa plugin yourself it is under this name
kceasy-fasttrack-0.8.7.exe

Thursday, July 07, 2005

Using BITS from C#

Here is instructions download a file with BITS. Perhaps to update your app from C# or at least this will get you started. Bits is written in C++ but fourtounately; Microsoft wrote a wrapper called BackgroundCopyManager (backgroundcopymanager.dll). You can read the article and download the assembly here. You need to start a new C# project and add a reference to the dll and also add this namespace to the top of your code.
using Microsoft.Msdn.Samples.BITS;
Here is code to queue a file and download it.
Manager m = new Manager();
Job bj = m.CreateJob("MyJob3");
bj.AddFile("http://localhost/largefile.msi", "destination123.msi");
bj.ResumeJob();
This code will bind a listbox and show the status of each job
Manager myBITS = new Manager();
JobCollection myJobs = myBITS.GetListofJobs(JobType.AllUsers);
myListBox.DataSource = myJobs;
Good luck.

BITSAdmin Uploading

Here is how you upload if you have been having problems. None of the documentation is in one place.
  1. Download the newest bits available ( see my other post and this)
  2. Install the bits server extentions ( part of windows 2003 server)
  3. Create a virtual folder in IIS ( or a new web site) (DO NOT GIVE ANY EXECUTE PERMISSIONS)
  4. Enable Bits Server Extentions in IIS for your virtual folder.
  5. Grant access to the folder to everyone (just for now ;] )
  6. Use this batch file to upload your files.

bitsupload.txt

@echo off
echo usage: %0 RandomJobname FullPathToSource FullPathToDest
echo i.e. %0 myJob91832 c:\windows\file.txt http://abc.com/file.txt
if "%1"=="" goto end
Bitsadmin /create /upload %1
Bitsadmin /addfile %1 %3 %2
BitsAdmin /setnoprogresstimeout %1 79200
BitsAdmin /setminretrydelay %1 60
Bitsadmin /resume %1
Bitsadmin /info %1 /verbose
rem Bitsadmin /monitor
:end

Hopefully that should work, drop me a line if that helped.

Wednesday, July 06, 2005

BITSAdmin Tool / Downloading

Have you had any problems with the Background Intelligent Transfer Service ? ( BITS) I did, and it took me quite a while and various resources to figure how this tool worked. I really needed it because its useful for downloading updates for you applications and also for backing up things such as your laptop to another server. Windows update uses this tool to slowly download updates. First of all ill show you how to download and upload with the bitsadmin tool ( bitsadmin.exe ). Lastly ill show you how to download using this code in C# without needing to know any C++. Just for basic use; here is microsofts docs on the tool LINK . Not too clear? They say to extract the most recent version from the windows xp service pack 2 support tools. I would get it ( version 2.0) here. I created a bat file to make the usage easier. To download is pretty Simple, try these in a cmd window. [Note, i replaced my previous intructions here with a .bat file instead]

bitsdownload.bat

@echo off
echo usage: %0 RandomJobName FullPathToSource FullPathToDest
echo i.e. %0 myJob141112 http://MyServer.com/file.txt c:\temp\file.txt
if "%1"=="" goto end
Bitsadmin /create %1
Bitsadmin /addfile %1 %2 %3
Bitsadmin /resume %1
Bitsadmin /info %1 /verbose
rem Bitsadmin /monitor
:end

Note that some servers dont support downloading with this. Also there is a parameter to notify you when the download is complete. Hope this helps someone.