Skip to content

Commit

Permalink
Started project that works specifically with resources.
Browse files Browse the repository at this point in the history
  • Loading branch information
lkinsella committed Jul 5, 2016
1 parent 75c1498 commit 8bc30b9
Show file tree
Hide file tree
Showing 17 changed files with 839 additions and 191 deletions.
6 changes: 6 additions & 0 deletions Src/Demo Application/Demo Application.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,9 @@
<ItemGroup>
<Reference Include="System" />
<Reference Include="System.Core" />
<Reference Include="System.Drawing" />
<Reference Include="System.Security" />
<Reference Include="System.Windows.Forms" />
<Reference Include="System.Xml.Linq" />
<Reference Include="System.Data.DataSetExtensions" />
<Reference Include="Microsoft.CSharp" />
Expand All @@ -50,6 +52,10 @@
<None Include="App.config" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\Workshell.PE.Resources\Workshell.PE.Resources.csproj">
<Project>{2b06cbf8-136a-4bec-8624-861992139489}</Project>
<Name>Workshell.PE.Resources</Name>
</ProjectReference>
<ProjectReference Include="..\Workshell.PE\Workshell.PE.csproj">
<Project>{2e173d25-1c2e-4a7b-8b37-d231324d372d}</Project>
<Name>Workshell.PE</Name>
Expand Down
22 changes: 14 additions & 8 deletions Src/Demo Application/Program.cs
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Reflection;
using System.Security.Cryptography.X509Certificates;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

using Workshell.PE;

Expand All @@ -24,9 +26,6 @@ static void Main(string[] args)
//string file_name = @"C:\Windows\System32\shell32.dll";
//string file_name = @"C:\Windows\SysWOW64\xpsservices.dll";
//string file_name = @"c:\windows\system32\advapi32.dll";
//string file_name = @"P:\Workshell\dotNET Dependency Walker\Bin\Debug\netdepends.exe";
//string file_name = @"C:\Users\Lloyd\Desktop\PE Related\Tools\PeInternals\x64\PeInternals.exe";
//string file_name = @"D:\Lloyd\Downloads\Win32DiskImager-0.9.5-install.exe";
string error_message;

if (!ExecutableImage.IsValid(file_name,out error_message))
Expand All @@ -37,11 +36,18 @@ static void Main(string[] args)
}

ExecutableImage image = ExecutableImage.FromFile(file_name);
Certificate cert = Certificate.Get(image);
X509Certificate x509 = cert.GetCertificate();

if (x509 != null)
X509Certificate2UI.DisplayCertificate((X509Certificate2)x509);
Resources resources = Resources.Get(image);
ResourceType cursor_groups = resources.FirstOrDefault(type => type.Id == ResourceType.RT_GROUP_CURSOR);
Resource group_resource = cursor_groups.FirstOrDefault(res => res.Id == 1001);
CursorGroupResource cursor_group = CursorGroupResource.FromResource(group_resource, Resource.DEFAULT_LANGUAGE);
CursorGroupResourceEntry cursor_entry = cursor_group.FirstOrDefault();
ResourceType cursors = resources.FirstOrDefault(type => type.Id == ResourceType.RT_CURSOR);
Resource resource = cursors.FirstOrDefault(res => res.Id == cursor_entry.CursorId);
CursorResource cursor_resource = CursorResource.FromResource(resource, Resource.DEFAULT_LANGUAGE);
Cursor cursor = cursor_resource.ToCursor();
Bitmap bitmap = cursor_resource.ToBitmap();

bitmap.Save(@"d:\test.bmp");

Console.ReadKey();
}
Expand Down
172 changes: 172 additions & 0 deletions Src/Workshell.PE.Resources/CursorGroupResource.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,172 @@
using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

using Workshell.PE.Extensions;
using Workshell.PE.Native;

namespace Workshell.PE
{

public sealed class CursorGroupResourceEntry
{

internal CursorGroupResourceEntry(CURSOR_RESDIR resDir)
{
Width = resDir.Cursor.Width;
Height = resDir.Cursor.Height;
Planes = resDir.Planes;
BitCount = resDir.BitCount;
BytesInRes = resDir.BytesInRes;
CursorId = resDir.CursorId;
}

#region Methods

public override string ToString()
{
return String.Format("{0}x{1} {2}-bit, ID: {3}", Width, Height, BitCount, CursorId);
}

#endregion

#region Properties

public ushort Width
{
get;
private set;
}

public ushort Height
{
get;
private set;
}

public ushort Planes
{
get;
private set;
}

public ushort BitCount
{
get;
private set;
}

public uint BytesInRes
{
get;
private set;
}

public ushort CursorId
{
get;
private set;
}

#endregion

}

public sealed class CursorGroupResource : IEnumerable<CursorGroupResourceEntry>
{

private CursorGroupResourceEntry[] entries;

internal CursorGroupResource(CursorGroupResourceEntry[] groupEntries)
{
entries = groupEntries;
}

#region Static Methods

public static CursorGroupResource FromBytes(byte[] data)
{
using (MemoryStream mem = new MemoryStream(data))
{
return FromStream(mem);
}
}

public static CursorGroupResource FromStream(Stream stream)
{
NEWHEADER header = Utils.Read<NEWHEADER>(stream);

if (header.ResType != 2)
throw new Exception("Not a cursor group resource.");

CursorGroupResourceEntry[] entries = new CursorGroupResourceEntry[header.ResCount];

for(var i = 0; i < header.ResCount; i++)
{
CURSOR_RESDIR cursor = Utils.Read<CURSOR_RESDIR>(stream);
CursorGroupResourceEntry entry = new CursorGroupResourceEntry(cursor);

entries[i] = entry;
}

CursorGroupResource group = new CursorGroupResource(entries);

return group;
}

public static CursorGroupResource FromResource(Resource resource)
{
return FromResource(resource, Resource.DEFAULT_LANGUAGE);
}

public static CursorGroupResource FromResource(Resource resource, uint language)
{
byte[] data = resource.ToBytes(language);

return FromBytes(data);
}

#endregion

#region Methods

public IEnumerator<CursorGroupResourceEntry> GetEnumerator()
{
for (var i = 0; i < entries.Length; i++)
yield return entries[i];
}

IEnumerator IEnumerable.GetEnumerator()
{
return GetEnumerator();
}

#endregion

#region Properties

public int Count
{
get
{
return entries.Length;
}
}

public CursorGroupResourceEntry this[int index]
{
get
{
return entries[index];
}
}

#endregion

}

}
Loading

0 comments on commit 8bc30b9

Please sign in to comment.