DllNotFoundException error when loading C++ DLL - unity3d

I want to use C++ DLL in Unity, but I am prompted with “DllNotFoundException” error in Unity.I tried the method of others on the network, it didn't work for me.
I tried the method on Unity's official website, but it didn't work.
https://docs.unity3d.com/2017.3/Documentation/Manual/NativePlugins.html
https://docs.unity3d.com/2017.3/Documentation/Manual/PluginsForDesktop.html
Running the SimplestPluginExample Demo of Unity's official website is also prompted with “DllNotFoundException” error in Unity.
extern "C" int __declspec(dllexport) AddTwoIntegers(int a, int b)
{
return a + b;
}
put the MyDLL.dll in Assets/Plugins
[DllImport("MyDLL")]
private static extern int AddTwoIntegers(int a, int b);
void Start ()
{
Debug.Log(AddTwoIntegers(2, 3));
}
DllNotFoundException: MyDLL

Just try with extension. And put importing dll(MyDLL.dll) in output path of the function Caller project.
[DllImport("MyDLL.dll")]
private static extern int AddTwoIntegers(int a, int b);
void Start ()
{
Debug.Log(AddTwoIntegers(2, 3));
}

I solved my problem.My system is 64 bit.I used VS to create 64-bit DLLs.This dll in my Unity is correct,not error tip.

Related

VTI71.DLL Give error du0x80004005 in c# project

I am try to automate Gupta's Team Developer control as mention in
Centura Gupta Team Developer Automation Possibility
I download 32-bit trial version of Team Developer 7.1
[DllImport("user32.dll")]
static extern IntPtr WindowFromPoint(System.Drawing.Point p);
[DllImport("User32.dll", SetLastError = true, CharSet = CharSet.Auto)]
static extern long GetClassName(IntPtr hwnd, StringBuilder lpClassName, long nMaxCount);
const string guptadllpath = #"C:\program files (x86)\gupta\team developer 7.1\VTI71.DLL";
[DllImport(guptadllpath)]
extern static int VisTblFindString(IntPtr hwndTable, int lStartRow, IntPtr hwndColumn, string lpctszSearchFor);
IntPtr _wndFromPoint;
private void MainForm_MouseUp(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Right)
{
Cursor.Current = Cursors.Default;
Point p = PointToScreen(e.Location);
_wndFromPoint = WindowFromPoint(p);
StringBuilder classText = new StringBuilder(256);
GetClassName(_wndFromPoint, classText, 256);
listBox1.Items.Add("Class: " + classText);
int a = VisTblFindString(_wndFromPoint, 0, IntPtr.Zero, "Pat");
this.Text = a.ToString();
}
}
But give me below error:
System.Runtime.InteropServices.SEHException (0x80004005): External component has thrown an exception.
My sample application is
Please suggest me how to resolve this error. Is it correct way to use Gupta's dll in c# for automate?
Thanks,
Calling VisTblFindString(..) from outside won't work. Even though the function takes a window handle as a parameter this will only work from inside the "grid-application". The reason is that one process cannot peek into the memory of another process (ok you can use GetWindowText(..) but this is not applicable here since in a grid not every cell is a distinct window).
You have to set up some interprocess-communication. Unfortunately in gupta grid there are no built-in functions that support this.
The only way I see is that you have to modify the grid-application (not sure if you control the source code of it). If you have the possibility to modify it then you can implement automation e.g. via Windows messages.
I dont know c# from a bar of soap - but if you are using the dll outside of TeamDeveloper, it could be the way you have imported it , or you haven't registered the dll, or you dont have a license to use it outside of TeamDeveloper , or you should be using the 64bit version. A Trial license may not cut it. But I'm just guessing here.

HoloLens Callbacks with Native Library

My goal is to call methods, which are implemented in the Unity Code, from my UWP DLL. (So I can use them in my HoloLens Project)
I tried this with a bigger project but failed. Therefore I wrote a simple example to make it easier to find the mistake and exclude other influences.
But still, I get the same error.
My Working Environment:
64-bit Computer with OS Windows 10
Micsrosoft Visual Studio Community
2015 Version 14.0.25431.01 Update 3
HoloLens Emulator 10.0.14393.0
Unity 5.5.0f3 Personal (64 bit)
Creating the UWP DLL:
To approach this I created a C++ DLL(Windows Universal) in Visual Studio 2015 as followed:
New Project > Visual C++ > Windows > Universal > DLL(Universal Windows)
After the project was auto generated I added my code.
So the code looks like this:
Native Library Code:
SimpleProjectDLL.cpp:
#include "pch.h"
#define DLL_EXPORT __declspec(dllexport)
typedef void(*CB_V)();
typedef void(*CB_V_VI)(const char * a, int b);
CB_V_VI cb_native_log;
CB_V cb_call;
void log()
{
// this method makes problems !
cb_native_log("Call for callback", 1);
}
extern "C" {
DLL_EXPORT void initInterfaceCallbacks(
CB_V_VI native_log,
CB_V call
) {
cb_native_log = native_log;
cb_call = call;
}
DLL_EXPORT void callSmth()
{
cb_call();
}
DLL_EXPORT int getSomeInt()
{
return 42;
}
DLL_EXPORT void initCallback()
{
log();
}
}
SimpleProjectDLL.h is prepearing the delegates:
SimpleProjectDLL.h:
#pragma once
#include <cstdint>
#define DLL_EXPORT __declspec(dllexport)
extern "C"
{
typedef void(*CB_V)();
typedef void(*CB_V_VI)(const char * a, int b);
}
I did not make any changes to the auto generated files dllmain.cpp, pch.cpp, pch.h or targetver.h.
Finally I build the project for "Release" mode and architecture "x86" to generate the DLL-file.
Location of the DLL-file is now: project-root-folder/Release/SimpleProject/SimpleProjectDLL.dll.
---------------------
Next step I created a new Unity Project added the HoloLens-Toolkit and made sure that the new project is running fine on the emulator.
Unity Project Code:
After that I added the SimpleProjectDLL.dll in the Asset-Folder and implemented the following code:
First of all we need to create the connection between the delegates.
Cpp.cs prepears the Delegates:
Cpp.cs
using UnityEngine;
using System;
using System.Runtime.InteropServices;
namespace Cpp
{
delegate void DelegateV();
delegate void DelegateVVi(IntPtr a, int b);
}
SimpleInterfaceCpp.cs initializes the connection:
SimpleInterfaceCpp.cs
using Cpp;
using System.Runtime.InteropServices;
using UnityEngine;
public static class SimpleInterfaceCpp
{
public static void Init()
{
initInterfaceCallbacks(
SimpleInterface.NativeLog,
SimpleInterface.Call
);
}
[DllImport(SimpleInterface.DLL)]
private static extern void initInterfaceCallbacks(
DelegateVVi native_log,
DelegateV call
);
}
Main:
MainController.cs
using UnityEngine;
using System.Collections;
using System.Runtime.InteropServices;
public class MainController : MonoBehaviour
{
void Start ()
{
SimpleInterfaceCpp.Init();
SimpleInterface.TestCalls();
}
}
SimpleInterface.cs is calling the methodes:
SimpleInterface.cs
using System;
using UnityEngine;
using System.Runtime.InteropServices;
using AOT;
using IntPtr = System.IntPtr;
using Cpp;
using StringReturn = System.IntPtr;
public class SimpleInterface
{
public const string DLL = "SimpleProjectDLL";
public static void TestCalls()
{
// This works fine
int number = getSomeInt();
Debug.Log("getSomeInt: " + number);
// This also works fine and outputs "--- A callback ---"
callSmth();
// This call gives the output "call_log: native log" but crashes afterwards !
initCallback();
}
[MonoPInvokeCallback(typeof(DelegateVVi))]
public static void NativeLog(IntPtr logMessage,
int logLevel)
{
string result = StringFromCReturn(logMessage);
UnityEngine.Debug.Log(result); // outputs "call_log: native log"
}
[MonoPInvokeCallback(typeof(DelegateV))]
public static void Call()
{
UnityEngine.Debug.Log("--- A callback---");
}
[DllImport(DLL)]
private static extern void initCallback();
[DllImport(DLL)]
private static extern void callSmth();
[DllImport(DLL)]
private static extern int getSomeInt();
public static string StringFromCReturn(StringReturn someReturnVal)
{
return Marshal.PtrToStringAnsi(someReturnVal);
}
}
Now if I create a SLN, open the project in Visual Studio and start it with the "HoloLens Emulator" I get the following Output:
getSomeInt: 42
(Filename: C:/buildslave/unity/build/artifacts/generated/Metro/runtime/DebugBindings.gen.cpp Line: 51)
--- A callback---
(Filename: C:/buildslave/unity/build/artifacts/generated/Metro/runtime/DebugBindings.gen.cpp Line: 51)
call_log: native log
(Filename: C:/buildslave/unity/build/artifacts/generated/Metro/runtime/DebugBindings.gen.cpp Line: 51)
The program '[1932] SimpleProject.exe' has exited with code -1073740791 (0xc0000409).
After that the App just closes.
So my Question is, does anyone know what the problem could be?
Is this the right way to use callbacks in a HoloLens Project?
Or does someone know how to find an error description for the code "-1073740791 (0xc0000409)" ?
Additional Information:
I also tried it on a real HoloLens device, same issue, so the problem does not lays at the emulator.
The error is STATUS_STACK_BUFFER_OVERRUN. The call destroyed callstack.
You have different declarations of callbacks in SimpleProjectDLL.cpp and SimpleProjectDLL.h. Cpp file uses "CPP" call conversation, header uses "C" call conversation.
You should change SimpleProjectDLL.cpp by removing
typedef void(*CB_V)();
typedef void(*CB_V_VI)(const char * a, int b);
and adding
#include "SimpleProjectDLL.h"

Reference to method is ambiguous in OpenJDK but not Eclipse - why?

I know that similar questions have been asked (here, here, here), but none of the answers seem to apply to my case.
Consider the following set of interfaces:
public interface I1<X> {
void method(X arg);
}
public interface I2 {
void method(String arg);
}
public interface I3 extends I1<String>, I2 {
// empty
}
Now I want to call method(String) on an instance of I3, like so:
public class C implements I3 {
public void method(String arg) {
// does nothing
}
public static void main(String[] args) {
((I3) new C()).method("arg");
}
}
The OpenJDK (Java 7 or 8, doesn't matter) flags an incompatibility error here:
generics\C.java:10: error: reference to method is ambiguous
((I3) new C()).method("arg");
^
both method method(String) in I2 and method method(X) in I1 match
where X is a type-variable:
X extends Object declared in interface I1
Since X is instantiated to String in I3, I do not see where the problem comes from. Note that Eclipse considers this to be fine.
The answer to the question, "why?" is simple but probably not what you're looking for. It is, "Because Eclipse and Open JDK each have their own compilers and either a) one of them has a bug or b) the language specification (JLS) is ambiguous and they've interpreted it differently."
Figuring out which of a) or b) is the case is a tricky, tedious task. It means, as a starting point, reading the relevant sections of the JLS, trying to compile the same code with Oracle JDK's javac, and possibly diving into the bug tracking systems of Eclipse and OpenJDK.

SharpDX.Direct Input in Unity

I've been trying to implement DirectInput into unity using SharpDX.DirectInput dlls, but when i create a joystick, it gives me an error:
SharpDXException: HRESULT: [0x80070057], Module: [General], ApiCode: [E_INVALIDARG/Invalid Arguments], Message: The parameter is incorrect.
and here is my code:
using System;
using UnityEngine;
using SharpDX.DirectInput;
namespace KurdifyEngine.Input
{
static class Direct
{
public static DirectInput directInput;
public static Guid directGuid;
public static Joystick directGamepad ;
internal static void Initialize()
{
// Initialize DirectInput
directInput = new DirectInput();
directGuid = Guid.Empty;
DirectUpdate();
}
internal static void DirectUpdate()
{
// search for Gamepad
foreach (var deviceInstance in directInput.GetDevices(SharpDX.DirectInput.DeviceType.Gamepad, DeviceEnumerationFlags.AttachedOnly))
directGuid = deviceInstance.InstanceGuid;
}
if (directGuid != Guid.Empty)
{
directGamepad = new Joystick(directInput, directGuid);
}
}
}
}
the error happens when i create a joystick:
directGamepad = new Joystick(directInput, directGuid);
We have been developing exotic controller support for a Unity product and encountered the same problem (SharpDXException: HRESULT: [0x80070057]) on the exact same line.
We tried recompiling the SharpDX by fixing it like it is explained in this github post: https://github.com/sharpdx/SharpDX/issues/406 but encountered other problems with the DirectX (June 2010) SDK.
We just found a way to compile our Unity project using the libraries included in this project (which is an add-on for exotic controllers support in Kerbal Space Program): https://github.com/pbatard/AltInput just pick up the .dll under Libraries/, replace your old .dll by these and you're good to go !

Eclipse Java JNI, LoadLibrary linking error

This my second time coding Java and never referencing any external library before. I follow the JNI examples online and I get UnsatisfiedLinkError when trying to load the dll. I thought that I have to create DLL first before trying to load, but all the examples I've looked they don't mention about creating DLL. Most of them stating that I should create Java code first then native code.
public class ClassSample1
{
public native void displayHelloWorld();
static
{
System.loadLibrary("MyLittleJNI");
}
public static void main(String[] args)
{
// TODO Auto-generated method stub
ClassSample1 classSample1;
classSample1 = new ClassSample1();
classSample1.displayHelloWorld();
System.out.println("Hello");
}
}
How can I get ride of the error?
The sample code you provide assumes that there is a DLL in the search path called MyLittleJNI.dll, containing a method displayHelloWorld. The actual C function name in the DLL is decorated using a well defined syntax.
If you get an UnsatisfiedLinkError in loadLibrary(), it is because the JVM cannot find the DLL. You can duck the issue temporarily by specifying the full pathname to the DLL using the System.load(filename) method instead.
Once load or loadLibrary succeeds, you need to make sure that the native function is named correctly. To aid in this, you can use javah to generate a header file containing prototypes for all the native functions in a class.
More information about how to use JNI can be found in here and here.
EDIT: Also, the "Related" column to the right of this questions seems to contain several useful related question.
I try to create new project again.
So here is JNISample2.java file
public class JNISample2
{
static
{
System.loadLibrary("JNISample2Dll");
}
public native void displayHelloWorld();
public static void main(String[] args)
{
System.out.println("from java Hello");
JNISample2 JNIsample2;
JNIsample2 = new JNISample2();
JNIsample2.displayHelloWorld();
}
}
And here .h file that's generated by the javah -classpath . JNISample2
/* DO NOT EDIT THIS FILE - it is machine generated */
#include <jni.h>
/* Header for class JNISample2 */
#ifndef _Included_JNISample2
#define _Included_JNISample2
#ifdef __cplusplus
extern "C" {
#endif
/*
* Class: JNISample2
* Method: displayHelloWorld
* Signature: ()V
*/
JNIEXPORT void JNICALL Java_JNISample2_displayHelloWorld
(JNIEnv *, jobject);
#ifdef __cplusplus
}
#endif
#endif
Here is my .h file dll that I create VS2005 with MFC app.
// JNISample2Dll.h : main header file for the JNISample2Dll DLL
//
#pragma once
#ifndef __AFXWIN_H__
#error "include 'stdafx.h' before including this file for PCH"
#endif
#include "resource.h" // main symbols
#include "JNISample2.h"
// CJNISample2DllApp
// See JNISample2Dll.cpp for the implementation of this class
//
class CJNISample2DllApp : public CWinApp
{
public:
CJNISample2DllApp();
// Overrides
public:
virtual BOOL InitInstance();
DECLARE_MESSAGE_MAP()
};
JNIEXPORT void JNICALL Java_JNISample2_displayHelloWorld(JNIEnv *, jobject);
And here is my .cpp file
// JNISample2Dll.cpp : Defines the initialization routines for the DLL.
//
#include "stdafx.h"
#include "JNISample2Dll.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#endif
//
//TODO: If this DLL is dynamically linked against the MFC DLLs,
// any functions exported from this DLL which call into
// MFC must have the AFX_MANAGE_STATE macro added at the
// very beginning of the function.
//
// For example:
//
// extern "C" BOOL PASCAL EXPORT ExportedFunction()
// {
// AFX_MANAGE_STATE(AfxGetStaticModuleState());
// // normal function body here
// }
//
// It is very important that this macro appear in each
// function, prior to any calls into MFC. This means that
// it must appear as the first statement within the
// function, even before any object variable declarations
// as their constructors may generate calls into the MFC
// DLL.
//
// Please see MFC Technical Notes 33 and 58 for additional
// details.
//
// CJNISample2DllApp
BEGIN_MESSAGE_MAP(CJNISample2DllApp, CWinApp)
END_MESSAGE_MAP()
// CJNISample2DllApp construction
CJNISample2DllApp::CJNISample2DllApp()
{
// TODO: add construction code here,
// Place all significant initialization in InitInstance
}
// The one and only CJNISample2DllApp object
CJNISample2DllApp theApp;
// CJNISample2DllApp initialization
BOOL CJNISample2DllApp::InitInstance()
{
CWinApp::InitInstance();
return TRUE;
}
JNIEXPORT void JNICALL Java_JNISample2_displayHelloWorld(JNIEnv *, jobject)
{
MessageBox(NULL, TEXT("In JNISample2Dll"), TEXT("DLL"), 1);
}
After I use command prompt to run: java JNISample2, it display the string "from java Hello", but how come it does not display messagebox that I put inside the .cpp DLL file?