c# Web Service referencing c# project referencing DLL - c#

Alright, so maybe this is inadvisable, but I'm writing a Web Service for an existing application. I'm trying to use the C# Library we wrote for the project, and that C# Library uses functions from a dll that is not acreated from a .NET project. I know the DLL isn't a problem, because the actual application (A windows form application) that uses the c# Library has no problem performing operations that use this dll, but my web service always crashes when the dll methods are called. The exception is:
"Unable to load DLL 'foo.dll': The specified module could not be found. (Exception from HRESULT: 0x8007007E)"} System.Exception {System.DllNotFoundException}
A sample of the import from the c# Library:
[DllImport("foo.dll")]
protected static extern void DllFunction(string param1, int param2, int param3);
I tried adding foo.dll to the Bin directory of my project, but this didn't work. I feel like I must be missing something really obvious. Any suggestions? I've read some funky things about DLLs and web services and tried a couple of fixes such as http://sites.google.com/site/tosapage/programming/asp-net#dll but they didn't work.

I would run the winforms exe with depends.exe. You might be getting that error for a dell that the dll you are calling needs.

Related

Calling C++ DLLs from ASP.NET 4

I'm trying to call a C++ DLL from ASP.NET 4.0 web application but keep getting a DLL Import error - unable to load library 'Library name'.
I've read a lot of blogs where developers experience the same probem and what seems to be working for most people is adding the path to where the unmanaged DLLs are to the PATH Environment Variable
I've done the same on the server and double, tripple checked that I spalled the path correctly and that the C++ DLLs are in that folder but when I view the application in the browser I'm still getting the same error.I've placed logging in the code and it's definitely coming from the portion of code which calls the unmanaged DLL.
Here is some information on my environment:
Windows Server 2003
IIS 6.0
.NET Framework 4.0
ASP.NET MVC 4.0
C#
On the dev machine it works if I copy the unmamanaged DLLs to C:\Program Files\IIS Express
Tried copying the C++ DLLs to the bin folder but as far as I understand ASP.NET copies the DLLs from the bin folder in inetpub\wwwroot\sitename\bin to a dynamic location when it executes the code but this process excludes the unmanaged DLLs.Any advice on what else I can try?
EDIT:
In terms of the code it's exactly the same as any other interop code,I have a wrapper class where I use DllImport to define all th required function calls and in my code I create a new instance of the wrapper and call the required function.As I mentioned this works fine on the dev machine as long as the C++ DLLs are i the IISExpress folder but when I deploy to the server it throws the above mentioned error:
[DllImport("My3rdparty.dll", CallingConvention = CallingConvention.Cdecl)]
private static extern int myfunction(int handle, int* response, int timer);
So after weeks of struggling I came up with a work around.The integration that I was implementing was for credit card processing. Along with the C++ DLLs they also have a compiled configuration file called system.dna and my suspicion is that when you add the folder containing the interop DLLs to the PATH Environment Variables it does not pick up the system.dna file.
As a workaround I created a WCF service which is installed as a Windows Service on the server and I've placed the logic for doing payments into this service and the web application simply talks to the service.I hope this helps other developers who are facing the same issue

DllNotFoundException From C# but not VB.NET

I have a third party DLL to integrate with their application. It is a 32 bit C++ DLL that does not expose itself via COM, and I have not been supplied source code or a header file.
It was supplied with working VB.NET example code in a WinForms example. This works, as long as the executable is run from the same directory as the API DLL and the application for which the API is interfacing.
I was using the example function declaration:
Declare Ansi Function GetVersion Lib "ThirdPartyAPI.dll" () As Integer
However, we’ll be using the API calls from a C# library to be used in a web site or web service, so I converted the example VB code to
[DllImport("ThirdPartyAPI.dll", SetLastError = true, CharSet = CharSet.Ansi)]
public static extern int GetVersion();
If I call this from within the C# library I get the error
Unhandled Exception: System.DllNotFoundException: Unable to load DLL
'ThirdPartyAPI.dll': A dynamic link library (DLL) initialization
routine failed. (Exception from HRESULT: 0x8007045A)
I took an alternative route and put the original working VB code from the example into its own VB.NET DLL and called that via C#. I got the same error. Strangely, a call to the VB.NET DLL from a VB.NET console app works.
In summary:
A simple VB.NET console app will work
A simple VB.NET WinForms app will work
A simple VB.NET console app can call GetVersion via a VB.NET DLL
A simple C# console app cannot call GetVersion via a VB.NET DLL
A simple C# console app cannot call GetVersion directly.
This could be to do with incorrectly defining the function calls. At
this stage, I’m not too worried about this.
32 bit compilation does not fix the issue
I have tried this on Server 2008 R2 and Windows 7 Enterprise
I created a WCF host via an NT service to expose the functions, hoping this would work around file locations and dependencies. This was written in VB.NET and placed in the same folder as ThirdPartyApi.dll.
None of the code in the NT service could access GetVersion
None of the code in the WCF service could access GetVersion
Please note, in all of the above cases, the executables are in the same directory as ThirdPartyApi.dll. All of its dependencies also exist in this directory.
Can anyone explain this behaviour, or suggest how this can be made to work within a C# web site?
Thanks
The error code is 0x8007045A. That is a COM error code that maps to the Win32 error code ERROR_DLL_INIT_FAILED. Which is described as:
A dynamic link library (DLL) initialization routine failed.
What this means is that your DLL was found, in spite of the perhaps misleading exception class DllNotFoundException. However, the DLL failed to load. You can gain no more information from the system. There is no mechanism for the DLL to report reasons why it failed to load. I suggest that you contact the vendor of the DLL and ask for support.

System.DllNotFoundException in .NET DLL

I took a program written in C/C++ and modified it's main function to accept some arguments as input and return a variable as output and created a Win32 DLL out of it. I then created a .NET DLL which uses InterOp to access the first DLL. Now when I load the .NET DLL in my C# app I get a System.DllNotFoundException from the DLL which is really baffling me as there were never memory issues with the program and both Win32/.NET dlls are located in the same directory (apart from modifying the main function, the code has not really changed).
The solution was provided in this thread, which was my original question some time ago. I'm pretty sure that answer is correct but I'm just missing something.
You can download my VS solution Here. The solution contains three projects: the Win32 DLL, the .NET DLL, and a winform app that references the .NET DLL (but when trying to test gives the DLL exception). Any help or debugging guidance would be greatly appreciated.
UPDATE: I have tried all the tips/suggestions below but I still get the exact same error. If it makes things easier, my VS solution is available to download in the hyperlink above.
Make sure you have placed the win32 dll on /windows/system32 folder(if only the dll name is passed to DllImport)
Alternatively you can also pass the full path of the dll to the DllImport Attribute.
Use a tool such as Dependency Walker to make sure you are not missing out on any dependent assembly.

Error: Unable to load DLL '.dll' : The specified module could not be found. (Exception from HRESULT: 0x8007007E) - What is this error?

The Situation:
I'm trying to run CharLS JPEG-LS Compression algorithm implementation
There is source code in the site about where you can download a C# VS 2010 Solution. In the Solution there are, 2 C# Class Library Projects, 1 Unmanaged VC++ project and 1 C# Console App. The console App calls one of the class libraries, `using CharLS;', and that particular library references the unmanaged VC++ project. The console application works perfectly on debug mode.
The Problem:
But when I compiled the console app for "Release", and ran the .exe (executable file), when a particular function is called during the process, it gives me this error:
Error: Unable to load DLL 'CharLS.dll' : The specified module could
not be found. (Exception from HRESULT: 0x8007007E)
I've also tried to create another project and reference the class library from there, but I get the same error.
I've tried reading other solutions for this over the net but they have not helped me.
Some of the reasons for this error could be:
the name you are providing to the "EntryPoint" property of "DllImport" attribute is not matching with the function you are exporting from the native component.
Another reason could be missing dll itself/or any of its dependencies(use dependency walker and check)
Third reason could be it could be debug/release version mismatch.
Have you (re-)built the project before run in Release mode? Looks like you didn't and release output contains no such lib. Check it anyway.
Ensure it's definitely registered on the server. Also have you got the Application Pool (assuming IIS7+) set to allow 32-bit processes?
If you have the dll just add it manually to the build output folder (bin).

Calling C# from ColdFusion

I've written a .dll in C# to change the permissions on a folder. I also wrote an .exe to test the .dll and it successfully changes the permissions. Now I'm trying to call the .dll from ColdFusion, but I'm getting an error about System/Security/IPermission not being found.
I'm assuming this is an interface in C# that ColdFusion can't find in any of the available assemblies on my system. I've added the System.Security assembly to my References in the C# project. Is there something else I need to do to make sure ColdFusion can find the interface?
Here's how I'm using the .dll:
<cfobject type="dotnet" name="permObj" assembly="#pathToDLLs#CoursePortal.dll" class="CoursePortal.Permissions">
<cfset permObj.revokePermissions(dir, username)>
I never could get it to work. I switched the DLL to an EXE and used <cfexecute> to call it. It's working fine now. The .NET code is called so infrequently it doesn't make much difference that it's a separate app.

Categories