DLL Pinvoke with Windows Mobile C++ libraries porting to C# - dll

I am trying to use OpenCV as here, and I am stuck at one moment where I get PInvoke, namely:
my DLL.cpp
#define DLL_API __declspec(dllexport)
//...
DLL_API short processImage(const char* in_file, const char * out_file)
//...
my form.cs
[DllImport("DLL", EntryPoint = "processImage")]
private static extern short _ProcessImage(byte[] in_file, byte[] out_file);
public static short binarizeImage(string in_file, string out_file)
{
return _ProcessImage(StringToASCIIByteArray(in_file), StringToASCIIByteArray(out_file));
}
public static byte[] StringToASCIIByteArray(string str)
{
return Encoding.ASCII.GetBytes(str + "\0");
}
I think that this might be a problem with target architecture (in my VS 2008 project). When I use 'Any CPU' it compiles & runs but throws Pinvoke, when I set it to 'Windows Mobile 6 Professional SDK (ARMV4I)' it compiles but doesn't want to deploy and I got this in output window:
1>------ Deploy started: Project: DLL, Configuration: Debug Windows Mobile 6 Professional SDK (ARMV4I) ------
1>The system cannot find the path specified.
1>
2>------ Deploy started: Project: smartDeviceOcr, Configuration: Debug Any CPU ------
2>Deploying 'D:\VS 2008 Projects\C++\SmartDevice\ocr\smartDeviceOcr\bin\Debug\smartDeviceOcr.exe'
========== Deploy: 1 succeeded, 1 failed, 0 skipped ==========
Does the specific ARMV4I matter ? I have ARM920T on my mobile. Can/should I edit this to make it work ?
EDIT:
Just to be clear the Pinvoke points to :
return _ProcessImage(StringToASCIIByteArray(in_file), StringToASCIIByteArray(out_file));
and the exception message is :
System.MissingMethodException was unhandled
Message="Cannot find the library DLL PInvoke 'DLL'."
StackTrace:
in smartDeviceOcr.Form1.binarizeImage(String in_file, String out_file)
in smartDeviceOcr.Form1.button1_Click(Object sender, EventArgs e)
in System.Windows.Forms.Control.OnClick(EventArgs e)
in System.Windows.Forms.Button.OnClick(EventArgs e)
in System.Windows.Forms.ButtonBase.WnProc(WM wm, Int32 wParam, Int32 lParam)
in System.Windows.Forms.Control._InternalWnProc(WM wm, Int32 wParam, Int32 lParam)
in Microsoft.AGL.Forms.EVL.EnterMainLoop(IntPtr hwnMain)
in System.Windows.Forms.Application.Run(Form fm)
in smartDeviceOcr.Program.Main()
EDIT2:
Ok now it's really strange. I have changed few things. I have added the openCV's dlls to the deployment list in the project properties so that I can see that they are being copied on deployment and I have copied manually all the dlls to exe directory on the PC.
I have also changed some of the deployment options in DLL project so that the dlls are copied to the proper directory (automatically) on the phone and ....
now I have the error on runtime (when trying to access the function from the dll - opencv) :
The remote connection to the device has been lost

As I don't see any answers I post my answer (although I still have problems with this)
The problem can be solved by adding directories to which dlls should be deployed to:
Project-> Properties -> Deployment -> Remote Directory
and properly specify files in:
Project-> Properties -> Deployment -> Additional Files
as like
highgui200.dll|$(SolutionDir)\opencv_winmo\dll|%CSIDL_PROGRAM_FILES%\smartDeviceOcr|0
cv200.dll|$(SolutionDir)\opencv_winmo\dll|%CSIDL_PROGRAM_FILES%\smartDeviceOcr|0
cxcore200.dll|$(SolutionDir)\opencv_winmo\dll|%CSIDL_PROGRAM_FILES%\smartDeviceOcr|0
ml200.dll|$(SolutionDir)\opencv_winmo\dll|%CSIDL_PROGRAM_FILES%\smartDeviceOcr|0
msvcr90.dll|$(BINDIR)\$(INSTRUCTIONSET)\|%CSIDL_PROGRAM_FILES%\smartDeviceOcr|0

Related

IIS ASP.NET 6 startup throws System.IO.DirectoryNotFoundException: D:\agent\_work\38\s\IdentityServer\wwwroot\

We are updating one of our applications, in this case IdentityServer, from .NET 5 to .NET 6. It is being hosted by IIS and deployed by Azure Devops Services. The issue we are seeing is that on our development environment the website fails to load but on our staging environment it runs just fine. The error we are seeing on development is
12:45:37.519|Fatal|1||Host terminated unexpectedly.||
System.IO.DirectoryNotFoundException: D:\agent\_work\38\s\IdentityServer\wwwroot\
at Microsoft.Extensions.FileProviders.PhysicalFileProvider..ctor(String root, ExclusionFilters filters)
at Microsoft.Extensions.FileProviders.PhysicalFileProvider..ctor(String root)
at Microsoft.AspNetCore.Hosting.StaticWebAssets.StaticWebAssetsLoader.<>c.<UseStaticWebAssetsCore>b__1_0(String contentRoot)
at Microsoft.AspNetCore.StaticWebAssets.ManifestStaticWebAssetFileProvider..ctor(StaticWebAssetManifest manifest, Func`2 fileProviderFactory)
at Microsoft.AspNetCore.Hosting.StaticWebAssets.StaticWebAssetsLoader.UseStaticWebAssetsCore(IWebHostEnvironment environment, Stream manifest)
at Microsoft.AspNetCore.Hosting.StaticWebAssets.StaticWebAssetsLoader.UseStaticWebAssets(IWebHostEnvironment environment, IConfiguration configuration)
at Microsoft.AspNetCore.WebHost.<>c.<ConfigureWebDefaults>b__9_0(WebHostBuilderContext ctx, IConfigurationBuilder cb)
at Microsoft.AspNetCore.Hosting.GenericWebHostBuilder.<>c__DisplayClass9_0.<ConfigureAppConfiguration>b__0(HostBuilderContext context, IConfigurationBuilder builder)
at Microsoft.Extensions.Hosting.HostBuilder.BuildAppConfiguration()
at Microsoft.Extensions.Hosting.HostBuilder.Build()
at IdentityServer.Program.Main(String[] args) in D:\agent\_work\38\s\IdentityServer\Program.cs:line 23
The path it reports, D:\agent\_work\38\s\IdentityServer\wwwroot\ is interesting because that path is the same as the path from the DevOps build machine. We don't see this error if we revert back to .NET 5 and we don't see the problem on our staging machine.
The Program.cs class is defined as
using System;
using Microsoft.AspNetCore;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Hosting;
using NewRelic.LogEnrichers.Serilog;
using Serilog;
using Serilog.Events;
namespace IdentityServer
{
public class Program
{
public static int Main(string[] args)
{
try
{
CreateLogger();
Log.Information("Starting host...");
CreateHostBuilder(args).Build().Run();
return 0;
}
catch (Exception ex)
{
Log.Fatal(ex, "Host terminated unexpectedly.");
return 1;
}
finally
{
Log.CloseAndFlush();
}
}
public static void CreateLogger()
{
var configuration = GetConfiguration();
Log.Logger = new LoggerConfiguration()
.ReadFrom.Configuration(configuration)
.Enrich.FromLogContext() // allows logging middleware to inject output values
.Enrich.WithThreadId()
.Enrich.WithNewRelicLogsInContext()
.CreateLogger();
}
public static IHostBuilder CreateHostBuilder(string[] args)
{
var configuration = GetConfiguration();
return Host.CreateDefaultBuilder(args)
.ConfigureWebHostDefaults(
webBuilder =>
{
webBuilder.UseConfiguration(configuration);
webBuilder.UseSerilog();
webBuilder.UseIIS();
webBuilder.CaptureStartupErrors(true);
webBuilder.UseStartup<Startup>();
});
}
private static IConfiguration GetConfiguration()
{
var environment = Environment.GetEnvironmentVariable("ASPNETCORE_ENVIRONMENT");
var builder = new ConfigurationBuilder()
.AddJsonFile("appsettings.json")
.AddJsonFile($"appsettings.{environment}.json", true, true);
var configuration = builder.Build();
return configuration;
}
}
}
We do have other .NET 6 web applications running just fine on this instance of IIS. I was thinking that the problem might be in our release pipelines but they are identical in their task configurations between the environments. Tried looking for the directory path in the code or configuration but don't see it anywhere. Have tried manually setting the WebRoot and ContentRoot paths via .UseWebRoot("path to folder") and .UseContentRoot("path to folder") in the Program.cs but didn't see any change in the logs or the app starting.
Even updated the web.config file to have the exact path for executing the project dll in the aspNetCore element but still no change.
Update 10 Feb 2022
Added debug output to the startup to verify file and folder paths. Everything in the environment variables and execution file path look correct.
ASPNETCORE_IIS_PHYSICAL_PATH - C:\inetpub\webapps\IdentityServer\
Executable Path: C:\inetpub\webapps\IdentityServer\IdentityServer.dll
The problem ended up being how we were pushing our updates out to the servers from DevOps. Our pipelines were built to copy over files out of the Release directory of the build folder. One of the problems with this approach is that files not needed for a site to run but generated during a build are also copied to the release server. In this case, a new file which is generated in .NET 6, .staticwebassets.runtime.json, was getting copied to our servers.
The way .NET 6 seems to behave is that if the environment is set to Development then it will look for this file to figure out where the static web assets are located. If the file doesn't exist then it will assume the files are in a wwwroot sub-directory of the site. This makes sense for instances where you are running the project from your local Visual Studio. More details about this file are available in another SO post with links to the source code in GitHub. To fix our problem we changed our release pipeline to use the publish.zip file that is generated when you run the publish command on a solution. The archive only contains the files needed to run the site, so none of the extraneous files like .staticwebassets.runtime.json are included. We should have been doing this the whole time... lesson learned.
We now unzip the publish.zip file, apply any file transformations, then copy the unzipped files to the web server.

Problem with 32-bit COM component from 64-bit application. (TYPE_E_CANTLOADLIBRARY)

I have a created a dummy application for testing purposes which is a 64-bit console application, which creates an instance of a 32-bit COM application. It is a third party application called Laserforms.
static void Main(string[] args)
{
LFM32.Application application;
try
{
application = new LFM32.Application();
Console.WriteLine("Success!");
application.Quit();
}
catch (Exception ex)
{
Console.WriteLine(ex.ToString());
}
finally
{
Console.Read();
}
}
Now, this works perfectly on my machine, but when I deploy it to another machine, which has Laserform installed as well I am getting the below error message.
{System.InvalidCastException: Unable to cast COM object of type 'System.__ComObject' to interface type 'LFM32.Application'.
This operation failed because the QueryInterface call on the COM component for the interface with IID '{6688DD46-3DA3-4FEC-8A1F-54E1453DCC77}'
failed due to the following error: Error loading type library/DLL. (Exception from HRESULT: 0x80029C4A (TYPE_E_CANTLOADLIBRARY)).}
I have go one through the registry of both and can't see any differences, what else could I be doing wrong?
UPDATE:
So I ran TlbImp on the .tlb file to create a DLL I could reference in my .NET application. This created a couple of extra DLLs and some warnings around Iunknown and some invalid types. I then ran Regasm (from Framework and Framework64) on this and the other DLLs it created but I am still seeing the exact same error.

"mono app.exe" gives error "File does not contain a valid CIL image."

I've made the following as a simple test.
using System;
namespace test
{
class App
{
static void Main(string[] args)
{
Console.WriteLine("test");
Console.ReadLine();
}
}
}
Then compiled and moved the entire bin folder to my pi and tried to run it using "mono app.exe" but I get the error in the title. How do I run compiled .Net core executables using mono on raspberry pi?
Realized that mono is .NET framework.
I installed .NET core runtime and it worked.
I followed this guide

New (.NET Core) C++/CLI project has compile issues when definition and implementation are split

I created a "CLR Empty Project (.Net Core)" project using Visual Studio 2019.
I simply created a new class using the 'add class' menu option and it generated this class.
I added the function called Test in the header:
using namespace System;
public ref class Class1 {
// TODO: Add your methods for this class here.
void Test();
};
Then using visual studio's generate implementation option it created this function defenition in the .cpp file:
#include "EngineEditorLayer.h"
#include "pch.h"
void Class1::Test() { throw gcnew System::NotImplementedException(); }
When compiling it gave me this error :
error C2653: 'Class1': is not a class or namespace name
I can only resolve this error by moving the implementation to the header file.
Am I missing something? Is there a setting I have to change to enable .cpp compilation?
Is there a compiler bug that prevents me from doing this currently?
Hans Passant from the comments was right. The pch.h has to be included first.
I unfortunately never got the warning. This was my output log:
1>------ Build started: Project: EngineEditorLayer, Configuration: Debug x64 ------
1>EngineEditorLayer.cpp
1>E:\Other Projects\PixEngine\EngineEditorLayer\EngineEditorLayer.cpp(7,18): error C2653: 'Class1': is not a class or namespace name
1>Done building project "EngineEditorLayer.vcxproj" -- FAILED.

What does compilationOptions.emitEntryPoint mean?

Just installed the rc1 tools and created a new web project to see what has changed in the template.
I noticed that project.json now contains:
"compilationOptions": {
"emitEntryPoint": true
}
But it's unclear what this does.
Does anyone have an idea?
As mentioned below: It looks like it is a flag to the compiler to indicate that the project is a console application vs. a library (namely: a console application must contain public static void Main())
You can see from the source here.
In the new RC1 default web application template, you'll notice at the bottom of Startup.cs there is a new expression bodied method that acts as the entry point:
public static void Main(string[] args) => WebApplication.Run<Startup>(args);
If you remove this method then perform a build (dnu build) you will get an error:
error CS5001: Program does not contain a static 'Main' method suitable for an entry point
However, if you change the emitEntryPoint flag to false and attempt to build again, it will succeed. This is because it is creating a library instead of a console app.
I see this in the source;
var outputKind = compilerOptions.EmitEntryPoint.GetValueOrDefault() ?
OutputKind.ConsoleApplication : OutputKind.DynamicallyLinkedLibrary;
Looks like it tells the compiler whether to create a Console Application or a Library.
Additionaly, if you create a new Class Library (Package) and Console Application (Package) in VS2015 you'll see that project.json for the Console Application includes the following, while the Class Library does not;
"compilationOptions": {
"emitEntryPoint": true
}