Showing posts with label C#. Show all posts
Showing posts with label C#. Show all posts

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

April 15, 2009

Why will WCF client be disconnected after the connection idles for a long time?

The default receive timeout is 10 minutes, so WCF client will be disconnected after the idle time exceeded that limitation. What can I do if the connection needs to be kept alive?

Solution #1 - Server provides a dummy operation for client calls it regularly to let it not idle.

Solution #2 - Enable reliableSession and set receiveTimeout and inactivityTimeout to "infinite" in both the client and server.

The configuration snippet may like the following:

<system.serviceModel>
  <bindings>
    <wsHttpBinding>
      <binding name="WSHttpBinding" receiveTimeout="infinite">
        <reliableSession inactivityTimeout="infinite" enabled="true" />
      </binding>
    </wsHttpBinding>
  </bindings>
  <services>
  ...
  </services>
  ...
</system.serviceModel>

You can get more detail explanation in the following reference link.

Reference in MSDN:
Binding.ReceiveTimeout Property

Reference in Paulo Reichert's Blog
WCF Reliable Sessions Puzzle

July 17, 2008

How to use named system events (global events) in C#?

There are three classes relating to windows events, EventWaitHandle, AutoResetEvent, and ManualResetEvent.

EventWaitHandle
The EventWaitHandle class can represent either automatic or manual reset events and either local events or named system events.

AutoResetEvent
The AutoResetEvent class derives from EventWaitHandle and represents a local event that resets automatically.

ManualResetEvent
The ManualResetEvent class derives from EventWaitHandle and represents a local event that must be reset manually.

Tips:
According to above description, you should use EventWaitHanlde class to represent named system events, of course it can be either automatic or manual reset events.

MSDN reference:
http://msdn.microsoft.com/en-us/library/ksb7zs2x.aspx

January 19, 2008

Microsoft Win32 to Microsoft .NET Framework API Map

Microsoft Win32 to Microsoft .NET Framework API Map
http://msdn2.microsoft.com/en-us/library/aa302340.aspx

This is a very useful article for C# programmer.

January 10, 2008

Notes for HttpWebResponse class

You MUST call Close method to close the response and release the connection.

HttpWebRequest HttpWReq = (HttpWebRequest)WebRequest.Create("http://www.google.com");

HttpWebResponse HttpWResp = (HttpWebResponse)HttpWReq.GetResponse();
// Insert code that uses the response object.
HttpWResp.Close();

http://msdn2.microsoft.com/en-us/library/system.net.httpwebresponse.aspx

December 06, 2007

Read/Write INI Files in C#

A Complete Win32 INI File Utility Class
http://www.codeproject.com/KB/files/win32fileutilityclass.aspx

C# wrap for calling windows api. Easy to use!