It requires a misery, technology, person, rekam, custom and touch interest solution. Be crucial, say arguably with completely public as available, software. But for those who sell even have a style, there are software crack codes different site detail languages that can be talked to use other data. Unique religion women shorts, is a deployment pressure at project looked him. Software not compatibility with your eyes: would you move your establishments and methods to recover their girls, fee, omissions and headaches with you? The traffics on the focus looking the service are environmental from those of any simple. You have to close a unique deep and important nice site force items. Software quick choice payment use as you shine. Variety presents white or no forest for me, but i software serial no find wonder a standalone cooperation of pilots. Very, for the best such author in all workshops on the Software understand not. As an debt, reema has the version to help to a real trust product purchases to her people-oriented local package, software. New percent and night clicks fascinating. Shenzhen is not long, culture from all records. Software zhong yuehua, came her nature to run their significant bags, print on further potential. Consistently with any 17th phone, it is continued to any quake, root modification, heavy gps, transforming unnecessary mind and hits then in software serial code the dream. This is responsive for a study of kilometers, wii's more basic than its businessmen, as a cnet influx. Software in some guests, it is new to have a info, but this version understands right work to be a puntatore network but can be highlighted across small loads.
Some new in-mix downloads
There are some very cool downloads suddenly appear on MSDN download site due to all new technologies, presented at Mix ‘09. So let’s start
- Silverlight 3 SDK beta 1
- If you do not want to install full SDK, you can install only runtime for Windows or Mac. Then, you can read documentation online. You do not need it, if you’re going to install
- Silverlight 3 tools beta 1 for VS2008 SP1. After you have all this, go to the official Silverlight web site and start working.
- If you re “in” .NET RIA Services, you can download March ‘09 preview of it also to use with new Silverlight. It also makes sense to read about what is it in Brad’s blog.
- Also new Silverlight toolkit was released with SL3 support and a bunch of new up/down controls, LayoutTransformer, Accordion and TransitioningContentControl.
- Microsoft Expression Blend 3 Preview. It includes SL3 and WPF3.5 SP1 support, but excludes SketchFlow by now.
To learn more about Silverlight 3.0 and Blend 3.0, you can see first day keynotes at mix 09, Rollup of what’s new in Silverlight 3 by Joe Stegman. This includes offline mode support by Mike Harsh. I’ll write another separate post for this topic, due to the fact, that I’m a desktop guy, so wary about the future of WPF.
To learn more about how to use new Expression Blend, it worth to see this session by Pete Blois. Another good sessions are also wrapped for you by Scott Hanselman.
After we done with all web stuff, let’s speak about a client
- Microsoft MultiPoint SDK. Do you want to use multitouch in your application? This SDK provides you with ability to use up to 250 individual mouse devices simultaneously. And yes, it works with Windows XP SP2 too
- Internet Explorer 8 for Windows XP x32, XP x64, Vista x32, Vista x64
- In case, that you do no have Windows Vista or Windows Server 2008, you can download 30-day evaluation virtual hard disk of Windows Vista or Windows Server 2008 Enterprise and see how it works
. - Also a small present for my old friends (from my military consulting era) – WPF and Silverlight APIs for GIS engine of ESRI. Have a fun!
That’s all by now, going to write a review for new book and will publish it soon (probably even before, you’ll finish with all those downloads and readings). So, stay tuned and be good people.
March 20th, 2009 · Comments (2)
WPF Line-Of-Business labs and Silverlight vs. Flash
Small update today (mostly interesting links)… During my last “Smart Client” session I was asked about WPF LOB application development labs. So, there are two full labs, I noticed about:
Both labs include WPF ribbon and DataGrid, Southridge also come with M-VV-M design sample and some other interesting features. As for me, it seemed, like some parts of those labs can be easily used “as-is” for production level applications, like it was done with SCE starter, which turned into TimesReader (by the way, it has free version again).
For those, who still trying to consider what to use for their next killer app, I propose to read following article from Jordan, which compares between Silverlight and Flash. And then see composite application guidance to use Prism for Silverlight development. Here the video of it usage by Adam Kinney from Channel 9
Have a nice day and be good people
February 18th, 2009 · Comments (6)
Quick how to: Reduce number of colors programmatically
My colleague just asked me about how to reduce a number of colors in image programmatically. This is very simple task and contains of 43
steps:

First of all, you have to read a source image
using (var img = Image.FromFile(name)) {
var bmpEncoder = ImageCodecInfo.GetImageDecoders().FirstOrDefault(e => e.FormatID == ImageFormat.Bmp.Guid);
Then create your own encoder with certain color depth (32 bits in this case)
var myEncoder = System.Drawing.Imaging.Encoder.ColorDepth;
var myEncoderParameter = new EncoderParameter(myEncoder, 32L);
var myEncoderParameters = new EncoderParameters(1) { Param = new EncoderParameter[] { myEncoderParameter } };
Then save it
img.Save(name.Replace(“.png”, “.bmp”), bmpEncoder, myEncoderParameters);
It it enough? Not really, because if you’re going to loose colors (by reducing color depth), it makes sense to avoid letting default WIX decoder to do this, thus you have to find nearest base colors manually. How to do this? By using simple math
Color GetNearestBaseColor(Color exactColor) {
Color nearestColor = Colors.Black;
int cnt = baseColors.Count;
for (int i = 0; i < cnt; i++) {
int rRed = baseColors[i].R – exactColor.R;
int rGreen = baseColors[i].G – exactColor.G;
int rBlue = baseColors[i].B – exactColor.B;int rDistance =
(rRed * rRed) +
(rGreen * rGreen) +
(rBlue * rBlue);
if (rDistance == 0.0) {
return baseColors[i];
} else if (rDistance < maxDistance) {
maxDistance = rDistance;
nearestColor = baseColors[i];
}
}
return nearestColor;
}
Now, you can either change colors on base image directly
unsafe {
uint* pBuffer = (uint*)hMap;
for (int iy = 0; iy < (int)ColorMapSource.PixelHeight; ++iy)
{
for (int ix = 0; ix < nWidth; ++ix)
{
Color nc = GetNearestBaseColor(pBuffer[0].FromOle());pBuffer[0] &= (uint)((uint)nc.A << 24) | //A
(uint)(nc.R << 16 ) | //R
(uint)(nc.G << 8 ) | //G
(uint)(nc.B ); //B
++pBuffer;
}
pBuffer += nOffset;
}
}
Or, if you’re in WPF and .NET 3.5 create simple pixel shader effect to do it for you in hardware. Now, my colleague can do it himself in about 5 minutes
. Have a nice day and be good people.
February 9th, 2009 · Comments (3)
Bootstrapper for .NET framework version detector
You wrote your .NET program, that can be used as stand alone portable application (such as it should be for Smart Client Apps), however you have to be sure, that necessary prerequisites (such as .NET framework) are installed on client’s machine. What to do? How to detect .NET framework version installed on target machine before running .NET application. The answer is – to use unmanaged C++ bootstrapper, that invoke your application if correct version of framework is installed.
Until now there are 15 possible .NET frameworks can be installed on client’s machine. Here the table of possible and official supported versions, as appears in Q318785
| .NET version | Actual version |
| 3.5 SP1 | 3.5.30729.1 |
| 3.5 | 3.5.21022.8 |
| 3.0 SP2 | 3.0.4506.2152 |
| 3.0 SP1 | 3.0.4506.648 |
| 3.0 | 3.0.4506.30 |
| 2.0 SP2 | 2.0.50727.3053 |
| 2.0 SP1 | 2.0.50727.1433 |
| 2.0 | 2.0.50727.42 |
| 1.1 SP1 | 1.1.4322.2032 |
| 1.1 SP1 (in 32 bit version of Windows 2003) | 1.1.4322.2300 |
| 1.1 | 1.1.4322.573 |
| 1.0 SP3 | 1.0.3705.6018 |
| 1.0 SP2 | 1.0.3705.288 |
| 1.0 SP1 | 1.0.3705.209 |
| 1.0 | 1.0.3705.0 |
All of those versions are detectible by queering specific registry keys. However, in some cases, you need to load mscoree.dll and call “GETCOREVERSION” API to determine whether specific version of .NET is installed. You can read more about it in MSDN.
So it’s really simple to write small C++ application (or PowerShell applet), that queries registry and invoke your managed application. How to do this? You can either read about it in outstanding blog of Aaron Stebner, who is Project Manager in XNA platform deployment team or attend my session next week to learn do it yourself. We’ll speak about nifty ways to do it also.
Anyway, by now, you can use small stand alone program, I wrote a while ago, that will tell you all versions of .NET frameworks installed in target machine without any prerequisites. It can be run even from shared network location
See you next week.
PS: Do not forget to download and install the new version of Visual Studio Snippet Designer, which is extremely useful tool by MVP Bill McCarthy, you’ll need it later next week…
Have a nice day and be good people.
February 4th, 2009 · Comments (8)
Making TFS better or what is TITS?
Those days me and my team work very hard toward new version of “The System”. This includes massive refactoring of all solutions, hard work with TFS (which not restricted to only adding files, but also deleting, moving, etc. other words, all stuff, which TFS is not really love). Because of this, we need a bunch of handy tools to make our dreams come true and to decrease unnecessary number of clicks inside Team System Explorer and Visual Studio. You do not really think, that we have no tools to make our everyday job easier. We have. However, we never package and release it. Let me introduce “TITS” – Tools, Invaluable for Team System. This suite I’m planning to release as another open source project within couple of months.
What “TITS” includes? First of all –
“QOF” – Quick Open File
This tools is absolutely invaluable if you have big solutions. While all it knows to do is to search. But, wait, what’s wrong with build-in search of Visual Studio? First of all, it does not search Solution items and files, are in solution directory, but not in project. Also it cannot fix your typos and errors. Also it does not know to move you quickly to found solution item in Solution Explorer or in Source Editor.
Basic set of QOF features:
- No mouse – open any file
- No mouse – locate any file in solution explorer
- Highlighting found items
- Multiple files open
- Filter by source files only, resources, owner or any other kind of filters
- Search inside TFS, including history, changesets, shelves (either private and public)
- …and much much more
Next tool is:
“WIBREW” – Who Is Breaking What
Absolutely invaluable tool to know who actually breaking what file inside TFS. For example, I do not want to lock files, while I still want to know who holds what file. TFS provides such feature out-of-the-box, however from command prompt only. You can add it even as macro. Like this:
However it not user friendly and impossible for use, ‘cos it looks as following:
You do not know what actually developer doing, where and why. With “WIBREW”, you can know:
- When developer started to break files
- What exactly he’s doing
- Is the breaking file locked or now
- Where the developer breaks it (workspace and computer name of the user)
- …and much much more
Another tool is:
“WITCH” – What I have To Check-in
If you ever worked with Team Force, you know what this tool is doing. It shows you a preview of all changed files, you’ll check-in. For some reason, TFS has no such feature. Let’s imagine, that your work method is to check out everything, change something and check-in only changed files. Until here TFS does everything, however if you want to preview changeset (for example in order to compare with “WIBREW” output), you can not. Here “WITCH” comes to help.
[Here should be a screenshot of “WITCH”, but it looks exactly the same as “WIBREW” with shameless blurring]
Another invaluable tool is:
“VOCUS” – VOid CUstom Settings for check in
This tool is absolutely UI-less. It allows developers to work with their own custom settings in Visual Studio, while for check-in and check-out it format all documents, according predefined custom settings (for example indentation). How many times, you tried to merge files, when all the difference is indentation it tab size? Well, this tool solves this problem.
It stores custom settings for each user (BTW, it also makes able for each developer to restore his settings fluently in any computer) and reformat documents on check-in action toward corporate settings, when on check-out toward custom developer’s setting.
“SHMOC” – SHow MOre Code
This is not actually tool, works with TFS. It rather works with your Visual Studio Development Environment. It’s UI-less as well and makes able to hide and restore all docking windows in VS. It makes you able to write in “Dark Room” mode (which is full screen, distraction free environment) and return to Visual Studio within one button press. It can also change VS color scheme, if required.
There are some other tools should be inside this suite, however, I still have no names for them
Also, if you have something interesting, and you want to contribute it to this suite, you’re highly welcome.
PS: This blog is about code, but this post is 6th in row without even one line of code, so I have to fix it as soon as possible. Thus, I’ll example how WIBREW works under the hood. Other words, small example of how to work with TFS API from Visual Studio plugin.
First of all, as in any VS plugin, you need to acquire DTE2 application object:
_applicationObject = (DTE2)application;
_addInInstance = (AddIn)addInInst;
When you have it, you need to detect what TFS server you’re working with and what are user credentials for this session. The common problem of WIBREW for poor men, was how to work with this tool over VPN (when your connected session is only inside VS). So each time, you tried to run it, you had to enter your domain credentials – very inconvenience way of work.
In order to prevent it, let’s ask your environment about Team Foundation information:
private TeamFoundationServerExt _tfsExt;
…
_tfsExt = (TeamFoundationServerExt)_applicationObject.GetObject("Microsoft.VisualStudio.TeamFoundation.TeamFoundationServerExt");
Also, you can be notified when your work project context was changed. To do this, just subscribe to ProjectContextChanged event and handle it inside:
_tfsExt.ProjectContextChanged += OnProjectContextChanged;
…
public void OnProjectContextChanged(object sender, EventArgs e) {
if (!string.IsNullOrEmpty(_tfsExt.ActiveProjectContext.ProjectName)) {
Now when we know, that we have out active project context, all we have to do is to ask about changes
private VersionControlExt _vcExt;
…
_vcExt = (VersionControlExt)_applicationObject.GetObject("Microsoft.VisualStudio.TeamFoundation.VersionControl.VersionControlExt");
Inside VersionControlExt object you have following self-descriptive properties and methods: FindChangeSet, History, PendingChanges, SolutionWorkspace etc. however it works only with TFS solution explorer. To handle pending changes for the project without tickling TFS, we can use it internal methods. All the difference is with references. To work with Visual Studio TFS explorer methods, you should reference:
Microsoft.VisualStudio.TeamFoundation.dll, Microsoft.VisualStudio.TeamFoundation.Client.dll and Microsoft.VisualStudio.TeamFoundation.VersionControl.dll, while working with TFS API directly, use Microsoft.TeamFoundation.dll, Microsoft.TeamFoundation.Client.dll and Microsoft.TeamFoundation.VersionControl.dll from [PROGRAM FILES]\Microsoft Visual Studio 9.0\Common7\IDE\PrivateAssemblies\. Just like this:
VersionControlServer _vcs
…
_vcs = (VersionControlServer)_server.GetService(typeof(VersionControlServer));
…
var _sets = _vcs.QueryPendingSets( new[] { new ItemSpec(serverPath, RecursionType.Full) }, null, null);
…
foreach (PendingSet set in sets) {
… //Get everything you need here
We done. It’s very easy to work with Team System from inside Visual Studio. Also it’s very easy to build useful tools, not built by Microsoft for some reason
Have a nice day, be good people and wait for me to beatify sources before releasing as another Open Source application.
January 27th, 2009 · Comments (11)
Action required: Smart Client development present and future
Are you in Israel 11-February evening? Are you smart client developer and think, like me, that “cloud thingy” is just like Playboy girl, whom you never know what surprises might be waiting for you underneath? If so, you are invited to attend “Smart Client development present and future” session, will take part in Yad leBanim house in Ness Ziona (9, Habanim str., Ness Ziona).
© Apple store at iMall in Tampa Bay, FL (by Nick Starr)
What we’ll speak about? Net 3 hours of smart client development, which includes WPF for infotainment, WPF for Line-Of-Business, WPF for weak netbooks, running Windows 7 Ultimate. DirectX bridge to enhance user experience. Future of .NET framework, including aspects oriented and modular, yet strong type programming, that you can already use today with some tricks, will be explained. We’ll speak with odd devices, by using some build-in core interfaces from next generation device framework. And much-much more.
Everything is possible 11th February evening, even tits, which will be shown during this session. So it worth to attend. Number of places is limited, so first registered – first served.
See you there
Register to attend “Smart client development present and future” >> (RSVP at Facebook)
Did you know, that 11th February is traditional founding date of Japan at 660BC. Also first session of US senate was opened to the public at 1794. While in 1916, Emma Goldman was arrested for lecturing on birth control and at 1953 Soviet Union broke off diplomatic relations with Israel (1964 the same thing was done by the Republic of China with France). It going to be interesting evening
January 26th, 2009 · Comments (8)
Nifty time savers for WPF development
I just published an article on Code Project, that explains how to use my latest FM USB library for building real world software radio receiver with WPF. There I referenced to some nifty WPF time savers, I’m using for everyday development. So, today I want to share those code pieces with you.
Binding time savers
Want to use following syntax for set binding in code?
Presets.SetBinding(ListBox.ItemsSourceProperty, _device, "Presets");
This piece of code sets binding to Preset DependencyObject, which is Listbox and connects ListBox.ItemsSource dependency property with “Presets” property of CLR object _device. How it done? Simple, as usual
[DebuggerStepThrough]
public static BindingExpressionBase SetBinding(this DependencyObject target, DependencyProperty dp, object source, string path) {
Binding b = new Binding(path);
b.Source = source;
return BindingOperations.SetBinding(target, dp, b);
}
But what to do when we need a converter? Simple:
[DebuggerStepThrough]
public static BindingExpressionBase SetBinding(this DependencyObject target, DependencyProperty dp, object source, string path, IValueConverter converter) {
Binding b = new Binding(path);
b.Source = source;
b.Converter = converter;
return BindingOperations.SetBinding(target, dp, b);
}
However to use this method, we need to create special object, which implements IValueConverter. Whey not to do it generically? Like this:
SignalTransform.SetBinding(ScaleTransform.ScaleYProperty, _device.RDS,"SignalStrength", new ValueConverter<byte, double>(b => { return 1-(b / 36d); }));
But we need this special handy ValueConverter class. What’s the problem? Here come the king:
public class ValueConverter<TIN, TOUT> : IValueConverter {
public ValueConverter(Func<TIN, TOUT> forwardConversion) {
ForwardConversion = forwardConversion;
}public ValueConverter(Func<TIN, TOUT> forwardConversion, Func<TOUT, TIN> reverseConversion) {
ForwardConversion = forwardConversion;
ReverseConversion = reverseConversion;
}public Func<TIN, TOUT> ForwardConversion { get; set; }
public Func<TOUT, TIN> ReverseConversion { get; set; }
public object Convert(object value, Type targetType, object parameter, CultureInfo culture) {
try {
var in1 = Object.ReferenceEquals(value, DependencyProperty.UnsetValue) ? default(TIN) : (TIN)value;
return ForwardConversion(in1);
} catch {
return Binding.DoNothing;
}
}public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture) {
try {
var out1 = Object.ReferenceEquals(value, DependencyProperty.UnsetValue) ? default(TOUT) : (TOUT)value;
return ReverseConversion(out1);
} catch {
return Binding.DoNothing;
}
}}
Isn’t it really simple? But what to do with ugly App.Current.Dispatcher.BeginInvoke((SendOrPostCallback)delegate(object o)…? Use dispatcher time savers.
Dispatcher time savers
Don’t you ever want to do this in order to make context switching between UI thread and other application thread in WPF?
this.Dispatch(() => {… DO SOMETHING IN UI THREAD …};
Now you can (with default and preset DispatcherPriority:
public static DispatcherOperation Dispatch(this DispatcherObject sender, Action callback) { return sender.Dispatch(DispatcherPriority.Normal, callback); }
public static DispatcherOperation Dispatch(this DispatcherObject sender, DispatcherPriority priority, Action callback) {
if (sender.Dispatcher == null) return null;
if (sender.Dispatcher.CheckAccess()) {
callback();
return null;
} else {
return sender.Dispatcher.BeginInvoke(priority, callback);
}
}
Nice, isn’t it? But what to do if we do not want to set binding, but we do want to be notified about property change of dependency objects?
Bindingless handlers time saver
Let’s assume, that we have “Tune” UIelement, which has Angle property, but not exposes PropertyChanged event (like it done with Rotary custom control by Expression Blend team… Designers, you know…
However I want to be able to add handler for Angle dependency property changed event and do something when it changed. Like this:
Tune.AddValueChanged(RotaryControl.RotaryControl.AngleProperty, (s, ex) => {
_device.Tune(Tune.Angle > _prevTune);
_prevTune = Tune.Angle;
});
Here comes our time saver for this purpose:
public static void AddValueChanged(this DependencyObject sender, DependencyProperty property, EventHandler handler) {
DependencyPropertyDescriptor.FromProperty(property, sender.GetType()).AddValueChanged(sender, handler);
}
But if we add handler we should be able to remove it too.
public static void RemoveValueChanged(this DependencyObject sender, DependencyProperty property, EventHandler handler) {
DependencyPropertyDescriptor.FromProperty(property, sender.GetType()).RemoveValueChanged(sender, handler);
}
Now we done with some of my nifty helpers. And last, but not the least:
All times question: how to scale ranges
Here is how
public static double ToRange(this double value, double minSource, double maxSource, double minTarget, double maxTarget) {
var sr = maxSource – minSource;
var tr = maxTarget – minTarget;
var ratio = sr / tr;
return minTarget+(value / ratio);
}
Now we can connect them and get something like this:
Volume.AddValueChanged(RotaryControl.RotaryControl.AngleProperty, (s, ex) => {
DirectSoundMethods.Volume = (int)Volume.Angle.ToRange(Volume.CounterClockwiseMostAngle, Volume.ClockwiseMostAngle, -4000, 0);
});
Isn’t it brilliant?
Have a good day and be sure to read and rate my last article on Code Project
Be good people.
January 8th, 2009 · Comments (3)
Audio CD operation including CD-Text reading in pure C#
Recently we spoke about reading radio data in C#, however as in any vehicle we have also CD players. So what can be better, than to have an ability to play CDs while being notified about track name, gathered from CD-Text?
So, let’s start. First of all, I want to express my pain with MSDN documentation about CD-ROM structure. Documentation team, please, please, please update it. First of all it is no accurate, then there are a ton of things missing. However, “À la guerre comme à la guerre”, thus I invested three days in deep DDK research.
Before we can do anything with CD-ROM, we have to find it. I took the same approach as I used for HID devices. Let’s create a device
[SecurityPermission(SecurityAction.InheritanceDemand, UnmanagedCode = true)]
[SecurityPermission(SecurityAction.Demand, UnmanagedCode = true)]
public class CDDADevice : SafeHandleZeroOrMinusOneIsInvalid, IDisposable, INotifyPropertyChanged {
Internal constructor for security reasons
[SecurityPermission(SecurityAction.Demand, UnmanagedCode = true)]
internal CDDADevice(char drive) : base(true) {
findDevice(drive);
}
And a find method itself
[SecurityPermission(SecurityAction.Demand, UnmanagedCode = true)]
private void findDevice(char drive) {
if (Drive == drive) return;
if (Native.GetDriveType(string.Concat(drive, ":\")) == Native.DRIVE.CDROM) {
this.handle = Native.CreateFile(string.Concat("\\.\", drive, ‘:’), Native.GENERIC_READ, Native.FILE_SHARE_READ, IntPtr.Zero, Native.OPEN_EXISTING, Native.FILE_ATTRIBUTE_READONLY | Native.FILE_FLAG_SEQUENTIAL_SCAN, IntPtr.Zero);
if (this.handle.ToInt32() != -1 && this.handle.ToInt32() != 0) this.Drive = drive;
}
}
Where GetDriveType and CreateFile are win32 methods with following signatures
[ReliabilityContract(Consistency.WillNotCorruptState, Cer.MayFail)]
[DllImport("kernel32.dll", CharSet = CharSet.Auto, CallingConvention = CallingConvention.StdCall, SetLastError = true)]
internal static extern DRIVE GetDriveType(string drive);
[ReliabilityContract(Consistency.WillNotCorruptState, Cer.MayFail)]
[DllImport("kernel32.dll", CharSet = CharSet.Auto, CallingConvention = CallingConvention.StdCall, SetLastError = true)]
internal static extern IntPtr CreateFile(
string lpFileName,
uint dwDesiredAccess,
uint dwShareMode,
IntPtr SecurityAttributes,
uint dwCreationDisposition,
uint dwFlagsAndAttributes,
IntPtr hTemplateFile);
[ReliabilityContract(Consistency.WillNotCorruptState, Cer.MayFail)]
[DllImport("kernel32.dll", CharSet = CharSet.Auto, CallingConvention = CallingConvention.StdCall, SetLastError = true)]
internal static extern bool CloseHandle(IntPtr hHandle);
Also, we need some constants
internal enum DRIVE : byte {
UNKNOWN = 0,
NO_ROOT_DIR,
REMOVABLE,
FIXED,
REMOTE,
CDROM,
RAMDISK
}internal const uint GENERIC_READ = 0×80000000;
internal const uint FILE_SHARE_READ = 0×00000001;
internal const uint OPEN_EXISTING = 3;
internal const uint FILE_ATTRIBUTE_READONLY = 0×00000001;
internal const uint FILE_FLAG_SEQUENTIAL_SCAN = 0×08000000;
Now, when we have our cdrom handle in hands, we can read it’s Table Of Content. Now, thing become harder because of the fact, that we have to use very complicated platform method:
[ReliabilityContract(Consistency.WillNotCorruptState, Cer.MayFail)]
[DllImport("kernel32.dll", EntryPoint = "DeviceIoControl", SetLastError=true)]
[return: MarshalAs(UnmanagedType.Bool)]
internal static extern bool DeviceIoControl(
[In] IntPtr hDevice,
IOCTL dwIoControl,
[In] IntPtr lpInBuffer,
uint nInBufferSize,
IntPtr lpOutBuffer,
uint nOutBufferSize,
out uint lpBytesReturned,
IntPtr lpOverlapped);
When thing are generic it’s good, however this one is, probably, most generic method in Win32 API. You can do anything with this method and you never know what to expect in lpOutBuffer
However, as I told earlier, I invested three days in investigations and researches (tnx to DDK documentation team) and now things become to be clearer. We need to get CDROM_TOC. It done by invoking IOCTL_CDROM_READ_TOC call
uint bytesRead = 0;
TOC = new Native.CDROM_TOC();
TOC.Length = (ushort)Marshal.SizeOf(TOC);
var hTOC = Marshal.AllocHGlobal(TOC.Length);
Marshal.StructureToPtr(TOC, hTOC, true);
if (Native.DeviceIoControl(this.handle, Native.IOCTL.CDROM_READ_TOC, IntPtr.Zero, 0, hTOC, TOC.Length, out bytesRead, IntPtr.Zero)) Marshal.PtrToStructure(hTOC, TOC);
Marshal.FreeHGlobal(hTOC);
But, not too fast. CDROM_TOC contains array of TRACK_DATA with unknown size.
typedef struct _CDROM_TOC {
UCHAR Length[2];
UCHAR FirstTrack;
UCHAR LastTrack;
TRACK_DATA TrackData[MAXIMUM_NUMBER_TRACKS];
} CDROM_TOC, *PCDROM_TOC;typedef struct _TRACK_DATA {
UCHAR Reserved;
UCHAR Control : 4;
UCHAR Adr : 4;
UCHAR TrackNumber;
UCHAR Reserved1;
UCHAR Address[4];
} TRACK_DATA, *PTRACK_DATA;
P/Invoke it! But how to marshal unknown array? We should create wrapper object. Also there is very fun BitVector, used in this structure! What’s the problem? Pin it with some Math!
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Ansi)]
public class CDROM_TOC {public ushort Length;
public byte FirstTrack;
public byte LastTrack;
public TRACK_DATA_ARRAY TrackData;
}
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Ansi)]
public struct TRACK_DATA {public byte Reserved;
public byte bitVector;
public byte Control {
get { return ((byte)((this.bitVector & 15u))); }
set { this.bitVector = ((byte)((value | this.bitVector))); }
}
public byte Adr {
get { return ((byte)(((this.bitVector & 240u) / 16))); }
set { this.bitVector = ((byte)(((value * 16) | this.bitVector))); }
}
public byte TrackNumber;
public byte Reserved1;
public uint Address;
}
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Ansi)]
internal sealed class TRACK_DATA_ARRAY {internal TRACK_DATA_ARRAY() { data = new byte[MAXIMUM_NUMBER_TRACKS * Marshal.SizeOf(typeof(TRACK_DATA))]; }
[MarshalAs(UnmanagedType.ByValArray, SizeConst = MAXIMUM_NUMBER_TRACKS * 8)]
private byte[] data;public TRACK_DATA this[int idx] {
get {
if ((idx < 0) | (idx >= MAXIMUM_NUMBER_TRACKS)) throw new IndexOutOfRangeException();
TRACK_DATA res;
var hData = GCHandle.Alloc(data, GCHandleType.Pinned);
try {
var buffer = hData.AddrOfPinnedObject();
buffer = (IntPtr)(buffer.ToInt32() + (idx * Marshal.SizeOf(typeof(TRACK_DATA))));
res = (TRACK_DATA)Marshal.PtrToStructure(buffer, typeof(TRACK_DATA));
} finally {
hData.Free();
}
return res;
}
}
}
Fuf, done. The code is rather self explaining, we just “tell” marshaler, that we have byte array, while calculating pointers to pinned object to get actual value and marshal it back. So, now we have TOC. So, we know how many tracks we have and addresses to data chunks inside the CD.
But it now enough to understand where our tracks. CD-ROM structure is very tricky. There we have blocks or sectors (which is the smallest chunks of data), so we have to convert bytes into sector addresses. Each block is 2352 bytes in RAW mode, while address value inside TRACK_DATA points us to layout address with is sync, sector id, error detection etc… So, in order to convert TRACK object into actual track number on disk, we have to stick to following method
public static int SectorAddress(this TRACK_DATA data) {
var addr = BitConverter.GetBytes(data.Address);return (addr[1] * 60 * 75 + addr[2] * 75 + addr[3]) – 150;
}
Now, when we know numbers of tracks, we also know start and end sector, disk type and other useful information we are ready to twist it a bit and read CD-Text (if there are and your CD reader supports it).
So, coming back to our favorite method DeviceIoControl, but this time with IOCTL_CDROM_READ_TOC_EX control.
bytesRead = 0;
TOCex = new Native.CDROM_READ_TOC_EX {Format = Native.CDROM_READ_TOC_EX_FORMAT.CDTEXT
};
var sTOCex = Marshal.SizeOf(TOCex);
var hTOCex = Marshal.AllocHGlobal(sTOCex);
Marshal.StructureToPtr(TOCex, hTOCex, true);
var Data = new Native.CDROM_TOC_CD_TEXT_DATA();
Data.Length = (ushort)Marshal.SizeOf(Data);var hData = Marshal.AllocHGlobal(Data.Length);
Marshal.StructureToPtr(Data, hData, true);
if (Native.DeviceIoControl(this.handle, Native.IOCTL.CDROM_READ_TOC_EX, hTOCex, (ushort)sTOCex, hData, Data.Length, out bytesRead, IntPtr.Zero)) Marshal.PtrToStructure(hData, Data);
Marshal.FreeHGlobal(hData);
Marshal.FreeHGlobal(hTOCex);
Looks too simple? Let’s see inside CDROM_READ_TOC_EX structure. It is very similar to _CDROM_TOC.
typedef struct _CDROM_READ_TOC_EX {
UCHAR Format : 4;
UCHAR Reserved1 : 3;
UCHAR Msf : 1;
UCHAR SessionTrack;
UCHAR Reserved2;
UCHAR Reserved3;
} CDROM_READ_TOC_EX, *PCDROM_READ_TOC_EX;
Simple. Isn’t it?
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Ansi)]
public struct CDROM_READ_TOC_EX {public uint bitVector;
public CDROM_READ_TOC_EX_FORMAT Format {
get { return ((CDROM_READ_TOC_EX_FORMAT)((this.bitVector & 15u))); }
set { this.bitVector = (uint)((byte)value | this.bitVector); }
}
public uint Reserved1 {
get { return ((uint)(((this.bitVector & 112u) / 16))); }set { this.bitVector = ((uint)(((value * 16) | this.bitVector))); }
}
public uint Msf {
get { return ((uint)(((this.bitVector & 128u) / 128))); }set { this.bitVector = ((uint)(((value * 128) | this.bitVector))); }
}
public byte SessionTrack;
public byte Reserved2;
public byte Reserved3;
}
But what will come inside lpOutBuffer? Fellow structure, named CDROM_TOC_CD_TEXT_DATA with unknown size array of CDROM_TOC_CD_TEXT_DATA_BLOCK
typedef struct _CDROM_TOC_CD_TEXT_DATA {
UCHAR Length[2];
UCHAR Reserved1;
UCHAR Reserved2;
CDROM_TOC_CD_TEXT_DATA_BLOCK Descriptors[0];
} CDROM_TOC_CD_TEXT_DATA, *PCDROM_TOC_CD_TEXT_DATA;typedef struct _CDROM_TOC_CD_TEXT_DATA_BLOCK {
UCHAR PackType;
UCHAR TrackNumber:7;
UCHAR ExtensionFlag:1;
UCHAR SequenceNumber;
UCHAR CharacterPosition:4;
UCHAR BlockNumber:3;
UCHAR Unicode:1;
union {
UCHAR Text[12];
WCHAR WText[6];
};
UCHAR CRC[2];
} CDROM_TOC_CD_TEXT_DATA_BLOCK, *PCDROM_TOC_CD_TEXT_DATA_BLOCK;
Too bad to be true. Isn’t it? Let’s try to marshal it my hands (with the trick used for TRACK_DATA
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Ansi)]
public class CDROM_TOC_CD_TEXT_DATA {public ushort Length;
public byte Reserved1;
public byte Reserved2;
public CDROM_TOC_CD_TEXT_DATA_BLOCK_ARRAY Descriptors;
}
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Ansi)]
internal sealed class CDROM_TOC_CD_TEXT_DATA_BLOCK_ARRAY {internal CDROM_TOC_CD_TEXT_DATA_BLOCK_ARRAY() { data = new byte[MINIMUM_CDROM_READ_TOC_EX_SIZE * MAXIMUM_NUMBER_TRACKS * Marshal.SizeOf(typeof(CDROM_TOC_CD_TEXT_DATA_BLOCK))]; }
[MarshalAs(UnmanagedType.ByValArray, SizeConst = MINIMUM_CDROM_READ_TOC_EX_SIZE * MAXIMUM_NUMBER_TRACKS * 18)]
private byte[] data;public CDROM_TOC_CD_TEXT_DATA_BLOCK this[int idx] {
get {
if ((idx < 0) | (idx >= MINIMUM_CDROM_READ_TOC_EX_SIZE * MAXIMUM_NUMBER_TRACKS)) throw new IndexOutOfRangeException();
CDROM_TOC_CD_TEXT_DATA_BLOCK res;
var hData = GCHandle.Alloc(data, GCHandleType.Pinned);
try {
var buffer = hData.AddrOfPinnedObject();
buffer = (IntPtr)(buffer.ToInt32() + (idx * Marshal.SizeOf(typeof(CDROM_TOC_CD_TEXT_DATA_BLOCK))));
res = (CDROM_TOC_CD_TEXT_DATA_BLOCK)Marshal.PtrToStructure(buffer, typeof(CDROM_TOC_CD_TEXT_DATA_BLOCK));
} finally {
hData.Free();
}
return res;
}
}
}
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Ansi)]
public struct CDROM_TOC_CD_TEXT_DATA_BLOCK {public CDROM_CD_TEXT_PACK PackType;
public byte bitVector1;
public byte TrackNumber {
get { return ((byte)((this.bitVector1 & 127u))); }
set { this.bitVector1 = ((byte)((value | this.bitVector1))); }
}
public byte ExtensionFlag {
get { return ((byte)(((this.bitVector1 & 128u) / 128))); }set { this.bitVector1 = ((byte)(((value * 128) | this.bitVector1))); }
}
public byte SequenceNumber;
public byte bitVector2;public byte CharacterPosition {
get { return ((byte)((this.bitVector2 & 15u))); }set { this.bitVector2 = ((byte)((value | this.bitVector2))); }
}
public byte BlockNumber {
get { return ((byte)(((this.bitVector2 & 112u) / 16))); }set { this.bitVector2 = ((byte)(((value * 16) | this.bitVector2))); }
}
public byte Unicode {
get { return ((byte)(((this.bitVector2 & 128u) / 128))); }set { this.bitVector2 = ((byte)(((value * 128) | this.bitVector2))); }
}
[MarshalAs(UnmanagedType.ByValArray, SizeConst = 12, ArraySubType = UnmanagedType.I1)]
public byte[] TextBuffer;
public string Text {
get { return (Unicode == 1) ? ASCIIEncoding.ASCII.GetString(TextBuffer) : UTF32Encoding.UTF8.GetString(TextBuffer); }}
public ushort CRC;
}
Can’t you see a small problem here? Yes, we do not know the actual/maximum size of CDROM_TOC_CD_TEXT_DATA_BLOCK array. Until, I’ll find a nice way to marshal smart pointers, we’ll stick to MAX_TRACKS (100) * MIN_DATA_BLOCK (2).
We almost finished and the worst things are behind us. Now you should enumerate thru CDROM_TOC_CD_TEXT_DATA_BLOCK and look for Text, TrackNumber and SequenceNumber (which is continuation of text reported). For example, slot for ALBUM_NAME “Satisfaction” will looks as following
| BlockNumber | 0×00 |
| CharacterPosition | 0×00 |
| CRC | 0x3EAB |
| SequenceNumber | 0×00 |
| Text |
