GetLastError() in std::string for windows phone

Windows dev always familiar with most Favorited GetLastError() function, and it has common wrapper to get error message in std::string. However most wrapper include FORMAT_MESSAGE_ALLOCATE_BUFFER which is not supported in windows phone or winrt application.

So you can use this function :

// Create a string with last error message
std::string GetLastErrorStdStr()
{
 DWORD error = GetLastError();
 if (error)
 {
 LPVOID lpMsgBuf;
 DWORD bufLen = FormatMessage(
 FORMAT_MESSAGE_FROM_SYSTEM |
 FORMAT_MESSAGE_IGNORE_INSERTS,
 NULL,
 error,
 MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
 (LPTSTR) &lpMsgBuf,
 0, NULL );
 if (bufLen)
 {
 LPCSTR lpMsgStr = (LPCSTR)lpMsgBuf;
 std::string result(lpMsgStr, lpMsgStr+bufLen);
 
 return result;
 }
 }
 return std::string();
}

Notice, without

FORMAT_MESSAGE_ALLOCATE_BUFFER
and
localfree to lpBuffer

Create crash dump file in Windows Phone

Your app got crash ?

This is very handy snippet for you :

this.UnhandledException+=App_UnhandledException;
private void Application_UnhandledException(object sender, ApplicationUnhandledExceptionEventArgs e)
{
 if (System.Diagnostics.Debugger.IsAttached)
{
// An unhandled exception has occurred; break into the debugger
 System.Diagnostics.Debugger.Break();
}
else
{
 System.IO.IsolatedStorage.IsolatedStorageFile f = System.IO.IsolatedStorage.IsolatedStorageFile.GetUserStoreForApplication();
 using(StreamWriter w = new StreamWriter(f.OpenFile("crashLog.txt",FileMode.Create)))
  {
   w.Write(e.ToString());
  }
 }
}

Enable Direct3D Debug Layer in Windows Phone

You can create Direct3D windows phone app using XAML+Direct3D template, but it has some cons. The most annoying cons is inability to enable Direct3D Debug Layer as the runtime create Direct3D device internally.

Actually with a little trick we can enable the debug layer using visual studio debugger.

  1. Create new breakpoint at d3d11!D3D11CreateDevice
  2. Start your program
  3. After the breakpoint triggered, change the R3 register value to (0x02 or 00000002 )

Now you can view some direct3D messages in output window, you can find some explanation about Direct3D debug layer’s messages here and here.

Get Graphic device information in DirectX

This time I work on windows phone 8 application, so i need to learn DirectX. Recently I had to get device information for device profile.

IDXGIDevice *m_DXGIDevice;
 IDXGIAdapter *m_DXGIAdapter;
 device->QueryInterface<IDXGIDevice>(&m_DXGIDevice);
 m_DXGIDevice->GetAdapter(&m_DXGIAdapter);
 m_DXGIAdapter->GetDesc(&m_adapterInfo);
 wcscpy(m_deviceModel,m_adapterInfo.Description);

Unfortunately this code work on windows desktop but not on windows phone.

 

Update 1 :

As you all know that windows phone 8 DirectX Feature level is 9.3, code above only supported on directX 10 and above. If you run this code in DirectX Feature level 9.3 (dekstop or phone) you will get “Software Driver” in m_adapterInfo.Description value.

Update 2 :

Windows Phone 8.1 brings DirectX 11.2, so you can use this snippet in this windows phone version.