Category Archive for Microsoft

How To Use the Microsoft WebBrowser Control to Render HTML from Memory

Microsoft has a WebBrowser control that is actually an Internet Explorer control that you can use to display HTML in your own applications. More information about this WebBrowser control can be found on MSDN. By using this control it’s very easy to display online or offline webpages. However, it’s not immediately obvious how to make it display HTML that you might have in a memory buffer. Of course, one solution is to write the HTML to a temporary file and then load that file using the WebBrowser control, but obviously there is a better way for doing this which I will explain below.

The first thing you have to do is to add the WebBrowser control to your dialog. So, in Visual Studio, open the resource editor and then open the dialog onto which you want to put the WebBrowser control. Once the dialog is opened in the resource editor, right click on an empty space on the dialog and select “Insert ActiveX Control…”. This will open a new window in which you can select “Microsoft Web Browser” and then click OK. Visual Studio will automatically create a wrapper class for this ActiveX control which will probably be called explorer.h and explorer.cpp while the wrapper class will most likely be called CExplorer.

Now, right click the WebBrowser control on your dialog and select “Add Variable”. Make a variable with category set to “Control” and with the variable type set to the wrapper class “CExplorer” and hit OK.

Now we can start writing code. The first thing required is to load up some basic document; I use about:blank. Do this in your OnInitDialog handler as follows.

COleVariant loc(L"about:blank");
m_explorer.Navigate2(loc, NULL, NULL, NULL, NULL); 

The above is very important. If you don’t load an initial document, the WebBrowser control will not render any HTML that you try to push to it. This also means that before you can start writing HTML from memory in the WebBrowser control, you have to wait until the initial document has been fully loaded. This can be done with the “DocumentComplete” event. In the dialog editor, right the WebBrowser control and click on “Add Event Handler…”. Select “DocumentComplete” as message type, select the appropriate class and click “Add and Edit”. You can use that handler to change a boolean variable in your code to mark whether the document has been fully loaded. When it is fully loaded you can start writing HTML from memory to it.

Once that is finished, you can add the following helper function:

#include <MsHTML.h>
void CMyDlg::WriteHTML(const wchar_t* html)
{
	IDispatch* pHtmlDoc = m_explorer.get_Document();
	if (!pHtmlDoc)
		return;
	CComPtr<IHTMLDocument2> doc2;
	doc2.Attach((IHTMLDocument2*)pHtmlDoc);
	if (!doc2)
		return;
	 // Creates a new one-dimensional array
	SAFEARRAY* psaStrings = SafeArrayCreateVector(VT_VARIANT, 0, 1);
	if (!psaStrings)
		return;
	BSTR bstr = SysAllocString(html);
	if (bstr)
	{
		VARIANT* param;
		HRESULT hr = SafeArrayAccessData(psaStrings, (LPVOID*)&param);
		if (SUCCEEDED(hr))
		{
			param->vt = VT_BSTR;
			param->bstrVal = bstr;
			hr = SafeArrayUnaccessData(psaStrings);
			if (SUCCEEDED(hr))
			{
				doc2->write(psaStrings);
				doc2->close();
			}
		}
	}
	// SafeArrayDestroy calls SysFreeString for each BSTR!
	if (psaStrings)
		SafeArrayDestroy(psaStrings);
}

With the above function, it’s very easy to dynamically create and display HTML from memory. For example:

WriteHTML(L"<html><body><h1>My Header</h1><p>Some text below the header</p></body></html>");

Note that the above code is expecting a Unicode build. If you don’t use Unicode, you need to change the wchar_t types and you need to change the way how you allocate the BSTR variable.

That’s it. Pretty easy if you know how to do it, but it took me some time to figure it out.

[ Update: Fixed some typos and added mshtml.h reference. ]

[ Update 2: Added a call to “doc2->close();” after “doc2->write()” and added code to check the result of the doc2.Attach() call. ]

Share

Windows 2000, XP SP2 and Vista End of Life Support

This is a message to everyone who is running an older version of Windows. There are a few End of Life Support dates coming up, so it’s important to keep that in mind to avoid running unsupported versions of Windows.

Windows 2000 Professional and Windows 2000 Server are approaching 10 years since their launch and both products will go out of support on July 13, 2010.

Windows XP was launched back in 2001. While support for the product will continue, Service Pack 2 will go out of support on July 13, 2010. From that date onwards, Microsoft will no longer support or provide free security updates for Windows XP SP2.  Please install the free Service Pack 3 for Windows XP to have the most secure and supported Windows XP platform.

Finally, Windows Vista with no Service Packs installed will end support on April 13 2010.  Please install the free Service Pack 2 for Windows Vista to have the most secure and supported Windows Vista platform.

Share

Breaking Change for RValue References in Visual Studio 2010 RC

The Release Candidate of Visual Studio 2010 has changed the behaviour of RValue references slightly compared to the implementation in the Visual Studio 2010 beta versions. This is because the C++0x standard commitee has changed the RValue reference feature a bit and Visual Studio 2010 RC has incorporated those standard changes. Unfortunately, this might lead to compiler errors when you try to build code that is following the old standard. Let me give an example. Previously using a beta version of Visual Studio 2010 that was using the old C++0x standard, the following code would compile without any problems.

#include <iostream>
using namespace std;
// Increment value by 1 using RValue reference parameter.
int increment(int&& value)
{
   cout << "value = " << value << endl;
   value++;
   return value;
}
int main()
{
   int a = 10;
   int b = 20;
   // Increment a
   int result = increment(a);
   cout << "  a=" << a << ", b=" << b << ", result=" << result << endl;
   // Increment b
   result = increment(b);
   cout << "  a=" << a << ", b=" << b << ", result=" << result << endl;
   // Increment an expression
   result = increment(a + b);
   cout << "  a=" << a << ", b=" << b << ", result=" << result << endl;
   // Increment a literal
   result = increment(3);
   cout << "  a=" << a << ", b=" << b << ", result=" << result << endl;
   return 0;
} 

However, when trying to compile this using the latest release candidate of Visual Studio 2010, you will get the following errors:

rvalue_test.cpp(18): error C2664: 'increment' : cannot convert parameter 1 from 'int' to 'int &&'
          You cannot bind an lvalue to an rvalue reference
rvalue_test.cpp(21): error C2664: 'increment' : cannot convert parameter 1 from 'int' to 'int &&'
          You cannot bind an lvalue to an rvalue reference

These are related to the lines that are trying to increment a and b. Incrementing an expression or a literal still works as before. To get rid of those errors, you need to convert the lvalue to an rvalue. You can use the std::move function for this as shown in red below.

#include <iostream>
using namespace std;
// Increment value by 1 using RValue reference parameter.
int increment(int&& value)
{
   cout << "value = " << value << endl;
   value++;
   return value;
}
int main()
{
   int a = 10;
   int b = 20;
   // Increment a
   int result = increment(std::move(a));
   cout << "  a=" << a << ", b=" << b << ", result=" << result << endl;
   // Increment b
   result = increment(std::move(b));
   cout << "  a=" << a << ", b=" << b << ", result=" << result << endl;
   // Increment an expression
   result = increment(a + b);
   cout << "  a=" << a << ", b=" << b << ", result=" << result << endl;
   // Increment a literal
   result = increment(3);
   cout << "  a=" << a << ", b=" << b << ", result=" << result << endl;
   return 0;
}

This now compiles without any errors and produces the following output:

value = 10
  a=11, b=20, result=11
value = 20
  a=11, b=21, result=21
value = 32
  a=11, b=21, result=33
value = 3
  a=11, b=21, result=4

Now, it again works as expected 🙂

Share

Visual Studio 2010 Licensing White Paper

Microsoft has released a white paper for Visual Studio 2010 licensing which provides an overview of the complete Visual Studio 2010 product line. The paper also gives a number of example deployment scenarios and the licensing requirements for those.

Client editions in the Visual Studio 2010 product line include:

  • Microsoft Visual Studio 2010 Ultimate with MSDN
  • Microsoft Visual Studio 2010 Premium with MSDN
  • Microsoft Visual Studio 2010 Professional with MSDN
  • Microsoft Visual Studio Test Professional 2010 with MSDN

(Visual Studio 2010 products can be purchased without an MSDN subscription in certain channels.)

Server products in the Visual Studio 2010 product line include:

  • Microsoft Visual Studio Team Foundation Server 2010
  • Microsoft Visual Studio Lab Management 2010

Volume licensing customers who need a definitive guide to licensing terms and conditions should reference the Microsoft Licensing Product Use Rights (PUR) and applicable licensing agreements. For retail customers, the license terms are specified in the End User Licensing Agreement (EULA) included with the product.

Share

Visual Studio 2010 and .NET Framework 4 Release Candidate

Microsoft has released the Release Candidate version of Visual Studio 2010 and the .NET Framework 4.

See Scott Guthrie blog post about it.

Right now it’s available for MSDN subscribers.
On Wednesday 10th of February everyone will be able to get their hands on it 🙂

Two important things to know (from Scott Guthrie blog post):

  • If you have previously installed VS 2010 Beta 2 on your computer you should use Add/Remove Programs (within Windows Control Panel) to remove VS 2010 Beta2 and .NET 4 Beta2 before installing the VS 2010 RC.  Note that VS 2010 RC can be installed on the same machine side-by-side with VS 2008 and VS 2005.
  • Silverlight 3 projects are supported with today’s VS 2010 RC build – however Silverlight 4 projects are not yet supported.  We will be adding VS 2010 RC support for SL4 with the next public Silverlight 4 drop. If you are doing active Silverlight 4 development today we recommend staying with the VS10 Beta 2 build for now.
Share

Microsoft Office 2010 Beta

Microsoft has just released a beta version of Office 2010.

The following programs are available in this beta version:

  • Word
  • PowerPoint
  • Outlook
  • Excel
  • OneNote
  • Access
  • Publisher
  • InfoPath
  • SharePoint Workspace
  • Communicator

Separately available beta programs are:

  • Microsoft Visio 2010
  • Microsoft Project 2010

Get all the details.

Share

Windows 7 breaks all pre-order records at Amazon

Apparently the pre-order of Windows 7 went extremely well. According to Amazon UK, they sold more Windows 7 pre-orders in the first 8 hours of its release than Vista did during it’s entire pre-order period.

According to Brian McBride, Amazon UK MD:

“The launch of Windows 7 has superseded everyone’s expectations, storming ahead of Harry Potter and the Deathly Hallows as the biggest grossing pre-order product of all-time at Amazon.co.uk, and demand is still going strong. Over the past three months, only Dan Brown’s The Lost Symbol has sold more copies than Windows 7, which is an incredible achievement for a software product.”.

Tomorrow is the official launch date of Windows 7 🙂

Share

Visual Studio 2010 and .NET Framework 4 Beta 2

Visual Studio 2010 and .NET Framework 4 Beta 2 are now available. The final version is scheduled for 22nd of March 2010. I’m looking forward to it 🙂

For Visual C++ developers there are lots of new things to look forward to, like parallel programming, MFC ribbon resource editor, easy application local deployment model etc etc…

When you use the .NET Framework you will apparently be able to have deployments with up to 81% reduction in the framework size by using the Client Profile.

According to the press release:

“The company also outlined a simplified product lineup and pricing options for Visual Studio 2010 as well as new benefits for MSDN subscribers, including the Ultimate Offer, available to all active MSDN Premium subscribers at the official product launch on March 22, 2010.”

The product lineup is simplified with the following versions:

  • Microsoft Visual Studio 2010 Ultimate with MSDN. The comprehensive suite of application life-cycle management tools for software teams to help ensure quality results from design to deployment
  • Microsoft Visual Studio 2010 Premium with MSDN. A complete toolset to help developers deliver scalable, high-quality applications
  • Microsoft Visual Studio 2010 Professional with MSDN. The essential tool for basic development tasks to assist developers in implementing their ideas easily

Download Beta 2 now.

Read the full Microsoft press release.

Share

Access Hidden Regional Themes in Windows 7

I just came across an interesting blog post.

Apparently, Windows 7 comes with several regional themes which include wallpapers from Canada, Australia, South Africa, and Great Britain. You can easily activate those themes following the procedure in the following blog post.

Access Hidden Regional Themes in Windows 7

Share

Windows API Code Pack for Microsoft .NET Framework

I just stumbled upon the Windows API Code Pack for the Microsoft .NET Framework. It’s a code pack that allows .NET developers to take advantage of some of the new Windows 7 features.

Features supported by version 0.9 of the code pack are:

  • Windows 7 Taskbar Jump Lists, Icon Overlay, Progress Bar, Tabbed Thumbnails, and Thumbnail Toolbars.
  • Known Folders, Windows 7 Libraries, non-file system containers, and a hierarchy of Shell Namespace entities.
  • Windows 7 Explorer Browser Control.
  • Shell property system.
  • Windows Vista and Windows 7 Common File Dialogs, including custom controls.
  • Windows Vista and Windows 7 Task Dialogs.
  • Direct3D 11.0, Direct3D 10.1/10.0, DXGI 1.0/1.1, Direct2D 1.0, DirectWrite, Windows Imaging Component (WIC) APIs. (DirectWrite and WIC have partial support)
  • Sensor Platform APIs
  • Extended Linguistic Services APIs
  • Power Management APIs
  • Application Restart and Recovery APIs
  • Network List Manager APIs
  • Command Link control and System defined Shell icons.

Requirements:

  • .NET Framework 3.5 or later.
  • This library targets the Windows 7 RC version, though some of the features will work on the older versions of Windows operating system.
  • DirectX features have dependency on Windows SDK for Windows 7 RC and March 2009 release of DirectX SDK.

There are also a few short 2-minute videos available to show you how easy it is to use some of the above features.

Share

KulenDayz Presentation “What’s new in Visual C++ 2010?”

On saturday 13th of June I gave a presentation on KulenDayz titled “What’s new in Visual C++ 2010?”. The presentation consisted of quite a few demos. This post contains links to the source code for all demos including articles giving some more details.

Share

Parallel Pattern Library (PPL) in Visual C++ 2010

Visual C++ 2010 comes with a brand new library called the Parallel Pattern Library or PPL. It is a powerful library that makes writing parallel code easier which is getting more and more important with the current and upcoming multicore CPUs. This article will give an overview of the PPL. Read the rest of this entry »

Share

MFC Restart Manager Support in Visual C++ 2010

Windows Vista introduced the restart manager. It is used to automatically restart an application after it crashes. It can also be used to restart application after a reboot by a Windows Installer or update. If you create a new MFC application using the project wizard in Visual C++ 2010, you will automatically get support for the restart manager. If you want to add support to an existing MFC application, you only need to add 1 line of code to the constructor of your CWinApp or CWinAppEx derived class. Read the rest of this entry »

Share

CTaskDialog in MFC in Visual C++ 2010

Windows Vista introduced the concept of Task Dialogs. Those are a powerful replacement for the standard message boxes. Read the rest of this entry »

Share

Kulendayz Microsoft Community Event 2009

From 12th till 14th of June, the Microsoft Community Osijek (Croatia) is organizing the Kulendayz event. I will be giving a lecture titled “What is new in Visual C++ 2010?”. If you want to participate in the event, please register on the Kulendayz website.

Share

SafeInt in Visual C++ 2010

The SafeInt library is a new addition to Visual C++ 2010. It allows you to safely perform arithmetic operations on integers ranging from 8-bit to 64-bit. The SafeInt library will automatically detect arithmetic overflow or divide by zero. Using the SafeInt library is pretty easy. The following piece of code uses the SafeInt library to safely calculate the addition of two 8-bit integers. Read the rest of this entry »

Share

‘auto’ Keyword in Visual C++ 2010

Starting with Visual C++ 2010, the ‘auto’ keyword has a different meaning. Auto is now used as a variable type and it instructs the compiler to figure out the exact type itself. This makes it much easier to define function pointers or to iterate over vectors for example. This post will give a brief overview of how to use the ‘auto’ keyword. Read the rest of this entry »

Share

The ‘Move Constructor’ in Visual C++ 2010

A new feature in Visual C++ 2010 is called Rvalue References. This is a feature from the C++0x standard. One thing that Rvalue References can be used for is to implement move semantics for objects. To add move semantics to a class, we need to implement a move constructor and a move assignment operator (optional). This article will briefly explain the benefits of move constructors and how to write them. Read the rest of this entry »

Share

Visual C++ 2010 Beta 1 and the Windows 7 RC SDK

The Visual C++ 2010 Beta 1 release contains the Windows 7 Beta SDK. For Direct2D and DirectWrite there were some breaking changes between the beta version of the SDK and the RC version of the SDK. So if you want to use those new Direct2D and DirectWrite APIs, you definitely need the latest Windows 7 RC SDK. There are some manual steps involved in getting that to work with Visual C++ 2010. For detailed explanation please check out Using the Windows 7 RC SDK in Visual C++ 2010 Beta 1 on the Visual C++ Team Blog.

Share

Visual Studio 2010 and .NET FX 4 Beta 1 Released :)

Visual Studio 2010 Beta 1 and .NET FX 4 Beta 1 have been released and is available for download if you have an MSDN subscription. The beta 1 will be publicly released very soon. Check out http://www.microsoft.com/visualstudio/en-us/products/2010/default.mspx for more information.

Share