$theTitle=wp_title(" - ", false); if($theTitle != "") { ?>
About Virtualization, VDI, SBC, Application Compatibility and anything else I feel like
18 Oct // php the_time('Y') ?>
Sometimes 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:
1 2 3 4 5 6 7 | NTSYSAPI NTSTATUS NtQueryKey( [in] HANDLE KeyHandle, [in] KEY_INFORMATION_CLASS KeyInformationClass, [out, optional] PVOID KeyInformation, [in] ULONG Length, [out] PULONG ResultLength ); |
1 2 | [DllImport("ntdll.dll", EntryPoint="NtQueryKey", SetLastError=false)] private static extern UInt32 NtQueryKey(IntPtr KeyHandle, KEY_INFORMATION_CLASS KeyInformationClass, IntPtr KeyInformation, UInt32 Length, ref UInt32 ResultLength); |
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:
1 2 3 4 | UInt32 nts; UInt32 resLen = 0; IntPtr buffer = IntPtr.Zero; nts = NtQueryKey(KeyHandle, KEY_INFORMATION_CLASS.KeyNameInformation, IntPtr.Zero, 0, ref resLen); |
13 Mar // php the_time('Y') ?>
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?
13 Oct // php the_time('Y') ?>
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:
1 2 3 4 | function Swap32(value: Integer): Integer; asm bswap eax end; |
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:
1 2 3 4 | IPAddress ipa = IPAddress.Parse("10.4.2.91"); uint ip = (uint)IPAddress.NetworkToHostOrder( (int)System.BitConverter.ToUInt32( ipa.GetAddressBytes(), 0)); |
12 Oct // php the_time('Y') ?>
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:
1 2 3 4 | using System.Net; IPAddress ipa = IPAddress.Parse("10.4.2.91"); uint ip = (uint)ipa.Address; |
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:
1 2 3 4 5 6 7 | using System.Net; IPAddress ipa = IPAddress.Parse("10.4.2.91"); #pragma warning disable 612, 618 uint ip = (uint)ipa.Address; #pragma warning restore 612, 618 |
But it would be better to use the non deprecated GetAddressBytes() Method:
1 2 3 4 5 6 | using System.Net; IPAddress ipa = IPAddress.Parse("10.4.2.91"); // for IPv4 we can convert to UInt32... uint ip = System.BitConverter.ToUInt32(ipa.GetAddressBytes(), 0); |
22 Aug // php the_time('Y') ?>
I 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:
1 2 3 4 5 6 7 8 9 10 11 12 | namespace TheExe.Core { internal static class AssemblyInfo internal static class StringExtensionMethods } internal static class StringExtensionMethods { // Methods public static string Hash(this string original, string password); // More methods... } |
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:
29 Jan // php the_time('Y') ?>
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:
1 | int preFrom = value.IndexOf(prePrefix, System.StringComparison.CurrentCultureIgnoreCase); |