October 21, 2009

DLL (Dynamic-Link Library) Search Order

Are you sure that your program loads the DLLs expected? You may want to check the search order for loading the proper DLLs.

Standard Search Order (SafeDllSearchMode is enabled)
1. The directory from which the application loaded.
2. The system directory. Use the GetSystemDirectory function to get the path of this directory.
3. The 16-bit system directory. There is no function that obtains the path of this directory, but it is searched.
4. The Windows directory. Use the GetWindowsDirectory function to get the path of this directory.
5. The current directory.
6. The directories that are listed in the PATH environment variable. Note that this does not include the per-application path specified by the App Paths registry key. The App Paths key is not used when computing the DLL search path.

More Detail Information
Dynamic-Link Library Search Order

September 12, 2009

Mp4 Explorer

Brief Introdution
This software is a very helpful tool to inspect and see in details a mp4 file and it is open source. In my case, I was trying to write a program to convert certain video to be compatible with iPhone, but I had no idea about it. Then, I found this useful utility and used it to inspect the video structure of some compatible mp4 files.

Mp4 Explorer

You can download the program and its source code in the following web site:

Official Website - Mp4 Explorer
http://mp4explorer.codeplex.com/

August 26, 2009

How To: Get the Content of Certain Environment Variable, ie: %ProgramFiles%

Code Snippet

#include <windows.h>
#include <string>

using namespace std;

wstring GetContentFromEnvironmentVariable(const wstring& wstrVariable)
{
  // Get the content of %ProgramFiles%

  wstring wstrRtn;
  DWORD dwSize = ::GetEnvironmentVariableW(wstrVariable.c_str(), NULL, NULL);
  if(dwSize > 0)
  {
    wchar_t* wszContent = new wchar_t [dwSize];
    dwSize = ::GetEnvironmentVariableW(wstrVariable.c_str(), wszContent, dwSize);

    wstrRtn = wszContent;

    delete [] wszContent;
    wszContent = NULL;
 }

 return wstrRtn;
}

Reference on MSDN
GetEnvironmentVariable Function

July 30, 2009

How To: Get the Installation Directory of Windows Media Player

Code Snippet

bool GetWMPInstallDirectory(wstring& wstrDirectory)
{
  bool bOK = false;
  HKEY hkResult = NULL;

  LONG lRet = ::RegOpenKeyExW(HKEY_LOCAL_MACHINE,
    L"SOFTWARE\\Microsoft\\MediaPlayer", NULL, NULL, &hkResult);

  if(lRet == ERROR_SUCCESS)
  {
    wchar_t wszRet[_MAX_PATH] = {0};
    DWORD type = REG_SZ;
    DWORD cbData = sizeof(wszRet);

    lRet = ::RegQueryValueExW(hkResult, L"Installation Directory",
      NULL, &type, (LPBYTE)wszRet, &cbData);

    if(lRet == ERROR_SUCCESS)
    {
      // We've got the installation directory of WMP
      wstrDirectory = wszRet;
      bOK = true;
    }

    ::RegCloseKey(hkResult);
    hkResult = NULL;
  }

  return bOK;
}

June 12, 2009

How To: Disable General Protection Fault Dialog for Your Program

Abstraction
Are you using any 3rd-party componet or ActiveX, which is not stable enough, in your program? You will get a GPF dialog if the componet in your program goes abnormal. That is not your problem totally, but actually your program crashed. There's a Windows API to prevent showing GPF dialog, you may adapt this solution if the crash can be ignored.

Code Snippet

UINT uPrevErrMode = SetErrorMode(SEM_NOGPFAULTERRORBOX);

Do you not know what GPF is?

Windows Calendar Crash

Reference on MSDN

June 06, 2009

Read/Write Windows Registry on Windows 64-Bits Platforms (C# Version)

Abstraction
How does a 32-bits(x64) program running on Windows 64-bits(x64) access the 64-bits registry? In C#, you can use P/Invoke to call RegOpenKeyEx function with KEY_WOW64_KEY to access the 64-bits registry.

Code Snippet

//
// This is a C# example to get the version of Windows Media Center on Windows 7 64-bits.
//

class RegQueryValueDemo
{
  [DllImport("advapi32.dll", CharSet = CharSet.Unicode, EntryPoint = "RegOpenKeyExW", SetLastError = true)]
  public static extern int RegOpenKeyEx(UIntPtr hKey, string subKey, uint options, int sam, out UIntPtr phkResult);
  public static UIntPtr HKEY_CURRENT_USER = (UIntPtr)0x80000001;
  public static UIntPtr HKEY_LOCAL_MACHINE = (UIntPtr)0x80000002;
  public static int KEY_QUERY_VALUE = 0x0001;
  public static int KEY_SET_VALUE = 0x0002;
  public static int KEY_CREATE_SUB_KEY = 0x0004;
  public static int KEY_ENUMERATE_SUB_KEYS = 0x0008;
  public static int KEY_WOW64_64KEY = 0x0100;
  public static int KEY_WOW64_32KEY = 0x0200;

  [DllImport("advapi32.dll", CharSet = CharSet.Unicode, EntryPoint = "RegQueryValueExW", SetLastError = true)]
  public static extern int RegQueryValueEx(UIntPtr hKey, string lpValueName, int lpReserved, out uint lpType,
    StringBuilder lpData, ref int lpcbData);

  public string GetMCEIdent()
  {
    UIntPtr regKeyHandle;
    if (RegOpenKeyEx(
    HKEY_LOCAL_MACHINE,
    "SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Media Center",
    0, KEY_QUERY_VALUE | KEY_WOW64_64KEY, out regKeyHandle) == 0)
    {
      uint type;

      StringBuilder stringBuilder = new StringBuilder(2048);
      int cbData = stringBuilder.Capacity;
      if (RegQueryValueEx(regKeyHandle, "Ident", 0, out type, stringBuilder, ref cbData) == 0)
      {
        return stringBuilder.ToString();
      }
    }

    return string.Empty;
  }
}


Find a C++ version?
Read/Write Windows Registry on Windows 64-Bits Platforms

Reference on MSDN:
RegOpenKeyEx Function
32-bit and 64-bit Application Data in the Registry

May 24, 2009

How To: A Web Page Interacts with a Embedded Web Browser Control

If your application contains a web browser control to show certain web page, can the web page interact with your application by script language? Yes, it can.

There's a sample on MSDN, it's very short but clear, please refer to the following link:

Reference on MSDN