How to know if my Windows computer is compatible with Microsoft Wireless Display Adapter?

Everybody is excited about the Microsoft Band, but I am more excited about the Microsoft Wireless Display Adapter.

When the Wireless Display Adapter was announced, the first question I had was: “Will this adapter be compatible with my Windows PC?” and “Do I need compatible hardware?”

So, I went with my skip manager and he pointed me that this is easy to know: If you see the “Add a wireless display” link in the Devices > Project charm, then your device is Miracast enabled and therefore it is compatible with the Wireless Display Adapter.

Wireless Display Adapter Windows Devices Charm

Wireless Display Adapter Windows Project Charm

Wireless Display Adapter Widnows Add A Wireless Display

Wireless Display Adapter Windows AddDevices

Wireless Display Adapter Widnows Connecting To MicrosoftDisplayAdapter_78

Today, I received my Wireless Display Adapter! This is how cool it looks:

Wireless Display Adapter Windows Unboxing

Wireless Display Adapter Inside The Box

Wireless Display Adapter TV On HDMI Input

Wireless Display Adapter TV And Windows 8 Laptop

Wireless Display Adapter Connected To USB And HDMI TV Inputs

Of course, not every Windows computer supports Miracast, but because this technology is so awesome, OEMs should make it clear what devices support Miracast (aka Wireless Display)!

Wireless Display Adapter This PC Is Miracast Compatible

Cons?

The name! Microsoft Wireless Display Adapter is not an attractive name, better names could be “Windows Cast”, “Win Mirror”, etc. …

How to get the response content of a DownloadOperation or UploadOperation when the result is not 200 OK?

It is common that REST APIs return useful information for developers in the response content when the request is not successful.

For example, lets say you want to access the following URL from the Facebook API:

http://graph.facebook.com/v2.2/803370993032579

But you forget to set the authentication parameters in the request, so you make a request to the URL without setting the OAuth token. By inspecting DownloadOperation.Progress.Status, you see the response was 400 Bad Request. But that is not enough information to fix the problem!

The actual Facebook server response contains more information, look at the raw HTTP response:

Access-Control-Allow-Origin: *
Cache-Control: no-store
Connection: keep-alive
Content-Length: 146
Content-Type: application/json; charset=UTF-8
Date: Tue, 04 Nov 2014 03:25:14 GMT
Expires: Sat, 01 Jan 2000 00:00:00 GMT
Facebook-API-Version: v2.2
Pragma: no-cache
WWW-Authenticate: OAuth "Facebook Platform" "invalid_token" "An access token is required to request this resource."
X-FB-Debug: MUJRrIP4wQYZWjXwzGkjsvt8+QaLFJlfAX3w9CRaAXdcSQt5Zs6X8/bd1zWfQzMQBu60XbBF//LpRFR3i2Ejwg==
X-FB-Rev: 1479988

{
  "error": {
    "message": "An access token is required to request this resource.",
    "type": "OAuthException",
    "code": 104
  }
}

You can access the rest of the error information using DownloadOperation.GetResultStreamAt(). You have to get the IInputStream, create a DataReader, load the DataReader and finally read the response content as string.

Here is the example:

// using Windows.Networking.BackgroundTransfer
// using System.Diagnostics;

IStorageFile resultFile = await ApplicationData.Current.LocalFolder.CreateFileAsync(
    "blah.txt",
    CreationCollisionOption.ReplaceExisting);
BackgroundDownloader downloader = new BackgroundDownloader();
Uri uri = new Uri("http://graph.facebook.com/v2.2/803370993032579");
DownloadOperation download = downloader.CreateDownload(uri, resultFile);

try
{
    await download.StartAsync();
}
catch(Exception ex)
{
    Debug.WriteLine(ex);
}

Debug.WriteLine(download.Progress.Status); // 400 Bad Request

IInputStream inputStream = download.GetResultStreamAt(0);
DataReader reader = new DataReader(inputStream);
reader.InputStreamOptions = InputStreamOptions.Partial;

var bytesLoaded = await reader.LoadAsync(1000000);

// Useful error information:
Debug.WriteLine(reader.ReadString(reader.UnconsumedBufferLength));

How to send text, JSON or files using HttpClient.PostAsync().

This is a common question in StackOverflow and MSDN forums. So, lets take a look at our three main options.

HttpStringContent

Send any string like this:

// E.g. a JSON string.
HttpStringContent stringContent = new HttpStringContent(
    "{ \"firstName\": \"John\" }",
    UnicodeEncoding.Utf8,
    "application/json");

HttpClient client = new HttpClient();
HttpResponseMessage response = await client.PostAsync(
    uri,
    stringContent);

This sends a request like this:

POST / HTTP/1.1
Accept-Encoding: gzip, deflate
Content-Length: 23
Content-Type: application/json; charset=UTF-8
Host: kiewic.com
Connection: Keep-Alive
Cache-Control: no-cache

{ "firstName": "John" }

HttpFormUrlEncodedContent

Send a list of key-value pairs, better known as x-www-form-urlencoded:

Dictionary<string, string> pairs = new Dictionary<string,string>();
pairs.Add("Name", "Bob");
pairs.Add("Age", "18");
pairs.Add("Gender", "Male");
HttpFormUrlEncodedContent formContent =
    new HttpFormUrlEncodedContent(pairs);
 
HttpClient client = new HttpClient();
HttpResponseMessage response = await client.PostAsync(uri, formContent);

This sends a request like this:

POST / HTTP/1.1
Accept-Encoding: gzip, deflate
Content-Length: 27
Content-Type: application/x-www-form-urlencoded
Host: kiewic.com
Connection: Keep-Alive
Cache-Control: no-cache

Name=Bob&Age=18&Gender=Male

This is equivalent to submit the following HTML form from a web browser:

<html>
  <head>
    <meta charset="UTF-8" />
  </head>
  <body>
    <form action="http://kiewic.com" method="post">
      <input type="text" name="Name" value="Bob">
      <input type="text" name="Age" value="18">
      <input type="text" name="Gender" value="Male">
      <input type="submit" />
    </form>
  </body>
</html>

These values can be easily accessed from PHP using the $_POST array. Or from ASP.NET using Request.Form property.

HttpMultipartFormDataContent

Send files, or text and files mixed, better known as multipart/form-data.

First, create a sample file:

IStorageFolder folder = ApplicationData.Current.LocalFolder;
IStorageFile file = await folder.CreateFileAsync(
    "foo.txt",
    CreationCollisionOption.ReplaceExisting);
await FileIO.WriteTextAsync(
    file,
    "The quick brown fox jumps ...");

Then, send a request like this:

IInputStream inputStream = await file.OpenAsync(FileAccessMode.Read);
HttpMultipartFormDataContent multipartContent =
    new HttpMultipartFormDataContent();
multipartContent.Add(
    new HttpStreamContent(inputStream),
    "myFile",
    file.Name);
multipartContent.Add(
    new HttpStringContent("Hello World"),
    "myText");
 
HttpClient client = new HttpClient();
HttpResponseMessage response = await client.PostAsync(
    uri,
    multipartContent);

The raw request looks like this:

POST / HTTP/1.1
Accept-Encoding: gzip, deflate
Content-Length: 371
Content-Type: multipart/form-data; boundary=c9b47f5b-ca6c-43bd-a953-6ea78b2ee24b
Host: kiewic.com
Connection: Keep-Alive
Cache-Control: no-cache

--c9b47f5b-ca6c-43bd-a953-6ea78b2ee24b
Content-Disposition: form-data; name="myFile"; filename="foo.txt"; filename*=UTF-8''foo.txt

The quick brown fox jumps ...
--c9b47f5b-ca6c-43bd-a953-6ea78b2ee24b
Content-Length: 11
Content-Type: text/plain; charset=UTF-8
Content-Disposition: form-data; name="myText"

Hello World
--c9b47f5b-ca6c-43bd-a953-6ea78b2ee24b--

These values can be accessed from PHP using the $_FILES array. Or from ASP.NET using the Request.Files property.

 

When traveling to Mexico, what forms do I need to fill out?

As a foreigner, you should fill out two forms before passing immigration when arriving at a Mexican airport:

  1. Customs declaration.
  2. Entry registration form.

The customs declaration looks like this:

Mexico Customs Declaration

The entry registration looks like this:

Mexico Entry Registration

Mexico Entry Registration Back 2

Mexico Entry Registration Stamp

The entry registration (immigration form) can be split into two. You have to fill out both parts of the form before seeing the immigration agent when arriving to Mexico. The immigration agent will keep the top part, and it will put a stamp to the bottom part, which you must keep and give to the airline when departing from Mexico.

It would be better if you fill out the forms in the airplane. Ask for both to your air attendant :)

How to install the Universal Apps templates in Visual Studio 2013?

If you are having troubles finding the Universal Apps templates, you may need to download the Visual Studio 2013 Update 3.

Once installed, you will find the Universal Apps templates under Templates > Visual C# > Store Apps > Universal Apps in the New Project window.

Visual Studio New Project Universal Apps Screenshot

The Universal Apps templates allow you to create one solution for an app that runs on Windows 8.1 and Windows Phone 8.1. Learn more here!

 

 

WRL WeakRef

So, you have two objects that are related. A mother gorilla and a baby  gorilla. You want the mother gorilla can reference the baby gorilla, and vice versa, so you add a reference to the baby from the mother and another reference to the mother from the baby.

class MotherGorilla : public RuntimeClass<FtmBase>
{
private:
    ComPtr<BabyGorilla> baby;
};

class BabyGorilla : public RuntimeClass<FtmBase>
{
private:
    ComPtr<MotherGorilla> mother;
};

Guess what? In COM, where the objects are freed until the reference counter hits zero, the mom’s counter will never be zero because the baby has a reference to the mom, and the baby’s counter will never be zero because the mom holds a reference to the baby. Possible memory leak.

Fix? Make one of the references a weak reference.

class MotherGorilla : public RuntimeClass<FtmBase>
{
private:
    WeakRef baby;
};

// BabyGorilla class requires a UUID, otherwise WeakRef::CopyTo()
// fails with "error C2787: 'BabyGorilla' : no GUID has been
// associated with this object" [uuid("7c8ab438-a275-467f-8bdd-7e556e0016f4")] class BabyGorilla : public RuntimeClass<FtmBase> { private: ComPtr<MotherGorilla> mother; };

And each time the mother wants to interact with the baby, she will need to get a hard reference to the baby by calling WeakRef::As or WeakRef::CopyTo.