Accessing CommandBarButton.Picture property the late bound way - c#

I try to read the information of Microsoft Access (Office) CommandBars contained in an *.mdb. I could use Microsoft.Office.Interop.Access for this; however, these PIA assemblies are tied to specific Office versions. Therefore, to be version independent, I do it the late-bound way through C#'s dynamic type. I.e., I have no references to Microsoft Office specific assemblies. The price for this is that the access to the command bars is now weakly typed.
This approach works well, except when I try to access the custom button images. This is a condensed version of my real code to illustrate the problem:
dynamic access = Activator.CreateInstance(Type.GetTypeFromProgID("Access.Application", true));
// Starts an Access XP (2002) process in my case, but could be any version.
foreach (dynamic commandBar in access.CommandBars) {
if (!commandBar.BuiltIn) { // Only my menus and toolbars.
foreach (dynamic control in commandBar.Controls) {
if (control.Type == (int)MsoControlType.msoControlButton && !control.BuiltInFace) {
string caption = control.Caption; // Works.
stdole.IPictureDisp picture = control.Picture; // <==== Throws exception! ====
// ...
}
}
}
}
Calling the getter of the CommandBarButton.Picture property throws this exception:
Catastrophic failure (Exception from HRESULT: 0x8000FFFF (E_UNEXPECTED))
at System.Runtime.InteropServices.Marshal.ThrowExceptionForHRInternal(Int32 errorCode, IntPtr errorInfo)
at System.Dynamic.ComRuntimeHelpers.CheckThrowException(Int32 hresult, ExcepInfo& excepInfo, UInt32 argErr, String message)
at System.Dynamic.UpdateDelegates.UpdateAndExecute1[T0,TRet](CallSite site, T0 arg0)
at System.Dynamic.UpdateDelegates.UpdateAndExecute1[T0,TRet](CallSite site, T0 arg0)
at MyApplication.MyMethod in MyApplication.cs:line 64
How can I get the picture avoiding this exception?
Note, this question is not about converting the IPictureDisp object to a System.Drawing.Image object. I already have a solution for this. Also, it makes no difference whether picture is typed as object, dynamic, or IPictureDisp. The property does exist, otherwise I would get the exception 'System.__ComObject' does not contain a definition for 'Picture'.
It is a .NET Framework 4.0 Windows Forms project compiled for x86 (32-bit).
Edit: Switching to Primary Interop Assemblies (PIA) allows strong typing, but does not solve the problem. The exception persists.
Meanwhile I have read more about PIAs. Since .NET Framework 4.0 you can tweak the properties of the assembly reference to make them version independent. Right click on References / Microsoft.Office.Interop.Access and
Set Embed Interop Types to True.
Set Specific Version to False.
Do this for References / office as well. Therefore I will be switching to PIAs instead of using dynamic.

Related

C# System.TypeInitializationException When Generic Types used

I am getting a System.TypeInitializationException in C# when i try to call the following:
List<BuyShopItem> buyShopItemList = new List<BuyShopItem>(0);
BuyShopItem is in an external assembly and contains the following:
namespace GameProtocol
{
public struct BuyShopItem
{
public int ShopItemID;
public int Amount;
public int GoldPrice;
public int SilverPrice;
public int CharacterPointPrice;
public int ResearchPointPrice;
}
}
It's probably because of the external assembly, right?
Unfortunately, i cannot change it as i need to pass the BuyShopItem back again to another external Assembly.
Some information about the assembly: It's from a Unity game, .NET 3.5 (according to DotPeek: msil, .Net Framework v3.5)
I'm having the issue in SharpDevelop as well as Visual Studio 2017, so it probably not IDE-related.
Result of peverify:
Microsoft (R) .NET Framework PE Verifier. Version 4.0.30319.0
Copyright (c) Microsoft Corporation. Alle Rechte vorbehalten.
[MD]: Error: Field has a duplicate, token=0x040059d7. [Token:0x040059CF]
[MD]: Error: Field has a duplicate, token=0x040059cf. [Token:0x040059D7]
[MD]: Error: Field has a duplicate, token=0x0400a48b. [Token:0x0400A478]
[MD]: Error: Field has a duplicate, token=0x0400a478. [Token:0x0400A48B]
4 Fehler wird/werden überprüft Assembly-CSharp.dll
If you have any Hints of what it could be, please tell me. I will try it out as soon as i can.
Here is a screenshot of the Exception in Visual Studio 2017:
https://i.imgur.com/WHCbWTM.png
Update: I just tried the following: Console.WriteLine(typeof(BuyShopItem));, same error occured. Why isn't it possible to get the type?
It is OK to create a zero-length list of structs.
It is also OK to create a list of some type that is defined in a different assembly - but check to make sure that the necessary DLL can be located at runtime.
Instances of structs are generally fairly safe to construct - but check to see if there is a custom constructor that is throwing an exception.
Also carefully check to see if the BuyShowItem struct specifies any static fields that call into other code, which in turn may be failing. This is often the root cause of TypeInitializationException.
See: https://msdn.microsoft.com/en-us/library/system.typeinitializationexception(v=vs.110).aspx

Activator.CreateInstance(type) Throws Exception

Actually it is very strange exception, because it happens only when I build the project as Release and does not happen at all when I choose Debug. In debug mode, the app works perfect and the following code works well.
Here is the code of my extension method:
public static T DeepClone<T>(this T source) where T : UIElement
{
T result;
// Get the type
Type type = source.GetType();
// Create an instance
result = Activator.CreateInstance(type) as T; //throws exception here only if I build project as (release)
CopyProperties<T>(source, result, type);
DeepCopyChildren<T>(source, result);
return result;
}
The exception is:
An exception of type 'System.MissingMethodException' occurred in
System.Private.Reflection.Execution.dll but was not handled in user
code
Additional information: MissingConstructor_Name,
Windows.UI.Xaml.Controls.RelativePanel. For more information, visit
http://go.microsoft.com/fwlink/?LinkId=623485
I've found some related questions to this exception, but they all are pointing to missing libraries or update libraries like this but didn't change anything in my app.
This problem is related to the fact that Release build of UWP apps uses the .NET native toolchain. In this mode reflection needs some hints to work properly. Aparently the constructor of the RelativePanel is not available for reflection.
Luckily there is a workaround for this as described in this blogpost.
In your UWP project's Properties folder is a file called default.rd.xml. Open it and add the following line inside the <Applications> element:
<Type Name="Windows.UI.Xaml.Controls.RelativePanel"
Dynamic="Required All" Activate="Required All" />
The Dynamic attribute should ensure reflection is possible and the Activate attribute should ensure the constructors are available for activation - which is key for your case.
This should include all members of RelativePanel for reflection and everything should work as expected.
You can see more details on the default.rd.xml file structure here.

How to collect types from current solution using Visual Studio Extension?

I have created Visual Studio 2012 Package (using VS2012 SDK). This Extension (if installed on the client's IDE environment) should has, among other things, a functionality of collecting all specific types from currently opened solution which developer is working on. A similar feature is embedded in Visual Studio Designer for ASP.NET MVC Application Project, where developer implements a Model/Controller class, build a project, and then is able to access this type in Scaffolding UI (Designer's dropdown list). The corresponding features are also available in WPF, WinForms Visual Designers, etc.
Let's say that my extension has to collect all types from current solution, which implement ISerializable interface. The steps are following: Developer creates specific class, rebuild containing project/solution, then do some action provided by extension UI, thus involves performing ISerializabletypes collecting.
I have tried to implement collecting operation using reflection:
List<Type> types = AppDomain.CurrentDomain.GetAssemblies().ToList()
.SelectMany(s => s.GetTypes())
.Where(p => typeof(ISerializable).IsAssignableFrom(p) && !p.IsAbstract).ToList();
But above code causes System.Reflection.ReflectionTypeLoadException exception to be thrown:
System.Reflection.ReflectionTypeLoadException was unhandled by user code
HResult=-2146232830
Message=Unable to load one or more of the requested types. Retrieve the LoaderExceptions property for more information.
Source=mscorlib
StackTrace:
at System.Reflection.RuntimeModule.GetTypes(RuntimeModule module)
at System.Reflection.RuntimeModule.GetTypes()
at System.Reflection.Assembly.GetTypes()
(...)
LoaderException: [System.Exception{System.TypeLoadException}]
{"Could not find Windows Runtime type 'Windows.System.ProcessorArchitecture'.":"Windows.System.ProcessorArchitecture"}
(...)
How can I properly implement operation of collecting specific types from currently built solution?
I was trying to do a similar thing and unfortunately the only way around this I've found so far is by doing the following (which I feel is a bit messy, but maybe with some tweaking for a specific situation might be ok)
var assemblies = AppDomain.CurrentDomain.GetAssemblies();
IEnumerable<Type> types = assemblies.SelectMany(x => GetLoadableTypes(x));
...
public static IEnumerable<Type> GetLoadableTypes(Assembly assembly)
{
try
{
return assembly.GetTypes();
}
catch (ReflectionTypeLoadException e)
{
return e.Types.Where(t => t != null);
}
}
This would give you all types, but you can filter out whatever you wish.
Referenced this post: How to prevent ReflectionTypeLoadException when calling Assembly.GetTypes()
I'm not sure if I understand you correctly, but if I am this will make it:
var assembly = Assembly.GetExecutingAssembly();
IEnumerable<Type> types =
assembly.DefinedTypes.Where(t => IsImplementingIDisposable(t))
.Select(t => t.UnderlyingSystemType);
........
private static bool IsImplementingIDisposable(TypeInfo t)
{
return typeof(IDisposable).IsAssignableFrom(t.UnderlyingSystemType);
}

Is it possible to use RoGetMetaDataFile from non Windows Store App

After reading Larry Osterman's response on the very same issue I am trying to solve at the moment, I thought I had found the answer to my question.
For the record, the question was : how can I from .Net (non-WinRT) list the types in a WinRT assembly ( mine are .dll files apparently, not .Winmd)
I therefore used the following code snippet :
//note, this wrapper function returns the metadata file name and token
// it immediately releases the importer pointer
static Tuple<string, UInt32> ResolveTypeName(string typename)
{
string path;
object importer = null;
UInt32 token;
try
{
var hr = RoGetMetaDataFile(typename, IntPtr.Zero, out path, out importer, out token);
//TODO: check HR for error
return Tuple.Create(path, token);
}
finally
{
Marshal.ReleaseComObject(importer);
}
}
[DllImport("WinTypes.dll")]
static extern UInt32 RoGetMetaDataFile(
[MarshalAs(UnmanagedType.HString)] string name,
IntPtr metaDataDispenser,
[MarshalAs(UnmanagedType.HString)] out string metaDataFilePath,
[MarshalAs(UnmanagedType.Interface)] out object metaDataImport,
out UInt32 typeDefToken);
( found on https://gist.github.com/2920743)
Unfortunately, I get a non-zero HResult.
I referred to the documentation and found this :
HR_RESULT_FROM_WIN32(ERROR_NO_PACKAGE) The function was called from a
process that is not in a Windows Store app.
Does that mean it is not possible to list the types from .Net (non-WinRT) at all ?
RoGetMetaDataFile is used to load a metadata file from within an app package. It locates the metadata file in which the named type is defined, loads that metadata file, and returns an IMetaDataImport interface pointer that represents that metadata file.
From ordinary .NET code you can call RuntimeEnvironment.GetRuntimeInterfaceAsIntPtr (or GetRuntimeInterfaceAsObject) to get the current runtime's IMetaDataDispenser interface pointer, which can be used to load arbitrary modules for inspection.
From native code, you can call ICLRMetaHost::GetRuntime to load a runtime, then from that object call ICLRRuntimeInfo::GetInterface to get its IMetaDataDispenser interface pointer.
RoGetMetaDataFile can be used from outside the app package, however it will only resolve system windows runtime types.
In order to resolve app specific types, you need to be running with "package identity" - in other words, in the context of a running application.

How to create an ActiveX control in C#?

I am not able to create a functioning ActiveX control in C#; I have tried following tutorials to do so without success.
I create a sample Class Library project which includes this code:
namespace AACWCSurvey
{
[ProgId("Prisoner.PrisonerControl")]
[ClassInterface(ClassInterfaceType.AutoDual)]
public class Class1
{
public Class1()
{
MessageBox.Show("FIRETRUCK!!!");
}
}
}
I then did the following steps:
Properties => Application => Assembly Information => Make Assembly COM-visible
Build => Register for COM interop TRUE (checked)
Make Strong name for assembly (signing)
Build the project
regasm MyDll.dll /tlb /codebase
Can't see Prisoner.PrisonerControl in tstcon32 =(
My OS is WinXP x86.
UPD: it works from VBScript:
Dim objJava
Set objJava = WScript.CreateObject("Prisoner.PrisonerControl")
but it is not visible in tstcon32.
If you read the actual article using the Prisoner.PrisonerControl control a sub key named Control is created inside the key with your control GUID.
On my machine with the guid {9DEA5F06-E324-31A7-837B-D0F3BDE91423} creating the key
HKEY_CLASSES_ROOT\CLSID\{9DEA5F06-E324-31A7-837B-D0F3BDE91423}\Control
Make the control appears in tstcon32. And with or without it the ActiveX is usable for javascript
var x = new ActiveXControl("Prisoner.PrisonerControl");
Actually i had to fight windows on both the javascript execution and registry path to test it on my system because it's an x64 machine but that's another story.
You have created a COM server but not an ActiveX control, which is a far more intricate COM object, the kind that you can exercise with tstcon32.exe.
It must implement a bunch of interfaces, key ones are IOleObject and IOleWindow. The kind of interfaces that allows it to do the required negotiations with an ActiveX host and create a visible window. The Winforms Control class is your best bet to create one.
Here are the relevant steps as documented externally. This is summarized leaving out some exposition but not any necessary steps.
This example is also very similar to the article Using Managed Controls as ActiveX Controls by Garry Trinder, November 25, 2008 and I've included some notes from this article as well.
Exposing Windows Forms Controls as ActiveX controls
This article will describe how to utilise Windows Forms controls
outside of .NET.
Writing the control
Create a new control project from within Visual Studio - my examples are all in C# but VB.NET could also be used.
[Here Garry's article suggests, "First, create a managed usercontrol project – either a Windows Forms class library or control library project. Use the usercontrol designer to design your custom usercontrol the way you want it (using any standard controls you like)."]
Add controls etc to the form, put in the code etc.
Add in the following using clauses...
using System.Runtime.InteropServices;
using System.Text;
using System.Reflection;
using Microsoft.Win32;
Attribute your class so that it gets a ProgID. This isn't strictly necessary as one will be generated, but it's almost always best to be
explicit.
[ProgId("Prisoner.PrisonerControl")]
[ClassInterface(ClassInterfaceType.AutoDual)]
This assigns the ProgID, and also defines that the interface
exposed should be 'AutoDual' - this crufts up a default interface for
you from all public, non-static members of the class. If this isn't
what you want, use one of the other options.
Update the project properties so that your assembly is registered for COM interop.
If you're using VB.NET, you also need a strong named assembly.
Curiously in C# you don't - and it seems to be a feature of the
environment rather than a feature of the compiler or CLR.
Add the following two methods into your class.
[ComRegisterFunction()]
public static void RegisterClass ( string key )
{
// Strip off HKEY_CLASSES_ROOT\ from the passed key as I don't need it
StringBuilder sb = new StringBuilder ( key ) ;
sb.Replace(#"HKEY_CLASSES_ROOT\","") ;
// Open the CLSID\{guid} key for write access
RegistryKey k = Registry.ClassesRoot.OpenSubKey(sb.ToString(),true);
// And create the 'Control' key - this allows it to show up in
// the ActiveX control container
RegistryKey ctrl = k.CreateSubKey ( "Control" ) ;
ctrl.Close ( ) ;
// Next create the CodeBase entry - needed if not string named and GACced.
RegistryKey inprocServer32 = k.OpenSubKey ( "InprocServer32" , true ) ;
inprocServer32.SetValue ( "CodeBase" , Assembly.GetExecutingAssembly().CodeBase ) ;
inprocServer32.Close ( ) ;
// Finally close the main key
k.Close ( ) ;
}
The RegisterClass function is attributed with ComRegisterFunction -
this static method will be called when the assembly is registered for
COM Interop. All I do here is add the 'Control' keyword to the
registry, plus add in the CodeBase entry.
CodeBase is interesting - not only for .NET controls. It defines a URL
path to where the code can be found, which could be an assembly on
disk as in this instance, or a remote assembly on a web server
somewhere. When the runtime attempts to create the control, it will
probe this URL and download the control as necessary. This is very
useful when testing .NET components, as the usual caveat of residing
in the same directory (etc) as the .EXE does not apply.
[ComUnregisterFunction()]
public static void UnregisterClass ( string key )
{
StringBuilder sb = new StringBuilder ( key ) ;
sb.Replace(#"HKEY_CLASSES_ROOT\","") ;
// Open HKCR\CLSID\{guid} for write access
RegistryKey k = Registry.ClassesRoot.OpenSubKey(sb.ToString(),true);
// Delete the 'Control' key, but don't throw an exception if it does not exist
k.DeleteSubKey ( "Control" , false ) ;
// Next open up InprocServer32
RegistryKey inprocServer32 = k.OpenSubKey ( "InprocServer32" , true ) ;
// And delete the CodeBase key, again not throwing if missing
k.DeleteSubKey ( "CodeBase" , false ) ;
// Finally close the main key
k.Close ( ) ;
}
The second function will remove the registry entries added when (if)
the class is unregistered - it's always a good suggestion to tidy up
as you go.
Now you are ready to compile & test your control.
Additional notes from Garry's blog:
[The] additional registry entries: Control, MiscStatus, TypeLib and
Version [can be created] with a .REG script, but it’s generally better
to write functions that will be called on registration/unregistration
He describes the registry keys in some detail:
Control is an empty subkey. TypeLib is mapped to the GUID of the
TypeLib (this is the assembly-level GUID in the assemblyinfo.cs).
Version is the major and minor version numbers from the assembly
version. The only mildly interesting subkey is MiscStatus. This needs
to be set to a value composed of the (bitwise) values in the OLEMISC
enumeration, documented here. To make this enum available, add a
reference to Microsoft.VisualStudio.OLE.Interop (and a suitable
‘using’ statement for the namespace).
His final note is a warning:
Note: this seems to work OK for Excel (with the very limited testing
I've done), partly works with PowerPoint, but fails miserably with
Word. Possibly, some more of the OLEMISC values might improve this;
possibly there are some messages we need to hook; possibly there are
some more interfaces we need to implement ... The fact that I’ve only
barely got it to work in a very limited way should tell you that this
is probably not a technique you want to use in any serious way.

Categories

Resources