Problems running a C# C++/CLI wrapper solution on x64 - c#

I am using Visual Studio 2019 version 16.11.10. I am trying to do a small C# project that can communicate with an C++ object with functions. What i understand was that i have to use something called C++/CLI wrapper.
I tried to follow this tutorial: redgate - Creating a C++/CLI Wrapper
The project consists of one solution with 3 projects. The three projects are Sandbox (C# project), Wrapper (C++/CLI wrapper project) and Core (C++ project).
A detail about the Core-project is that you have to configure it as Static library (.lib). To do it you will Right-click on the Core-project and select Properties. Under General we select Static library (.lib).
A detail about the Wrapper-project is that you have select “Not Using Precompiled Headers” at Properties->C/C++->Precompiled Headers
I have done some kind of x86/x64 “alignment” for the projects in the Configuration Manager. If i select x86 as solution platform i will set Sandbox to x86, Core to Win32 and Wrapper to Win32. If i select x64 as solution platform i will set Sandbox, Core and Wrapper to x64
I had problems to run the solution on x86. The problem was that Wrapper and Sandbox used different .NET-versions. So now, if both use the same .NET-version (for example .NET 3.1 or .NET 5) for Wrapper and Sandbox, then i can run the solution on x86.
I have problems running the solution on x64. When i run it i get this error:
Microsoft Visual Studio
The debug executable
'C:\Users\Philip\kodning2\vsworkdir\egnaTestProjekt\Solution_CLIExample3\Core\Sandbox\bin\x64\Debug\net5.0\Sandbox.exe'
specified in the 'Sandbox' debug profile does not exist.
OK
I also get these Errors:
Severity Code Description Project File Line Suppression State
Error LNK2019 unresolved external symbol main referenced in function "int __cdecl invoke_main(void)" (?invoke_main##YAHXZ) Core C:\Users\Philip\kodning2\vsworkdir\egnaTestProjekt\Solution_CLIExample3\Core\Core\MSVCRTD.lib(exe_main.obj) 1
Error C1010 unexpected end of file while looking for precompiled header. Did you forget to add '#include "pch.h"' to your source? Wrapper C:\Users\Philip\kodning2\vsworkdir\egnaTestProjekt\Solution_CLIExample3\Core\Wrapper\Entity.cpp 16
Error CS0246 The type or namespace name 'Entity' could not be found (are you missing a using directive or an assembly reference?) Sandbox C:\Users\Philip\kodning2\vsworkdir\egnaTestProjekt\Solution_CLIExample3\Core\Sandbox\Program.cs 9 Active
Error CS0246 The type or namespace name 'Entity' could not be found (are you missing a using directive or an assembly reference?) Sandbox C:\Users\Philip\kodning2\vsworkdir\egnaTestProjekt\Solution_CLIExample3\Core\Sandbox\Program.cs 9 Active
Error CS0246 The type or namespace name 'CLI' could not be found (are you missing a using directive or an assembly reference?) Sandbox C:\Users\Philip\kodning2\vsworkdir\egnaTestProjekt\Solution_CLIExample3\Core\Sandbox\Program.cs 2 Active
Error CS0006 Metadata file 'C:\Users\Philip\kodning2\vsworkdir\egnaTestProjekt\Solution_CLIExample3\Core\x64\Debug\Wrapper.dll' could not be found Sandbox C:\Users\Philip\kodning2\vsworkdir\egnaTestProjekt\Solution_CLIExample3\Core\Sandbox\CSC 1 Active
Error LNK1120 1 unresolved externals Core C:\Users\Philip\kodning2\vsworkdir\egnaTestProjekt\Solution_CLIExample3\Core\x64\Debug\Core.exe 1
How to be able to run this solution as x64? What i am doing wrong?
EDIT1:
As Alan Birtles suggested, i post the text from the Output-window:
Build started...
1>------ Build started: Project: Core, Configuration: Debug x64 ------
Restored C:\Users\Philip\kodning2\vsworkdir\egnaTestProjekt\Solution_CLIExample3\Core\Sandbox\Sandbox.csproj (in 3 ms).
1>Entity.cpp
1>MSVCRTD.lib(exe_main.obj) : error LNK2019: unresolved external symbol main referenced in function "int __cdecl invoke_main(void)" (?invoke_main##YAHXZ)
1>C:\Users\Philip\kodning2\vsworkdir\egnaTestProjekt\Solution_CLIExample3\Core\x64\Debug\Core.exe : fatal error LNK1120: 1 unresolved externals
1>Done building project "Core.vcxproj" -- FAILED.
2>------ Build started: Project: Wrapper, Configuration: Debug x64 ------
2>pch.cpp
2>AssemblyInfo.cpp
2>Entity.cpp
2>C:\Users\Philip\kodning2\vsworkdir\egnaTestProjekt\Solution_CLIExample3\Core\Wrapper\Entity.cpp(16,1): fatal error C1010: unexpected end of file while looking for precompiled header. Did you forget to add '#include "pch.h"' to your source?
2>Wrapper.cpp
2>Generating Code...
2>Done building project "Wrapper.vcxproj" -- FAILED.
3>------ Build started: Project: Sandbox, Configuration: Debug x64 ------
3>CSC : error CS0006: Metadata file 'C:\Users\Philip\kodning2\vsworkdir\egnaTestProjekt\Solution_CLIExample3\Core\x64\Debug\Wrapper.dll' could not be found
3>Done building project "Sandbox.csproj" -- FAILED.
========== Build: 0 succeeded, 3 failed, 0 up-to-date, 0 skipped ==========

Related

Problem to build VS-project with DCOM-refs under GitHub Actions

One of the C# projects in my solution has references to 2 DCOM apps (win32, quite an old DCOM apps).
To successfully build my VS project on PC it is enough to copy 2 exe files and run 2 commands:
XObjectsEx.exe -regserver
SfGbVars.exe -regserver
And then I can run a command to build it:
msbuild SfR2.sln -p:Configuration=Release
Build is successful.
Thus, to successfully build my solution it is enough to do only these steps:
take a new clean PC install (for example: clean VM install with Windows 10/2012/2019 - just any, I tried all of these)
install MS Build Tools 2019
copy 2 exe files + run 2 mentioned commands
run mentioned build command
But(!)...
When I try to do the same under GitHub Actions it reports errors like these:
C:\Program Files (x86)\Microsoft Visual Studio\2019\Enterprise\MSBuild\Current\Bin\Microsoft.Common.CurrentVersion.targets(2827,5): warning MSB3284: Cannot get the file path for type library "d6ed8d7e-1780-4747-b41b-a685526cb19b" version 1.0. Library not registered. (Exception from HRESULT: 0x8002801D (TYPE_E_LIBNOTREGISTERED)) [D:\a\spectrum-abz-scm-sw-cust\spectrum-abz-scm-sw-cust\Modules\SfManagement\SfR2\assemblies\Sf.OleOp\source\Sf.OleOp.csproj]
CoreCompile:
[...]
D:\a\spectrum-abz-scm-sw-cust\spectrum-abz-scm-sw-cust\Modules\SfManagement\SfR2\assemblies\Sf.OleOp\source\OprGbVar.cs(23,7): error CS0246: The type or namespace name 'SFGlobalVars' could not be found (are you missing a using directive or an assembly reference?) [D:\a\spectrum-abz-scm-sw-cust\spectrum-abz-scm-sw-cust\Modules\SfManagement\SfR2\assemblies\Sf.OleOp\source\Sf.OleOp.csproj]
D:\a\spectrum-abz-scm-sw-cust\spectrum-abz-scm-sw-cust\Modules\SfManagement\SfR2\assemblies\Sf.OleOp\source\OprGbVar.cs(744,17): error CS0246: The type or namespace name 'SFGlobalVars' could not be found (are you missing a using directive or an assembly reference?) [D:\a\spectrum-abz-scm-sw-cust\spectrum-abz-scm-sw-cust\Modules\SfManagement\SfR2\assemblies\Sf.OleOp\source\Sf.OleOp.csproj]
CompilerServer: server - server processed compilation - 93f84f1f-193a-4dcc-9b12-98452dca1411
In attempt to resolve this problem I added a step to YML file (a step before calling the build itself):
- name: Restore artifacts (SF)
run: build-tools\scripts\InstallArtifacts.ps1 ${{ env.RepoRoot }}/${{ env.OpcPackages_Path }}
working-directory: ${{ env.RepoRoot }}
Where InstallArtifacts.ps1 script executing also following commands:
XObjectsEx.exe -regserver
SfGbVars.exe -regserver
rem ===== .NET registration =====
set NetHome=C:\Windows\Microsoft.NET\Framework\v4.0.30319
set SdkHome=C:\Program Files (x86)\Microsoft SDKs\Windows\v10.0A\bin\NETFX 4.7.2 Tools
"%SdkHome%\TlbImp.exe" SfGbVars.exe /out:SfGbVars_Net.dll
"%SdkHome%\TlbImp.exe" XObjectsEx.exe /out:XObjectsEx_Net.dll
"%NetHome%\RegAsm.exe" SfGbVars_Net.dll
"%NetHome%\RegAsm.exe" XObjectsEx_Net.dll
In GitHub Actions output I clearly see that all these commands were correctly executed. In output it writes "Types registered successfully" and so on.
Also I checked generated SfGbVars_Net.dll and XObjectsEx_Net.dll with Reflector and confirms - there is a valid .NET wrappers for these DCOM objects.
However that does not affect the build result. It is still reporting the same errors! :-\
Could you please advise - how to resolve this problem?
Why there is such a strange difference in behavior between normal PC and virtual PC under GitHub Actions? So, on PC it is enough to run {app} -regserver command but under GitHub Actions that does not work.
Note:
here example of my YML file - https://drive.google.com/file/d/1Id-sMWT7BrGj-TBGvmVEpPIVe_kH9IJq/view?usp=share_link

Azure deployment failes due to missing assembly reference

So I have a simple C# Web API project that I'm attempting to get deployed into Azure. The project builds and runs fine locally (dont they always?!?) but in Azure it fails due to a missing Microsoft.ServiceBus assembly which is used.
The project structure is quite simple:
\
--\Common
----MyProj.common.dll << This uses Microsoft.ServiceBus and has its own packages.config with WindowsAzure.ServiceBus referenced
--\Web
----MyProj.Api << This has an assembly reference to MyProj.common.dll. It doesn't have a reference to WindowsAzure.ServiceBus itself (although I added one to see if it helped, but it didn't)
The output from the Azure deployment script is as follows:
Command: "D:\home\site\deployments\tools\deploy.cmd"
Handling ASP.NET Core Web Application deployment.
Restoring packages for D:\home\site\repository\MyProj\Web\MyProj.Api\MyProj.Api.csproj...
Installing WindowsAzure.ServiceBus 4.0.0.
Writing lock file to disk. Path: D:\home\site\repository\MyProj\Web\MyProj.Api\obj\project.assets.json
Restore completed in 12.73 sec for D:\home\site\repository\MyProj\Web\MyProj.Api\MyProj.Api.csproj.
NuGet Config files used:
D:\local\AppData\NuGet\NuGet.Config
Feeds used:
https://api.nuget.org/v3/index.json
Installed:
1 package(s) to D:\home\site\repository\MyProj\Web\MyProj.Api\MyProj.Api.csproj
Microsoft (R) Build Engine version 15.1.548.43366
Copyright (C) Microsoft Corporation. All rights reserved.
D:\Program Files (x86)\dotnet\sdk\1.0.1\Microsoft.Common.CurrentVersion.targets(1964,5): warning MSB3245: Could not resolve this reference. Could not locate the assembly "Microsoft.ServiceBus, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL". Check to make sure the assembly exists on disk. If this reference is required by your code, you may get compilation errors. [D:\home\site\repository\MyProj\MyProj.Shared\MyProj.Common.csproj]
Azure\AQM.cs(7,17): error CS0234: The type or namespace name 'ServiceBus' does not exist in the namespace 'Microsoft' (are you missing an assembly reference?) [D:\home\site\repository\MyProj\MyProj.Shared\MyProj.Common.csproj]
Azure\AQM.cs(8,17): error CS0234: The type or namespace name 'ServiceBus' does not exist in the namespace 'Microsoft' (are you missing an assembly reference?) [D:\home\site\repository\MyProj\MyProj.Shared\MyProj.Common.csproj]
Azure\AQM.cs(14,17): error CS0246: The type or namespace name 'NamespaceManager' could not be found (are you missing a using directive or an assembly reference?) [D:\home\site\repository\MyProj\MyProj.Shared\MyProj.Common.csproj]
D:\Program Files (x86)\dotnet\sdk\1.0.1\Sdks\Microsoft.NET.Sdk\build\Microsoft.NET.Sdk.targets(92,5): error : Cannot find project info for 'D:\home\site\repository\MyProj\MyProj.Shared\MyProj.Common.csproj'. This can indicate a missing project reference. [D:\home\site\repository\MyProj\Web\MyProj.Api\MyProj.Api.csproj]
Failed exitCode=1, command=dotnet publish "MyProj\Web\MyProj.Api\MyProj.Api.csproj" --output "D:\local\Temp\8d493823074840c" --configuration Release
An error has occurred during web site deployment.
\r\nD:\Program Files (x86)\SiteExtensions\Kudu\62.60430.2807\bin\Scripts\starter.cmd "D:\home\site\deployments\tools\deploy.cmd"
I notice at no point does it attempt to restore the package for the Common assembly - how do I 'tell' Azure to do this? And is this even the issue?
Edit 1
Looking at the deployment script using Kudu, it seems to be doing a solution wide restore, so not sure how/why there is a missing assembly reference:
:: 1. Restore nuget packages
call :ExecuteCmd dotnet restore "MyProj\MyProj.sln"
IF !ERRORLEVEL! NEQ 0 goto error
Thanks
I notice at no point does it attempt to restore the package for the Common assembly - how do I 'tell' Azure to do this? And is this even the issue?
According to your description, your project is .net core project.
You mentioned Servicebus Libriary (WindowsAzure.ServiceBus 4.0.0) is not .NET Core compatible. And the new Libriary, that is a pre-release version, is 100% compatible. We can get more detail here.
We also can find it from another SO thread.
Edit:
According to your comment I update the answer:
I also try to create C# Web API project and deploy to Azure it works correctly on my side.
Please have a try to clean the build and rebuild the project again with a new WebApp.
The following is my detail steps:
1.Create a class libiary project named common and a public ServiceBusHelper class with function GetQueue
2.Create a WebApp project and reference Common.dll and Microsoft.ServiceBus.dll, then change the getvaules function to get queues
public List<QueueDescription> Get()
{
var queueList = Common.ServiceBusHelper.GetQueueList();
return queueList;
}
3.Remove the existed file from the Auzre WebApp during publish the WebApi to Azure
4.Test the WebApp

Build MonoDevelop on Debian/Jessie using Mono 4.3.3

I'am trying to build MonoDevelop(master branch) on my Debian/Jessie box.
So far I managed to get it compiled, but it shows me 5 errors and 1 warning.
Build FAILED.
Warnings:
/home/max/monodevelop/main/Main.sln (default targets) ->
(Build target) ->
/home/max/monodevelop/main/external/RefactoringEssentials/RefactoringEssentials/RefactoringEssentials.csproj (default targets) ->
/usr/lib/mono/4.5/Microsoft.Common.targets (GetReferenceAssemblyPaths target) ->
/usr/lib/mono/4.5/Microsoft.Common.targets: warning : Unable to find framework corresponding to the target framework moniker '.NETPortable,Version=v4.5,Profile=Profile7'. Framework assembly references will be resolved from the GAC, which might not be the intended behavior.
Errors:
/home/max/monodevelop/main/Main.sln (default targets) ->
(Build target) ->
/home/max/monodevelop/main/src/addins/NUnit/NUnitRunner/NUnitRunner.csproj (default targets) ->
/usr/lib/mono/4.5/Microsoft.CSharp.targets (CoreCompile target) ->
NUnitTestRunner.cs(105,44): error CS0103: The name `LoggingThreshold' does not exist in the current context
NUnitTestRunner.cs(105,14): error CS1501: No overload for method `Run' takes `4' arguments
NUnitTestRunner.cs(124,25): error CS0246: The type or namespace name `ParameterizedMethodSuite' could not be found. Are you missing an assembly reference?
NUnitTestRunner.cs(124,25): error CS0150: A constant value is expected
/home/max/monodevelop/main/Main.sln (default targets) ->
(Build target) ->
/home/max/monodevelop/main/external/RefactoringEssentials/RefactoringEssentials/RefactoringEssentials.csproj (default targets) ->
/usr/lib/mono/4.5/Microsoft.Common.targets (GetReferenceAssemblyPaths target) ->
/usr/lib/mono/4.5/Microsoft.Common.targets: error : PCL Reference Assemblies not installed.
1 Warning(s)
5 Error(s)
Time Elapsed 00:00:07.6677160
Makefile:1086: recipe for target 'sln_build' failed
make[2]: *** [sln_build] Error 1
make[2]: Leaving directory '/home/max/monodevelop/main'
Makefile:645: recipe for target 'all-recursive' failed
make[1]: *** [all-recursive] Error 1
make[1]: Leaving directory '/home/max/monodevelop/main'
Makefile:23: recipe for target 'all-recursive' failed
make: *** [all-recursive] Error 1
I suggest that it has something to do with nunit which shipps with mono and PCL Reference Assemblies which are not installed. I know MS released PCL Reference Assemblies as an installer, but I can't install them on my linux box. Because its an .NET needed msi installer or sort of.
The solution is to download
http://github.com/directhex/xamarin-referenceassemblies-pcl
using git. Adjust the control file under debian/ directory not depending on
mono-xbuild
Build the debian package using
debuild -i -us -uc -b
then installing the package using
dpkg -i <package>
What the installation does is to copy the PCL reference assemblies to
/usr/lib/mono/xbuild-frameworks/.NETPortable.
After this compiling MonoDevelop does not show error about missing PCL reference assemblies. Hope that helps anyone trying to build latest MonoDevelop.

How to make MSBUILD resolve System.Action<T1,T2> correctly?

My projects build fine in Visual Studio and also using MSBUILD on my machine.
On the build agent however if I run MSBUILD I get the error below.
I have already tried to specify a target framework of 4.0 but without success.
Any other ideas?
"c:\Test\Test.csproj" (default target) (6) ->
(CoreCompile target) ->
ItemSearchDelegate.cs(17,10): error CS0433: The type 'System.Action<T1,T2>' exists in both 'c:\Windows\Microsoft.NET\
Framework\v4.0.30319\mscorlib.dll' and 'c:\Windows\assembly\GAC_MSIL\System.Core\3.5.0.0__b77a5c561934e089\System.Core.
dll'
What I find most interesting: some lines above that error from MSBUILD, I see:
CSC : warning CS1685: The predefined type 'System.Action' is defined in multiple assemblies in the global alias; using definition from 'c:\Windows\Microsoft.NET\Framework\v4.0.30319\mscorlib.dll'
So it decides to use v4 just to complain a second later?
All I do is: open the command line at the location of the .CSPROJ file and run "msbuild". Nothing fancy.

Compiling C# code with Windows Forms in Linux

I downloaded a few source codes containing C# code that uses windows forms. Is it possible to compile the codes under the Linux environment (Ubuntu 12.04) and run it?
I already tried successfully to compile a few C# source codes with mcs and run the resulting exe file with mono.
The source codes can be downloaded here. If it's possible please describe how would you compile and run chapter 2/OpeningDocument solution.
$ mcs Program.cs
Program.cs(3,14): error CS0234: The type or namespace name `Windows' does not exist in the namespace `System'. Are you missing an assembly reference?
Compilation failed: 1 error(s), 0 warnings
EDIT
$ gmcs -pkg:dotnet Program.cs
Program.cs(17,33): error CS0246: The type or namespace name `Form1' could not be found. Are you missing a using directive or an assembly reference?
Program.cs(17,25): error CS1502: The best overloaded method match for `System.Windows.Forms.Application.Run(System.Windows.Forms.Form)' has some invalid arguments
/usr/lib/mono/gac/System.Windows.Forms/2.0.0.0__b77a5c561934e089/System.Windows.Forms.dll (Location of the symbol related to previous error)
Program.cs(17,25): error CS1503: Argument `#1' cannot convert `object' expression to type `System.Windows.Forms.Form'
Compilation failed: 3 error(s), 0 warnings
If you're not using the latest version of Mono, this should work:
gmcs -pkg:dotnet *.cs
The latest version of mono uses mcs instead of gmcs. In the previous ones, mcs targets .NET 1.1.
You may wish to run your code against the mono migration analyser to see what fails or isn't supported. You can find the tool here: http://www.mono-project.com/MoMA. NOTE For the tool to work you will need access to a windows machine.
You can also take a look at current mono compatibility with various .Net features at http://www.mono-project.com/Compatibility.
Specifically for windows forms see: http://www.mono-project.com/WinForms
Hope this helps.

Categories

Resources