Using PInvoke to load a dll in Azure Service Fabric - azure

So in my application, I have an actor that uses Pinvoke to call some functions defined in a win32 dll. I also have a front end webservice that calls these actors.
When I run this locally, everything works perfectly. The win32 dll is found and loaded correctly and my actors can call functions from there.
However, when I deploy my solution to Azure, I just get an internal server error when the actor tries to load the dll. Theres no descriptive error so I think that the actor either can't find the dll or can't find one of its dependencies.
I already tried using dependency walker to figure out the dependencies. Most were typical windows binaries with the exception of vcruntime140.dll. I added this to the VS project as a resource file by right clicking on the Project and then navigating to Add>Existing Item and then editing the "Copy to Output Directory" property of the resource in the solution explorer to "Copy always". Is this the right way deploying a native dll to service fabric? I'm also running in release mode so there shouldn't be any debug dll's needed.
Some extra information - My project was initially ported over from a web api project and I was able to successfully call functions in the win32 dll when hosting my web api on Azure in release mode.
Let me know if I need to provide some extra information

So I figured it out. For people who may stumble upon this later, here is how:
When I used dependency walker, I saw that I had immediate dependencies on vcruntime140.dll and msvcp140.dll. However, by digging deeper, I noticed that msvcp140.dll has an additional dependency on concrt140.dll. When I copied this over to my server's bin folder, I didn't get an internal server error anymore and I was able to call into my win32 dll.
All of these are visual studio c++ redistributable binaries that are usually found here "C:\Program Files (x86)\Microsoft Visual Studio 14.0\VC\redist\x64\Microsoft.VC140.CRT".

Related

References from Precompiled DLL

I've successfully built a Precompiled Function (DLL) and but ran into some issues regarding Reference from my DLL to other .Net Framework libraries.
I first tried to promote only my DLL to Azure and that failed due to missing dependancies. Next I tried to use Reflector to list the actual dependancies of my DLL and include them with Nuget but ended up just copying every DLL from my project bin directory to Azure and it worked.
So what how do I tell what libraries are actually included by the Azure Function environment and which I need to Nuget or upload myself. I couldn't find any documentation on the subject.
With the pre-compiled model, you need to bring your dependencies with the assembly where your function is defined (much like deploying a console or standard Web Application).
The simplest approach is to deploy the files from your assembly's output folder. The Azure Functions Tools for Visual Studio 2017 will do exactly that for you, giving you the ability to publish directly from VS.

How to create a x64 (instead of AnyCPU) build of .Net based Azure Cloud Service?

I'm using Azure 2.7.
I created a C# cloud service that only contains a single worker role. After it's done, the solution contains
A ccproj
A csproj
Both are using the setting of AnyCPU.
I then added the PropertyGroup "Debug|x64" and "Release|x64" into both projects. Also changed the default to x64 for both. As a result, an x64 build.
However, whenever I select "publish" on the "ccproj", I notice that it always builds AnyCPU instead of x64. I cannot find how to force it to build x64 for packaging and deployment.
I then edited both proj file again, and removed the PropertyGroup for "Debug|AnyCPU" and "Release|AnyCPU". After I did this, and when I tried to build x64 again, I got error as:
C:\Program Files
(x86)\MSBuild\14.0\bin\Microsoft.Common.CurrentVersion.targets(723,5):
error : The OutputPath property is not set for project
'AzureWorkerRoleExample.ccproj'. Please check to make sure that you
have specified a valid combination of Configuration and Platform for
this project. Configuration='Release' Platform='AnyCPU'. This error
may also appear if some other project is trying to follow a
project-to-project reference to this project, this project has been
unloaded or is not included in the solution, and the referencing
project does not build using the same or an equivalent Configuration
or Platform.
I could not figure out how to work around this error.
Any idea on how to force VS to make a x64 build of cloud service and deploy it?
One workaround I found is that: keep AnyCPU property groups, but also put
<PlatformTarget>x64</PlatformTarget>
in the group. Thus the AnyCPU assembly are actually targetting to x64. But this sounds hacky.
Any idea?
Thanks a lot!

Loading web role in Azure emulator results in a BadImageFormatException

I have an ASP.NET MVC project that loads fine when I start it directly. When I try to run it vai the Azure SDK's emulator, I get the following exception:
Could not load file or assembly 'file:///C:\Windows\Microsoft.NET\Framework64\v4.0.30319\Temporary ASP.NET Files\root\62253e0e\13ba4ff3\App_global.asax.a-wnopwm.dll' or one of its dependencies. An attempt was made to load a program with an incorrect format.
This usually indicates something like a reference to a 64bit assembly on a 32bit system, but I've never seen this error on a ASP.NET project dll. The project configuration is set to AnyCPU. What gives?
The problem was due to the assemblyPostProcessorType attribute added to the system.web/compilation element of the web.config file when I used VS 2012's performance analysis (profiling) tool. It doesn't appear to clean up after itself very well and the config was left in place, causing an apparently assembly platform mismatch. It also caused problems when publishing to Windows Azure because the post-build assembly wasn't available.
The solution was to add a transform to the web config transform to remove the attribute (assemblyPostProcessorType).

Visual C++ ATL Com registration issue

I have a Visual C++ 6 project that is creating a COM DLL. This is an old project that I have not used in years but I came back to it recently to update some functionality of the DLL. The issue I am having is that when I try to manually register the DLL (using regsvr32) that is created by the project, I get no response whatsoever. It does not say it succeeds and it does not say it fails. I have verified that the DLL is not being added to the registry. I have never seen a situation with regsvr32 when it would not show any confirmation message.
Also, the DLL is set to self-register in the project and when it attempts to do this, it also does not show a message.
I have not touched this project in a long time, but the last time I did touch it, it worked without issue. Now I come back to it, and all of the sudden I am having this problem. It is as if the DLL being created by the Visual C++ project is not compatible with the current version of Windows. I have updated the development software to SP6 but it has not changed the situation.
I ran Depends on regsvr32 when it was pointing to the created DLL, and it shows this message:
LoadLibraryExW("C:\Source32\BIS\Projects\ALFA\DigiPixSvc\DigiPixObj\Debug\DigiPixObj", 0x00000000, LOAD_WITH_ALTERED_SEARCH_PATH) returned NULL. Error: The operating system cannot run %1 (182).
Can anyone please tell me why this is happening and what I can do to correct it?
Disclaimer: since I don't have VS 6.0 installed anymore, I'll have to use some Google searches to give you the exact steps for the VC 6.0 dialogs - so some of this information may be inaccurate. I believe it's correct - it's really what you'd do in the current IDE, just for VC 6.0.
Before you go through the trouble of debugging your DLL, make sure you run it through Dependency Walker to verify that all your dependencies are on your machine. Don't run Dependency Walker on regsvr32 - what matters is whether your DLL has its dependencies. If they're missing, regsvr32 won't be able to load the DLL.
To see if your DLL is getting loaded by regsvr32 when you try to register it, you need to debug through the DLLRegisterServer() function and see what happens there - this is one of the 4 entry points a COM DLL must have and regsvr32 calls this function when the DLL is about to be registered. In order to do this, you'll have to set regsvr32 as the startup program of your project and pass the full path to your debug-built DLL as a command-line parameter to regsvr32.
Bring up Project Properties for your project.
Go to the Debug tab.
Make sure the General category is selected.
Enter the full path to regsvr32 in the Executable for debug session textbox.
Enter the full path to the debug version of your DLL in the Program arguments textbox. Make sure you have a PDB file for your DLL so you can see symbol information during debugging.
Put a breakpoint on the first line in DLLRegisterServer(). The code in this function may be long or short, depending on how it was created: generated by a wizard, written by a developer, etc.
Start debugging.
Your breakpoint should be hit and you should be able to step through the registration code and see at which point it fails.

How to find the dependent dlls to run an EXE file created by a VC++ project

In my vc++ project I am using boost , OpenCv and JRTPLIB libraries , I have created an exe file and when i try to run it on another PC(the pc has no vc++ or the libraries mentioned) I get the error message...
"the application has failed to start because its side by side configuration is incorrect .please see the application event log or use the command line sxstrace.exe tool for details"
I am new to creating exe files and could you please help me in understanding the error. will there be dependencies which I need to copy with exe file?
For running sxstrace.exe, go to Visual Studio command prompt and type sxstrace.exe.
Usage is as follows:
Before running your application, run sxstrace in trace mode:
sxstrace.exe Trace -logfile:C:\MySxSTrace.log
Reproduce the error by starting your application
Now stop the trace by using the below command
sxstrace.exe Parse -logfile:C:\MySxSTrace.log -outfile:C:\MySxSTrace.txt
Open output file from C:\MySxSTrace.txt
What is Side by Side Configuration?
A side-by-side assembly contains a collection of resources—a group of DLLs, Windows classes, COM servers, type libraries, or interfaces—that are always provided to applications together. These are described in the assembly manifest.
Why is it Important?
In many cases, it is possible to update existing applications to use side-by-side assemblies without having to change the application code. Developers are encouraged to use side-by-side assemblies to create isolated applications, and to update existing applications into isolated applications for the following reasons:
Side-by-side assemblies reduce the possibility of DLL version conflicts.
Side-by-side assembly sharing enables multiple versions of COM or Windows assemblies to run at the same time.
Applications and administrators can update assembly configuration on either a global or per-application configuration basis after deployment. For example, an application can be updated to use a side-by-side assembly that includes an update without having to reinstall the application.
for Side by Side Configuration Incorrect,read this Article...........
.

Resources