Implementation of Interop101 Blog Series (C#, C++, C++/CLI) on Github

Recently the Interop101Series blog articles on MSDN helped me review
all of the various Interop modes for a project.

Interop 101 - Part 1
Interop 101 - Part 2
Interop 101 - Part 3
Interop 101 - Part 4
Interop 101 - Part 5

Everyone has different experiences with Interop. Many of us have
done much in the past, but details are lost in memory. It can be
painful to consider various Interop options, because there are many
different ways to do the same thing. There are advantages and
disadvantages of each method. So, how easy is it to test every
method?

A great way to quickly familiarize yourself with each Interop
method is to implement the code for each Interop101Series blog
article in Visual Studio 2010.

The blog articles are well written. But, I did not find complete
code samples, nor a solution bundle, outside the blog snippets.
The blog articles are also five years old, and Visual Studio 2010
is different. There are different properties, templates, COM
attributes, etc. 

But, not to worry, because now there is a single Visual Studio 2010
solution on Github implementing the complete blog series in
distinct projects. Enjoy!

Interop101Series Solution on Github

// Demonstrate 5 major scenarios.
// (1)(a) Client in C++/CLI calls the native code, and
      (b) Native client calls Native DLL
// (2) Client in C# calls the native code using its
       built-in interop functionality (a.k.a. P/Invoke)
// (3) Client in C# calls the native code via a COM interface
// (4) Client in C# calls the native code via a C++/CLI wrapper
// (5)(a) Native client calls the C# code (DLL)
      via C++/CLI wrapper and
      (b) CLR Client calls C# code (DLL)

Another (secret) reason to review the Interop methods is to
test Visual Studio 11, and Windows 8 features. Let's see what
has changed, and what stays the same. :) 
Posted in Uncategorized | Leave a comment

Random() Implementation for Rock, Paper, Azure!

I recently attended one of the Rock, Paper, Azure! training sessions. Fun times. We learned how to quickly build, test Azure applications locally, and deploy them to the internet. However, I was perplexed by some of the MyBot API features. My naive .Net coding attempts were clearly not working once deployed on Azure. This article explains the problem using a not thread safe .NET Random() implementation. Finally, we show how to create an Azure thread safe full implementation of System.Random().

The basic Rock, Paper, Azure! ideas are for using API features from the reference library RockPaperScissorsPro.RPA. Trainees implement various strategies for playing the game inside a MakeMove() method. The method signature is:

public Move MakeMove(IPlayer you, IPlayer opponent, GameRules rules)

In this blog article we have three examples. (1) The NotRandom class shows the problems with non-thread safe coding of Random() and bad results running on Azure. (2) The MovesRandom class shows a working example with the very simple Rock, Paper, Azure! API random integer method. (3) The final DNetRandom and RandomGenerator classes show a working example with full System.Random() features, and safe for running on Azure.

Here we go….

Example One – NotRandom

This example shows one naive attempt to implement System.Random(). There are lots of ways to do it wrong. In this case we re-seed the Random() function using a sequence (not thread safe) and select the NextDouble() for game play. What we expect is 25% of the time getting Scissors, and 75% of the time choosing Paper.

Source code for the NotRandom.cs class follows:

using RockPaperScissorsPro;
using System;

namespace RockPaperAzure
{
    public class NotRandom : IRockPaperScissorsBot
    {
        private static int mSeed = 0;

        // Random sample implementation
        public Move MakeMove(IPlayer you, IPlayer opponent, GameRules rules)
        {
            Move rtn;

            // seed, Int32.MaxValue == 2,147,483,647
            if (mSeed >= Int32.MaxValue)
                mSeed = 0;
            Random rand = new Random(mSeed++);
            // returns a random number between 0.0 and 1.0
            double randDouble = rand.NextDouble();

            // THIS IS FAIL
            if (randDouble > 0.25)
                rtn = Moves.Paper;
            else
                rtn = Moves.Scissors;

            return rtn;
        }
    }
}

This simply does *not* work. First we load all three libraries in the Bot Lab either locally or in Azure. We click the Start Battle button to generate game results. The load & game results are shown here:

 

The problem with the naive (non thread safe) random generation is shown in the logs. The NotRandom distribution we get is a repeated pattern of five or six Scissors (~25%) & Paper pairs, followed by twelve Papers (~75%), and it repeats for the entire series. Yuck! An example from the NotRandom log (in second column) is shown in this image:

 

How do we fix this?

Example Two – MovesRandom

This example shows using the Rock, Paper, Scissors! API with method generating an Int32 random number, e.g. Moves.GetRandomNumber(int max). This simple implementation works, but is very basic providing only integers. What we get is 25% of the time choosing Scissors, and 75% of the time choosing Paper.

Source code for the MovesRandom.cs class follows:

using RockPaperScissorsPro;
using System;

namespace RockPaperAzure
{
    public class MovesRandom : IRockPaperScissorsBot
    {
        // Random sample implementation
        public Move MakeMove(IPlayer you, IPlayer opponent, GameRules rules)
        {
            Move mReturn;

            // APPEARS WORKING
            int random = Moves.GetRandomNumber(100);

            if (random > 25)
                mReturn = Moves.Paper;
            else
                mReturn = Moves.Scissors;

            return mReturn;
        }
    }
}

This works, but is too basic, providing only random integers. An example from the MovesRandom log (in second column) is shown in this image:

Working, but this Int32 random function is too basic for my needs.

Example Three – DNetRandom

The final example shows a working, complete implementation .NET System.Random(). This implementation is thread safe for Azure, and provides all the functionality of System.Random(). We get the same 25% of the time choosing Scissors, and 75% of the time choosing Paper.

Source code for the DNetRandom.cs class follows:

using RockPaperScissorsPro;
using System;

namespace RockPaperAzure
{
    public class DNetRandom : IRockPaperScissorsBot
    {
        // Random sample implementation
        public Move MakeMove(IPlayer you, IPlayer opponent, GameRules rules)
        {
            Move mReturn;

            double random = RandomGenerator.NextDouble();

            // this is pass
            if (random > 0.25)
                mReturn = Moves.Paper;
            else
                mReturn = Moves.Scissors;

            return mReturn;
        }
    }
}

You need the thread safe RandomGenerator.NextDouble() implementation in the RandomGeneration.cs class. The source code follows:

using System;

namespace RockPaperAzure
{
    internal static class RandomGenerator
    {
        // Fields
        private static Random _global = new Random();

        // indicates that the value of a static field is unique for each thread
        [ThreadStatic]
        private static Random _local;

        // Methods
        public static double NextDouble()
        {
            return getLocal().NextDouble();
        }
        // Methods
        public static int Next()
        {
            return getLocal().Next();
        }
        // Methods
        public static int Next(int maxValue)
        {
            return getLocal().Next(maxValue);
        }
        // Methods
        public static int Next(int minValue, int maxValue)
        {
            return getLocal().Next(minValue, maxValue);
        }
        // Methods
        public static void NextBytes(byte[] buffer)
        {
            getLocal().NextBytes(buffer);
        }

        private static Random getLocal()
        {
            Random random = _local;
            if (random == null)
            {
                int num;
                // The lock keyword ensures that one thread does
                // not enter a critical section of code while
                // another thread is in the critical section.
                // If another thread tries to enter a locked code,
                // it will wait, block, until the object is released.
                lock (_global)
                {
                    num = _global.Next();
                }
                _local = random = new Random(num);
            }
            return random;
        }
    }
}

The getLocal() method and [ThreadStatic] decorator expose the *tricks* (or features) required to write the Random() implementation in a thread safe Azure way. Check MSDN documentation for the Lock Statement. You will need to create your own MyBot projects (libraries) with these classes, make minor name changes in the factory classes, and add project references for the Rock, Paper, Azure! library APIs.

This final example provides a full System.Random() thread safe implementation. An example from the DNetRandom log (in second column) is shown in this image:

 

This article might help you code thread safe implementations of other .Net methods for your Rock, Paper, Azure! games. You can use the same pattern and techniques. Be careful not to lock threads for too long or Azure will kill your threads – and you may lose the game.

Hope this helps!

m


 

Posted in Uncategorized | Leave a comment

“mo” program on github

Purpose: Set Console Window & Buffer sizes, QuickEdit, other options from command line. (read the README)

I love using the Microsoft Windows console for simple tasks. A frequent annoyance is setting the Command Prompt properties manually. We manually set Buffer Size, Window Size, QuickEdit mode, etc., using a mouse and navigating a bunch of tabs. When closing the dialog we can save settings that will apply to new Command Prompt windows. Yuck. I want something like the DOS “mode” command but improved to do all these manual tasks from the command line.

When using Git version control on Windows the Git Bash shell launches a Console Shell that has similar issues, but an additional problem is the modified settings can *not* be saved. Big yuck! Something had to be done to overcome these problems.

See similar complaints on stackoverflow: Is there a better Windows “Terminal” application?

To the rescue, the “mo” program fixes all these problems and more. The source code is posted on Github, along with the 4 required files. Simply place these 4 files anywhere in the WinOS path and it works.

The “mo” program also works on Windows screens at any resolution. It provides four presets: 0 = Small window 1/3 screen, 1 = Medium window 1/2 screen, 2 = Large window 2/3 screen, 3 = Max window almost full screen.

For example, lets look at a Git Bash shell opened on the c:\Windows directory:

The window is too small, and has only 300 lines in buffer. Also, QuickEdit cut and paste with mouse does not work. Running “mo” shows the shell status.

When we run a simple “mo” program command “mo 1 c=115″ with a screen resolution of 1920×1080 the situation is dramatically improved. The buffer size is increased to 2400 (preset 1) and the window and buffer columns are set to 115 which removes the horizontal scroll bar. The horizontal scroll bar appears if the buffer columns are greater than windows columns. Also, QuickEdit mode is enabled (preset 1), e.g.:

The weird bit to notice is the QuickEdit and Insert modes are unknown in a Bash Shell, but setting them true still enables cut & paste with the mouse. All of these features work similarly in a Command Prompt with DOS, plus the QuickEdit and Insert modes are known to be True or False under DOS.

There are a bunch of available commands in the “mo” program, which we can read by running “mo x”, e.g. shown below:

The source is posted on Github and no surprises were found. The “mo” program works as specified. It is amazing what we go through when the basic Windows utilities are lacking what we consider important command line utility features.

m

Enjoy!

Posted in Uncategorized | Leave a comment

Successful example use of Tangible’s C++ to Java Converter

Given a complex C++ application, consisting of 20 class files of about 100k bytes, I did a good bit of manual conversion before realizing I needed to check for automation solutions. I found Tangible from blog comments made by Dave, who works at Tangible, and he was offering conversion tips at various websites. Impressed by his ideas, I contacted Tangible who agreed to provide a limited license. I committed to provide this blog entry documenting my work – with upcoming posts of source and results!

The conversion project was a success. The tool I used to automate a portion of my work is Tangible’s “C++ to Java Converter”, version 2.4, build date Jan-04-2011.

The steps used were:

  1. Convert the old C++ source (DOS 8.3 file format) to compile in VS2010, including fixing bugs in the original source code.
  2. Manually, I was hitting a wall because of:
    1. taking a long time to manually complete,
    2. difficulty using a consistent style among techniques,
    3. challenges balancing time allocated between manually converting bulk code, or solving complex conversions,
    4. requiring minimal mistakes to reduce test cycles.
  3. Run Tangible’s C++ to Java Converter. Comparing converted code to my manual attempts, and the original code.
  4. Perform another manual pass through all target files, implementing conversions from the tool, and skipping (with comments) the most difficult pieces.
  5. Decide how to fix the difficult conversion segments, while maintaining the original source design as much as possible. This last bit was a good challenge requiring:
    1. when possible, extending conversion concepts in the tool,
    2. using Tangible’s syntax for consistency,
    3. fixing details I missed but were found by the tool,
    4. fixing conversion idiosyncrasies missed by the tool,
    5. liberal commenting and staging incomplete code in comments,
    6. running tests (as soon as possible), debugging fixes.
  6. Repeated tests, explore all code paths, debug and fix issues.
  7. Compare and match Java file IO format against the original code IO running in VS2010.
  8. Implement simple improvements, some optional bits, clean up final solution.

And, the results were a success. In a future blog post(s), I will be posting the sources, examples from the tool, and discussion of my work at various stages in the conversion process. Enjoy!

m

Posted in Uncategorized | Leave a comment

Android hierarchyviewer.bat file missing?

UI Android development with Hierarchy Viewer is awesome. I was missing the launch file, tools\hierarchyviewer.bat, and found it here:

http://android.git.kernel.org/?p=platform/sdk.git;a=blob_plain;f=hierarchyviewer/etc/hierarchyviewer.bat;hb=tools_r7

The contents:

@echo off
rem Copyright (C) 2008 The Android Open Source Project
rem
rem Licensed under the Apache License, Version 2.0 (the "License");
rem you may not use this file except in compliance with the License.
rem You may obtain a copy of the License at
rem
rem      http://www.apache.org/licenses/LICENSE-2.0
rem
rem Unless required by applicable law or agreed to in writing, software
rem distributed under the License is distributed on an "AS IS" BASIS,
rem WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
rem See the License for the specific language governing permissions and
rem limitations under the License.

rem don't modify the caller's environment
setlocal

rem Set up prog to be the path of this script, including following symlinks,
rem and set up progdir to be the fully-qualified pathname of its directory.
set prog=%~f0

rem Change current directory and drive to where the script is, to avoid
rem issues with directories containing whitespaces.
cd /d %~dp0

rem Check we have a valid Java.exe in the path.
set java_exe=
call lib\find_java.bat
if not defined java_exe goto :EOF

set jarfile=hierarchyviewer.jar
set frameworkdir=
set libdir=

if exist %frameworkdir%%jarfile% goto JarFileOk
 set frameworkdir=lib\

if exist %frameworkdir%%jarfile% goto JarFileOk
 set frameworkdir=..\framework\

:JarFileOk

set jarpath=%frameworkdir%%jarfile%

call %java_exe% -Xmx512m -Djava.ext.dirs=%frameworkdir% -Dhierarchyviewer.adb= -jar %jarpath% %*

Enjoy

m

Posted in Uncategorized | Leave a comment

Blog on WordPress 3.0

Back in June 2010 I decided as a trial to go with WordPress 3.0 for this blog, see details. So far I have been pretty happy with all the features, not many faults, good integration with Twitter, etc.

Now Microsoft Switches to WordPress for Windows Live-Based Blogs. Wow.

I guess my trial period is over, and I will be sticking with WordPress.

m

Posted in Uncategorized | Leave a comment

Request Error: Server Logs? on ASP.NET Developer Server (Cassini)

So, using VS2010 I created a new project with ASP.NET Empty Web Application. Next I added a new item WCF Data Service and some C# code behind. Running the service in the browser gives the non-specific error:

“Request Error: The server encountered an error processing the request. See server logs for more details.”

Aside from digging into supposed logs in some %temp% directory (which I never found) there is simply not enough information in the error message. What to do?

In this case the bug is on a class attribute seen below (typo CustomerID vs Id):

 [DataServiceKey("CustomerID")]
 public class CustomerRecord
 {
    public int CustomerId { get; set; }

The solution is to turn on IncludeExceptionDetailsInFaults = true.

 [System.ServiceModel.ServiceBehavior(
    IncludeExceptionDetailInFaults = true)]
 public class WcfDataService1 : DataService<SampleDataSource>

Now the error message has additional details required to solve the error.

The server encountered an error processing the request.
The exception message  is 'On data context type 'SampleDataSource',
there is a top IQueryable property  'SampleCustomerData' whose
element type is not an entity type. Make sure that  the
IQueryable property is of entity type or specify the IgnoreProperties
attribute on the data context type to ignore this property.'.
See server logs  for more details. The exception stack trace is: ...

Problem found and solved. Caution is required using the ServiceBehaviorAttribute.IncludeExceptionDetailInFaults Property because it is only recommended as a way of temporarily debugging a service application. The link shows another method of setting it in the application configuration file. Good luck.

Hope this helps.

m

Posted in Uncategorized | Leave a comment

TRIM active on Windows 7?

I recently read an interesting SSD review with lots of good information, including a method to test for TRIM support.

Kingston SSD NOW V+ Series 128GB Review

“As this system needs to be compatible with the TRIM command we have not installed the Intel INF drivers (which don’t support TRIM yet), and are instead using the default Windows 7 AHCI driver. TRIM is confirmed as running by using the command line and entering fsutil behavior query disabledeletenotify – a response of disabledeletenotify = 0 indicates TRIM is active”.

Microsoft AHCI vs Intel’s AHCI driver

“I was using Microsoft Standard AHCI1.0 drivers for the longest time since I read it was the only driver to support trim. Performance was great, according to ATTO I was exceeding the advertised speed, but I was experiencing a major annoyance in that if I put my computer into sleep mode when I woke it up all my secondary hard drives would be missing even if I refreshed the hardware in device manager. I would have to reboot to get my drives back rendering sleep mode pretty much useless to me. Now I read that the new Intel Rapid Storage driver supports trim (can a corsair guy confirm that?). So I installed that driver (if I click driver details I see iastor.sys version 9.6.0.1014 as my driver). The good news is I can use sleep mode again, my secondary drives are present when I wake it up and my read speed went from a max of 216000MB/sec to 239000MB/sec. The bad news is my write speeds took a hit going from a high of 189000 down to a high of 159000. 1. Has anyone else experienced the same sleep issue I was having with the Standard AHCI1.0 drivers? 2. Did your write speed also suffer when switching to Intel’s RST driver?”

Well that is pretty confusing. Who knows the best configuration for TRIM on a Kingston SSD? Also, the largest SSD – Kingston 512GB – drive size is great, but the price is outrageous. The confusion on TRIM and drivers makes me cautious. How about a few other practical comparisons?

Intel X25-M Mainstream SSDSA2MH160G2R5 2.5″ 160GB SATA II MLC Internal Solid State Drive (SSD)

Crucial CT256M225 2.5″ 256GB SATA II MLC Internal Solid State Drive (SSD)

Customer reviews say it all. Not sure what is wrong with Crucial M225 series. So the Kingston reviewer’s preference (see conclusion) of M225 is misplaced. However, the Crucial C300 seems like a good drive. After reviewing many performance reports, I’m going for the X25-M, random write IO, reasonable size and price.The bleeding edge is very high risk/cost – and I need my data, and optimal random write IO performance.

Existing SSD drives do not support TRIM with RAID, and I have a bunch of other issues with SSD drives. Next year – we will see what is new.

Enjoy.

m

As this system needs to be compatible with the TRIM command we have not installed the Intel INF drivers (which don’t support TRIM yet), and are instead using the default Windows 7 AHCI driver. TRIM is confirmed as running by using the command line and entering fsutil behavior query disabledeletenotify – a response of disabledeletenotify = 0 indicates TRIM is active.

More About…

Posted in Uncategorized | Leave a comment

Wireless adapter (WN111v2) on Windows 7 64bit – Install issues?

Months ago I switched to using the Windows 7 64bit OS. However, most of my old wireless adapters no longer worked because of lacking drivers. Some quality wireless adapters work fine because the manufacturer maintains updates for their products. The technology changes quickly, and I was looking for a new adapter with -N protocol support.

So I searched for a new wireless adapter with -N protocol support and Windows 7 64bit support. I got the Netgear WN111v2 and is has worked fine. However, upgrading my laptop now, I found problems.

The drivers I downloaded from Netgear’s support website failed completely. Every version I tried including old versions was a complete FAIL. First, the programs hung during install and the application software and drivers would not uninstall, nor rollback via Windows Restore. Second, somehow I got WG111v2 software installed instead of WN111v2. Probably my error. This software also refuses to uninstall and launching WG111v2.exe at boot generates “Internal Error: Without VIDPID registry key!!”. Your results may vary.

The only workaround I found was uninstalling everything with the *original* CDROM distributed with my WN111v2. Next, reinstalling with the *original* CDROM worked. I was frustrated when running *uninstall* and the program instead created (yet) another Windows 7 restore point and attempts to *install* again. This only resolved with the *original* CDROM inserted during uninstall. And, what language is the message “without VIDPID registry key”, because it does not read like English. What a Netgear software FAIL. in my opinion.

I manually removed all references to WG111v2 software from my registry and file system. Now my WN111v2.exe launches at startup and the program runs fine.

Others have struggled with this too: Netgear forum: without VIDPID registry key?

Good luck.

m

Posted in Uncategorized | Leave a comment

Iometer – Do you trust IO benchmarks on new PC SSDs?

Iometer is a wonderful tool for benchmarking input-output rates of existing hardware. But, how much can you trust IO benchmarks?

Some of the latest PC and laptop SSD (2.5 inch SATA II/III drive) vendors claim almost 300MB/sec sequential read & write access using tools like Iometer. Really? Did you know some SSD controller vendors implement compression? If the data pattern used in benchmark test is based on repeating bytes, and the controller compresses the data, then the results will be inflated. How can you tell if the quoted SSD performance claim is based on inflated results (in my opinion – cheating) or not?

The most accurate benchmark method is to measure against your actual data. Other methods found on the internet include testing with many different benchmarking tools, but this only confuses the issue. Changing your entire test for comparison is not scientific. Changing a single thing in your test for comparison is scientific. Testing with a list of different benchmark tools may or may *not* report inflated results resulting from controller compression. I prefer the option to directly measure IO results with and/or without compression.

A *new* benchmarking option is available with Iometer, e.g. “Write IO Data Pattern” as “Repeating bytes”, “Pseudo random”, or “Full random”. When you write a “Full random” data pattern to an SSD disk you defeat controller data compression. If you do not expect gains from controller compression in your actual data you can now run tests to prevent inflating IO results from controller data compression. The method is simple, and the steps are listed below. I assume familiarity with Iometer, so I do not otherwise describe how to use it.

Step 1: Create a new directory to host the Iometer files on your PC. Open a command window and change to the new directory.

Step 2: Fetch the development files from SourceForge using Subversion using the following command:

C:\iometer>svn co https://iometer.svn.sourceforge.net/svnroot/iometer iometer

Note: You can get VisualSVN here.

Step 3: Navigate to the working directory for the new “Write IO Data Pattern” feature, and run the program.

cd C:\iometer\binaries\2010q2rnd\i386
IOMETER.exe

Step 4: Select the “Full random” option, and do your Iometer testing as normal.

The development branch for writing *random* data (2010q2rnd) may change over time. I did not realize the tool was available to run without compiling it myself. If you find an Iometer bug using this new feature on the development branch please report it to the Iometer team. You can be a “Rock Star” and also patch any bugs and submit a fix!

The good news is we can all (non-devs) begin to test new PC SSD drives and controllers without seeing our Iometer results inflated by compression. Good luck! Enjoy.

m

PS: What are others saying about “Full random” tests with Iometer?

http://www.google.com/#hl=en&q=iometer+%22full+random%22

http://www.bing.com/search?q=iometer+%22full+random%22

http://www.overclockers.com/patriot-inferno-100gb-ssd-review/#recent

with the comment by Mr Alpha: “About the Anandtech use of IOMeter: they don’t use that old 2006 build of IOMeter that everybody else seems to use, but a newer 2008 build, which has, according to the change log, an change to how IOMeter writes data to avoid undesired side effects when benchmarking flash based media, which was submitted by a couple of guys at Intel.

I believe they went even further with SandForce testing and compiled their own version of IOMeter that lets you write randomized data, which matters since the write performance of the SandForce drives depend on data compression.”

Posted in Uncategorized | Leave a comment