Use a C++/CLI DLL using afxwinforms.h as Reference of another C++/CLI DLL also using afxwinforms.h - dll

In Visual C++ 2010 i added a reference from a C++/CLI DLL (ControlWrapper.dll) to another C++/CLI DLL (CliLibrary.dll).
Both are including afxwinforms.h in the stdafx.h.
When i try to compile i get these errors:
error C2011: 'Microsoft::VisualC::MFC::CWin32Window' : 'class' type redefinition
error C2011: 'Microsoft::VisualC::MFC::CWinFormsEventsHelper' : 'class' type redefinition
If i turn of the Option Reference Assembly Output and add #using "CliLibrary.dll" to the using .cpp File i get the following warnings:
1>ControlWrapper.dll : warning C4944: 'CWin32Window' : cannot import symbol from 'c:\dev\trunk\CliLibrary.dll': as 'Microsoft::VisualC::MFC::CWin32Window' already exists in the current scope
1> C:\Program Files (x86)\Microsoft Visual Studio 10.0\VC\atlmfc\include\afxwinforms.h(83) : see declaration of 'Microsoft::VisualC::MFC::CWin32Window'
1> This diagnostic occurred while importing type 'Microsoft.VisualC.MFC.CWin32Window' from assembly 'CliLibrary, Version=1.0.4843.17337, Culture=neutral, PublicKeyToken=null'.
1>ControlWrapper.dll : warning C4944: 'CWinFormsEventsHelper' : cannot import symbol from 'c:\dev\sfirm\trunk\sfclrlib\debug\sfclrlib.dll': as 'Microsoft::VisualC::MFC::CWinFormsEventsHelper' already exists in the current scope
1> C:\Program Files (x86)\Microsoft Visual Studio 10.0\VC\atlmfc\include\afxwinforms.h(122) : see declaration of 'Microsoft::VisualC::MFC::CWinFormsEventsHelper'
1> This diagnostic occurred while importing type 'Microsoft.VisualC.MFC.CWinFormsEventsHelper' from assembly 'CliLibrary, Version=1.0.4843.17337, Culture=neutral, PublicKeyToken=null'.
How could i solve the error?

Well, this is a painful problem. It certainly explains why you are the first programmer I ever encountered that actually uses this. The problem is caused by this declaration in afxwinforms.h:
public ref class CWin32Window : public System::Windows::Forms::IWin32Window
// etc..
The public keyword is the killer, that adds the class to the manifest of your assembly. So when you reference it in another project that also includes the header then there are two definitions of the class. The mix of both native and managed classes in that header prevents a clean solution.
I think you already found the best solution, using the #include, with #pragma comment(disable:4944) to shutup the compiler. Including the header inside a namespace might be another viable hack, it renames the namespace of CWin32Window, but I'd expect trouble when linking mfcm90.lib. Restructuring your solution and keeping all winforms code inside one project is the only thing I can recommend.

Are you using the types in afxwinforms.h?
If not (like in my case), the solution is to comment the include and add this two lines:
#using <System.Windows.Forms.dll>
#using <System.Drawing.dll>
or add a reference to that two assemblies in your Project.

Related

Registering .net assembly for COM succeeds with regasm but fails using RegistrationServices.RegisterAssembly

This is one of the strangest issue I have encountered.
There is a .net assembly, which is exposed to COM.
If you register it with regasm /codebase my.dll - it is sucessfully registered, and can be used.
However, if you register it from code using RegistrationServices.RegisterAssembly() :
[...]
RegistrationServices regSvcs = new RegistrationServices();
Assembly assembly = Assembly.LoadFrom(path);
// must call this before overriding registry hives to prevent binding failures on exported types during RegisterAssembly
assembly.GetExportedTypes();
using (RegistryHarvester registryHarvester = new RegistryHarvester(true))
{
// ******** this throws *********
regSvcs.RegisterAssembly(assembly, AssemblyRegistrationFlags.SetCodeBase);
}
Then it throws exception:
Could not load file or assembly 'Infragistics2.Win.UltraWinTree.v9.2, Version=9.2.20092.2083,
Culture=neutral, PublicKeyToken=7dd5c3163f2cd0cb' or one of its dependencies.
Provider type not defined. (Exception from HRESULT: 0x80090017)
This error has very little resource on the net, and looks like related to some security(?) cryptography(?) feature.
After long-long hours, I figured out what causes this (but don't know why):
If there is a public class with a public constructor in the assembly with a parameter UltraTree (from the referenced assembly 'Infragistics2.Win.UltraWinTree.v9.2'), then you cannot register from code, but with regasm only.
When I changed the have a public function Init(UltraTree tree), then it works, I can register from code. So:
// regasm: OK / RegistrationServices.RegisterAssembly(): exception
public class Foo
{
public Foo(UltraWinTree tree) { .. }
}
Foo foo = new Foo(_tree);
-------------- vs --------------
// regasm: OK / RegistrationServices.RegisterAssembly(): OK
public class Foo
{
public Foo() {}
public void Init(UltraWinTree tree) { .. }
}
Foo foo = new Foo();
foo.Init(_tree);
So I could workaround by passing UltraWinTree in a new Init() function instead of constructor, but this is not nice, and I want to know the reason, what the heck is going on?
Anyone has any idea? Thanks.
PS:
Okay, but why we want to register from code? As we use Wix to create installer, which uses heat.exe to harvest registry entries (which are added during asm registration), so heat.exe does assembly registration from code.
I've been dealing with this for years so this is the only answer you need to read:
Heat calls regasm /regfile. So does InstallShield when you tell it to. If you read this page:
https://learn.microsoft.com/en-us/dotnet/framework/tools/regasm-exe-assembly-registration-tool
There's a very important caveat in the remarks section.
You can use the /regfile option to generate a .reg file that contains
the registry entries instead of making the changes directly to the
registry. You can update the registry on a computer by importing the
.reg file with the Registry Editor tool (Regedit.exe). The .reg file
does not contain any registry updates that can be made by user-defined
register functions. The /regfile option only emits registry entries
for managed classes. This option does not emit entries for TypeLibIDs
or InterfaceIDs.
So what to do? Use Heat to generate most of the metadata. Then on a clean machine, (snapshot VM best) us a registry snapshot and compare tool such as InCntrl3 or InstallWatch Pro and sniff out what additional meta regasm writes to the registry. Finally massage that into your Wxs code.
Then on a cleam machine test the install. The result should work and not require any custom actions in the install.

XAML UWP Windows Store : error CS0266/warning CS1697 when deploying solution

I've some warning/error when trying to "create App Packages" from a XAML UWP project.
Firstly a warning :
2017\Projects\MyProjectName\MyProjectName\obj\x86\Release\MyPageName.g.cs(1,1,1,194): warning CS1697: Different checksum values given for C:\Users\administrateur\Documents\Visual Studio 2017\Projects\MyProjectName\MyProjectName\MyPageName.xaml
It seem there is a problem with space (the second path is beginning after the last space -before "2017"-). I'm on Win 10.
Ive an cast error too :
1>C:\Users\administrateur\Documents\Visual Studio 2017\Projects\MyProjectName\MyProjectName\obj\x86\Release\MyPageName.g.cs(39,36,39,85): error CS0266: Cannot implicitly convert type 'Windows.UI.Xaml.Controls.Button' to 'MyProjectName.MyButtonReturn'. An explicit conversion exists (are you missing a cast?)
MyButtonReturn is a button coded in a class extending Button, the definition of "MyButtonReturn" is :
public sealed class MyButtonReturn : Button
{...
Any help ?
Thanks
Found the solution, cleaning the solution + building the project on release worked.

Powershell v2 - The correct way to load an assembly as part of a module

I have a Powershell (v2.0) script that I am working on.
One of the first things it does is loads a Module (runMySQLQuery.psm1) which includes (amongst other bits) a function to connect to a MySQL Database.
Import-Module runMySQLQuery.psm1
However, this requires that an assembly MySQL.Data.DLL is loaded in order for this to work.
If I place the line:
[void][system.reflection.Assembly]::LoadFrom("C:\PowerShell\modules\runMySQLQuery\MySql.Data.dll")
at the top of my script (separate to the Import-Module entry), then the whole thing works fine.
But I want to be able to load this assembly at the same time as the module, so that I don't have to worry about forgetting to including the assembly each time I use this module.
I tried placing it at the top of the .psm1 file, but that didn't work.
I then added it to my manifest file as:
RequiredAssemblies = #("C:\PowerShell\modules\runMySQLQuery\MySql.Data.dll")
This also didn't work.
Am I missing something here, is there a proper way to include assemblies as part of a module?
n.b. the error I get when it hasn't loaded properly is:
You cannot call a method on a null-valued expression
Can you just try to assign a varialbe a the beginig of you module :
$dumy = [system.reflection.Assembly]::LoadFrom("C:\PowerShell\modules\runMySQLQuery\MySql.Data.dll")

My Helpers.vbhtml file results in "Type 'ASP.global_asax' is not defined"

I created the ASP.NET special folder called App_Code in my MVC project. I added a new file Helpers.vbhtml that will contain repeatedly-used razor code snippets.
Helpers.vbhtml
#Helper GetTime()
#DateTime.Now
End Helper
But this results in the error Type 'ASP.global_asax' is not defined. (x2)
Nothing seems to be affected - the application still compiles and runs. What's this error mean?
This error is caused by a naming conflict; there is a namespace called System.Web.Helpers.
The problem appears to be VB.NET-specific. I can't reproduce the problem in C#.
Rename the .vbhtml file to Snippets.vbhtml or something else that won't cause a naming conflict.

Obtaining a list of resolved reference paths in msbuild

I'm trying to use ILMerge to merge multiple assemblies into one plugin assembly, which will be dynamically loaded by another application.
Using the community tasks, I've so far got:
<Target Name="CombineAssemblies" AfterTargets="MvcBuildViews">
<ItemGroup>
<MergePaths Include="#(_ResolvedProjectReferencePaths)" />
<!--<MergePaths Include="%(Reference.ResolvedPath)" /> This doesn't work! -->
</ItemGroup>
<ILMerge
DebugInfo="true"
LogFile="log.txt"
InputAssemblies="#(MergePaths)"
OutputFile=""$(TargetPath)""
CopyAttributes="true"
TargetPlatformVersion="v4"/>
</Target>
Using #(_ResolvedProjectReferencePaths) works fine for the project references, and their paths are passesd correctly through to the ILMerge task.
When running this through MSBuild, ILMerge.exe errors out. Looking at the log file, I get:
Unresolved assembly reference not allowed: System.Web.Mvc.
at System.Compiler.Ir2md.GetAssemblyRefIndex(AssemblyNode assembly)
at System.Compiler.Ir2md.GetTypeRefIndex(TypeNode type)
at System.Compiler.Ir2md.VisitReferencedType(TypeNode type)
at System.Compiler.Ir2md.VisitMethod(Method method)
at System.Compiler.Ir2md.VisitClass(Class Class)
at System.Compiler.Ir2md.VisitModule(Module module)
at System.Compiler.Ir2md.SetupMetadataWriter(String debugSymbolsLocation)
at System.Compiler.Ir2md.WritePE(Module module, String debugSymbolsLocation, BinaryWriter writer)
at System.Compiler.Writer.WritePE(String location, Boolean writeDebugSymbols, Module module, Boolean delaySign, String keyFileName, String keyName)
at System.Compiler.Writer.WritePE(CompilerParameters compilerParameters, Module module)
at ILMerging.ILMerge.Merge()
at ILMerging.ILMerge.Main(String[] args)
I'm referencing the MVC libraries, which from the log file I assume could not be resolved by ILMerge, even though they are resolved correctly in the actual compilation process.
I therefore want to pass the location of these libraries through to ILMerge, ideally using the References item so I don't have to manually add a bunch of reference paths which would have to change if I added or removed references. Is there some equivalent to #(_ResolvedProjectReferencePaths) for normal references?
Thanks!
Try #(ReferencePath), which should be used after target ResolveAssemblyReferences.