Remko Weijnen's Blog (Remko's Blog)

About Virtualization, VDI, SBC, Application Compatibility and anything else I feel like

Archive for the ‘C#’ Category

Get registry key name from a handle

RegeditSometimes it can be useful to determine what registry key belongs to a registry key handle. And exampling is when hooking RegQueryValue so you can determine the registry key that a value belongs to instead of having to track the registry key when it’s opened.

To obtain the registry key we can pass a handle to the NtQueryKey API with which has the following signature:

To get the keyname we need to pass the KeyNameInformation enum and we can set the ReturnLength parameter to 0 to obtain the required buffer size:

(more…)

  • 0 Comments
  • Filed under: C#, Uncategorized
  • Returning a string from unmanaged dll to .net

    I write most of my code in unmanaged languages such as Delphi and C/C++. Sometimes customers ask me to interface my code to their .net code in which case I create a dll for them.

    A recurring thing is that I need to return string to .net.

    There are many ways to do this of course but in all cases we need to manage memory: who will allocate the memory for the string and who is responsible for freeing it?

    (more…)

  • 3 Comments
  • Filed under: .NET, C#, C++, Delphi
  • Yesterday I wrote about converting an IP Address to an Integer in C#. But both methods I presented return the IP Address in network byte order.

    However in some cases, especially when calling WinApi functions, you will need to convert the Integer to host byte order which is little-endian on Intel processors.

    In an unmanaged language we could do very fast byte swap with inline assembly, eg:

    From WinApi we could use the ntohl function and in managed languages we can use the NetworkToHostOrder method from the System.Net.IPAddress class.

    For an IPv4 address we need to make sure we are using the proper overload by casting the result of System.BitConverter to an int:

     

  • 0 Comments
  • Filed under: C#
  • For a call to a WinApi function I needed to convert an IP Address to an Integer in C#.

    This can be done using the System.Net.IPAddress class:

    Although this works, the compiler issues a warning: 

    warning CS0618: ‘System.Net.IPAddress.Address’ is obsolete: ‘This property has been deprecated. It is address family dependent. Please use IPAddress.Equals method to perform comparisons. http://go.microsoft.com/fwlink/?linkid=14202′

    This warning is issued because the Address property is not IPv6 compatible. The warning can be suppressed like this:

    But it would be better to use the non deprecated GetAddressBytes() Method:

  • 1 Comment
  • Filed under: C#
  • Red Gate .NET ReflectorI wanted to call a hash function from a .net executable from my code. My first step was to inspect the executable with Reflector.

    The Hash function was in a namespace called Core:

    Notice that the Core namespace is marked as internal so it was not meant to be callable outside of the executable. It’s still possible to call it using Reflection:

    (more…)

  • 0 Comments
  • Filed under: C#
  • Making String.IndexOf case insensitive

    I don’t do much programming in .NET based languages but I have to for some things like the Windows Live Writer plugin I am creating.

    I didn’t expect this but the String.IndexOf Method is by default case sensitive.

    But we can make it case insensitive if we use one of the overloads: IndexOf(String, StringComparison).

    Example:

  • 0 Comments
  • Filed under: .NET, C#
  • Blogroll


    Categories


    Archives