C# getting version of unmanaged dll - c#

I'm calling an unmanaged dll from my managed c# code and wanted to check I'm calling the right version.
The code I'm trying to load the assembly (to then get the resource file and then get the version) is:
cur_version = Assembly.LoadFile("X:\Workspace\yreceipts_pos\yRprintProcessor\Debug\yRprintProcessor.dll");
It's failing because of this error:
The module was expected to contain an assembly manifest. (Exception from HRESULT: 0x80131018)
Does anyone know how to get around this or have a better way to check the version of an unmanaged dll from managed c# code?
Thanks in advance,
Richard

As stated by logicnp; the Assembly.Load is for managed assemblies only. To determine the version of any version-ed file you can use System.Diagnostics.FileVersionInfo.GetVersionInfo(filename) and to load and call unmanaged procedures in DLLs you can refer to these articles:
http://blogs.msdn.com/jonathanswift/archive/2006/10/02/780637.aspx
http://blogs.msdn.com/jonathanswift/archive/2006/10/03/Dynamically-calling-an-unmanaged-dll-from-.NET-_2800_C_23002900_.aspx
Good luck...

The reason it fails is becuase you cannot use Assembly.Load to load unmanaged dlls. See the link suggested by David Brown.

Related

dll File not accessible

I know this question has been asked before but all the steps advised doesn't seem to work for me.
I am trying to use WebCamLib.dll to my project but it keeps giving me this error:
please make sure the file is accessible and that is a valid assembly
or com component
I tried to register the dll to SYSTEM32 and I get this error
The module C:\WebCamLib.dll" was loaded but the entry-point
DllRegisterServer was not found.
Make sure that "C:\WebCamLib.dll" is a valid DLL or OCX file and then
try again.
Please suggest a way. Thank you in advance.
The library you are trying to use is a managed library, you don't need to register it or use dllimport. Just add a reference from inside your project to the dll.
First option is DLL file may be corrupt. Make sure it is working with other applications.
My second opinion is you are trying to use this DLL as COM object which is not.

Unable to load DLL 'Xeneth.dll'

I get following error message:
Unable to load DLL 'Xeneth.dll': The specified module could not be found. (Exception from HRESULT: 0x8007007E)
Although the Xeneth.dll has been added under the references I get this error message on this codeline:
XCHANDLE = (uint)DllImports.XC_OpenCamera(CameraName, StatusCallback, IntPtr.Zero);
thanks in advance for the help..!
That error code is a COM error wrapping the Win32 error code ERROR_MOD_NOT_FOUND. This means that either Xeneth.dll or one of its dependencies cannot be found on the DLL search path.
You should consult the documentation for this library to work out where it must be deployed. Usually, and most sensibly, unmanaged DLLs should be placed in the same directory as the executable file. Doing so ensures that they are located.
If doing that does not help, then you may need to deal with missing dependencies. Again check the documentation. Do you need to install an MSVC runtime on which this DLL depends?
If all this fails then you might try using a tool like Dependency Walker, and use its profiling mode to try to work out what is missing. However, I do suggest that you start with the library documentation first. It's always best to follow and understand the instructions.

A procedure imported by {dll} could not be loaded

I have several Unmanaged C++ written lib files which I need to link to Managed C++ dll.
Then I need to invoke functions of this Managed C++ from C# application.
First step is OK - Managed C++ dll is created, I can see with ildasm that it exports functions I need. However when I try to call this function from my C#-written test app it says:
An unhandled exception of type 'System.IO.FileLoadException' occurred in Unknown Module.
A procedure imported by {MyManagedCPP.dll} could not be loaded.
This message goes from VS2010.
I made simple experiment - removed dependencies from all lib files in Managed C++ dll and rebuild it.
With this change it is OK - app starts, I can call functions of Managed C++ dll from C# test app.
Is it not possible by design to call managed c++ functions when dll has static linkage with lib files? Technical restriction? Or there is some workaround?
Thanks
You no doubt have an implicit dependency on a native DLL. It isn't clear from the question what DLL that might be. It could be msvcrxx.dll for example, a runtime support library for native C++ code. Which would be rather bad, you don't want to mix CRT versions. Such a missing DLL otherwise prevents the C++/CLI assembly from getting loaded, producing the FileLoadException.
If you have no idea what that DLL might be then you could use SysInternals' ProcMon utility. The trace will show you the program searching for the DLL and not finding it. If it is msvcrxx.dll then be sure to rebuild the .lib files using the same compiler version you used to build the C++/CLI assembly. If it is something else then make sure you copy that DLL to the build directory.

C#: Adding DLL Reference

Preface: I know basically nothing about C#.
I've added a dll to my project. I have no build errors, but when I try to run, I get an error that says it can't find the dll. I've tried copying it to the output directories too. To no avail.
Any idea what could be happening?
Specific Error:
System.IO.FileNotFoundException was unhandled Message=Could not load
file or assembly 'controllib_clr.dll' or one of its dependencies. The
specified module could not be found. Source=controllib_demo_cs...
I'll be happy to add more information if need be. :) I just don't know what info would be beneficial given my (very) limited knowledge.
It looks like it is not able to find/load some dependencies dlls I will use DependencyWalker to figure out what it is missing http://www.dependencywalker.com/
Is it a managed (.NET) or an unmanaged (native) DLL? I'm assuming unmanaged.
FileNotFoundException is commonly thrown when missing a dependency. The DLL you are loading might require any number of other DLLs on load. In most cases it won't tell you which file it needs. You have to refer to the documentation for that DLL.
If it exists, in your output folder, then most likely its one of its dependencies that is missing. You can fix this by adding its dependencies as references in your project.
if it doesn't exist in your output, then, check that "Copy Local" is set in the properties of the reference.

.NET does not search in PATH for my C# DLL

I have a C# dll say dll1 that is referenced by another C# dll say dll2 . The path to dll1 is specified in the env variable PATH. when I try to compile dll2 , the dll2 reports error that it could not find the assembly dll1. I am not sure how I can make this happen I thought PATH was used in Dll search by .Net.
Awaiting some helpful advice
Thanks
Karandeep Malik
The PATH is not searched at all. This article on msdn explains how the runtime locates assemblies.
In short: No the .Net framework doesn't use the PATH environment variable to locate assemblies.
In more detail: The .NET assembly lookup rules are arcane to say the least - you'd be better off reading the Microsoft documentation.
Here's the link: http://msdn.microsoft.com/en-us/library/yx7xezcf.aspx

Categories

Resources