OpenZWave / openzwave-dotnet-uwp

An Open-ZWave wrapper for use with .NET or UWP apps
Apache License 2.0
44 stars 27 forks source link

Exception "Could not load file or assembly 'OpenZWave.dll' or one of its dependencies" when running my app on another computer #14

Closed yesyesuk closed 6 years ago

yesyesuk commented 7 years ago

I have an issue here that I could use a bit of help with. I'm using this wrapper in my home automation app to support Z-Wave devices in addition to my own hardware.

It all works fine on the computer I'm developing on. But now I want to use that app on my home automation server but I get an exception saying that OpenZWave.dll could not be loaded.

The part of my app that is using OpenZWave.dll is itself a DLL file. Not sure that is relevant because it works fine on my development machine.

This is the exception I get (from my app log)

2017/06/24 11:56:56.574 L0  PortAdaptor "Z-Wave" : Trying to load port DLL "HA.Port.ZWave.dll" with Init String ""
2017/06/24 11:56:56.574 L0  PortAdaptor "Z-Wave" : Failed to load the assembly. Exception: Exception has been thrown by the target of an invocation.
2017/06/24 11:56:56.574 L0  PortAdaptor "Z-Wave" : Failed to load the assembly. Exception:    at System.RuntimeTypeHandle.CreateInstance(RuntimeType type, Boolean publicOnly, Boolean noCheck, Boolean& canBeCached, RuntimeMethodHandleInternal& ctor, Boolean& bNeedSecurityCheck)
   at System.RuntimeType.CreateInstanceSlow(Boolean publicOnly, Boolean skipCheckThis, Boolean fillCache, StackCrawlMark& stackMark)
   at System.RuntimeType.CreateInstanceDefaultCtor(Boolean publicOnly, Boolean skipCheckThis, Boolean fillCache, StackCrawlMark& stackMark)
   at System.Activator.CreateInstance(Type type, Boolean nonPublic)
   at System.RuntimeType.CreateInstanceImpl(BindingFlags bindingAttr, Binder binder, Object[] args, CultureInfo culture, Object[] activationAttributes, StackCrawlMark& stackMark)
   at System.Activator.CreateInstance(Type type, BindingFlags bindingAttr, Binder binder, Object[] args, CultureInfo culture, Object[] activationAttributes)
   at System.Reflection.Assembly.CreateInstance(String typeName, Boolean ignoreCase, BindingFlags bindingAttr, Binder binder, Object[] args, CultureInfo culture, Object[] activationAttributes)
   at System.Reflection.Assembly.CreateInstance(String typeName, Boolean ignoreCase)
   at HomeAutomation.Common.PortAdaptor.LoadPort()
2017/06/24 11:56:56.574 L0  PortAdaptor "Z-Wave" : Failed to load the assembly. InnerException: Could not load file or assembly 'OpenZWave.dll' or one of its dependencies. The specified module could not be found.
2017/06/24 11:56:56.574 L0  PortAdaptor "Z-Wave" : Failed to load the assembly. InnerException:    at HomeAutomationPort.ZWavePort..ctor()
2017/06/24 11:56:56.574 L3  BObject "Z-Wave" Status changed to: InvalidParameters

Both machines are running Windows 7 x64. I use Visual Studio 2015 Community. The target framework in the app and all its DLLs is set to .NET 4.5.2 and I compile for x86. I installed this wrapper using Nuget. Before I started using ZWave the app was .NET 4.0 and Any CPU. I installed .NET 4.5.2 Runtime on the "server" before trying to run the new version of my app with ZWave.

When I copy the folder where my app is in from the server back to a new folder on my development machine, it works. So I assume all the necessary files are there.

It looks like there is something installed on my development machine that is not present on the "server". How can I find out what that is?

yesyesuk commented 7 years ago

Hmmm, I'm a bit confused (and not experienced with this) When I load the OpenZWave.dll into dotPeek it says it is for .NET 4.0. but (one of) my own DLLs from my application is for .NET 4.5.2 dotpeek

Was it a mistake to change my application to .NET 4.5.2?

yesyesuk commented 7 years ago

Interesting... I have now installed Visual Studio 2015 Community on my server and now I don't get that exception any more. I would really like to find out what the cause of that is as it is not very practical to install VS on every machine my software needs to run on. Any ideas?

dotMorten commented 7 years ago

AnyCPU is not supported. That would mean it'll run as x64 on your PC and the library is only available as a 32bit dll. Please compile specifically for x86 and it should work.

dotMorten commented 7 years ago

oh just saw your follow-up question. I'm guessing it's because you are missing the VCLibs dependency. When installing Visual Studio, that'll get installed too, or you can just install those by themselves. Good point! I hadn't thought about that.

yesyesuk commented 7 years ago

Ah, OK, that makes sense considering that this wrapper is C++ , not C# (I didn't know that but recenlty read it here in another issue).

So, in addition to the .NET 4.5.2 Framework, is this what I need? https://www.microsoft.com/en-gb/download/details.aspx?id=48145

dotMorten commented 6 years ago

@yesyesuk Yup that should be it.