I wrote a C++ dll which I want to use in Unity3D. I put it into Assets/Plugins and wrote a C# script which should access a function of it but I get this error:
EntryPointNotFoundException: Multiply
test.Start () (at Assets/Scripts/test.cs:11)
Manager.h:
extern "C"
{
class Manager
{
public:
__declspec(dllexport) float Multiply(float a, float b);
};
}
Manager.cpp:
#include "Manager.h"
extern "C"
{
float Manager::Multiply(float a, float b)
{
return a * b;
}
}
C# script:
[DllImport("Manager", CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Unicode)]
private static extern float Multiply(float a, float b);
void Start()
{
Debug.Log(Multiply(10, 2));
}
Edit: This doesn't work during I'm using a class. To use a class you have to do it like this
Related
I have created a Qt/C++ .dll for use in a C#/WPF project.
The Qt section code is as below:
cpluspart_global.h:
#ifndef CPLUSPART_GLOBAL_H
#define CPLUSPART_GLOBAL_H
#include <QtCore/qglobal.h>
#if defined(CPLUSPART_LIBRARY)
# define CPLUSPARTSHARED_EXPORT Q_DECL_EXPORT
#else
# define CPLUSPARTSHARED_EXPORT Q_DECL_IMPORT
#endif
extern "C" __declspec(dllexport) void ShowSampleUI();
extern "C" __declspec(dllexport) int Sum(int a, int b);
#endif // CPLUSPART_GLOBAL_H
cpluspart.cpp:
#include "cpluspart.h"
CPlusPart::CPlusPart()
{
}
extern "C" void ShowSampleUI()
{
BenQ *sampleUI = new BenQ();
sampleUI->show();
}
extern "C" int Sum(int a, int b)
{
return a + b;
}
BenQ is an empty QDialog that is just used for tests.
Here is the implementation in C#:
[DllImport("CPlusPart.dll", CallingConvention = CallingConvention.Cdecl)]
public static extern void ShowSampleUI();
[DllImport("CPlusPart.dll", CallingConvention = CallingConvention.Cdecl)]
public static extern int Sum(int a, int b);
The "Sum" method works properly. However, when I want to call "ShowSampleUI", I receive the following error:
Unhandled exception at 0x00007FFCD125286E (ucrtbase.dll) in CPlusInWPF.exe: Fatal program exit requested.
I would appreciate it if you could kindly help me with this issue.
I'm trying to return from my native c++ code to c# code a struct with field of other struct but have an error : Method's type signature is not PInvoke compatible.
this is my c++ code:
namespace path
{
struct Vector3
{
public:
float x;
float y;
float z;
};
struct PointState
{
public:
Vector3 position_;
bool reformation;
};
}
Here my api functions:
extern "C"
{
PATHFINDER_API void SetupGraph(const char * json);
PATHFINDER_API path::Vector3 CheckReturn();
PATHFINDER_API path::PointState CheckStruct();
}
And this is my C# struct code:
[StructLayout(LayoutKind.Sequential)]
public struct Vector3
{
public float x;
public float y;
public float z;
};
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Ansi)]
public struct PointState
{
[MarshalAs(UnmanagedType.LPStruct)]
public Vector3 position_;
public bool reformation;
};
and Pinvoke DLLImport:
[DllImport("path", CallingConvention = CallingConvention.Cdecl, ExactSpelling = false, EntryPoint = "CheckStruct")]
private static extern PointState CheckStruct();
public PointState CheckReturn2()
{
return CheckStruct();
}
Pls, what i'm doing wrong? I can't find answer by myself.
There are a few problems with your code:
You used [MarshalAs(UnmanagedType.LPStruct)] on the nested struct, but that's just wrong. That field isn't a pointer. That attribute should be removed.
The bool type can't be marshalled in a return value. You can replace the C# bool with byte to resolve that, and then compare the value against zero.
In addition, some of the attributes that you have added seem needless. I suspect that you did the usual thing of trying lots of changes at random, but then leaving them in the code that you posted here.
I'd use the following declarations:
[StructLayout(LayoutKind.Sequential)]
public struct Vector3
{
public float x;
public float y;
public float z;
};
[StructLayout(LayoutKind.Sequential)]
public struct PointState
{
public Vector3 position_;
private byte _reformation;
public bool reformation { get { return _reformation != 0; } }
};
[DllImport("path", CallingConvention = CallingConvention.Cdecl)]
private static extern PointState CheckStruct();
Suppose I want to call c++ functions from a c# code, I am having the following problem:
Case 1:
class abc
{
private :
int a ;
public :
int getValue()
{
return 100;
}
};
int GetCounter()
{
abc* p = new abc();
int i = p->getValue();
return i;
}
This case when calling the function from C# throws me a DLL not found exception.
Case 2:
int GetCounter()
{
int i = 333;
return i;
}
The case when calling the function from C# works just fine.
Any ideas why? How can I fix it?
use this sample Line of code in CPP Project (MathFuncsDll.h)
extern "C" __declspec(dllexport) double Add(double a, double b);
extern "C" __declspec(dllexport) double Sub(double a, double b);
extern "C" __declspec(dllexport) double Mul(double a, double b);
extern "C" __declspec(dllexport) double Div(double a, double b);
in C# Code use like this
[DllImport("MathFuncs.dll", CallingConvention = CallingConvention.Cdecl)]
public static extern Double Add(Double a, Double b);
[DllImport("MathFuncs.dll", CallingConvention = CallingConvention.Cdecl)]
public static extern Double Mul(Double a, Double b);
[DllImport("MathFuncs.dll", CallingConvention = CallingConvention.Cdecl)]
public static extern Double Sub(Double a, Double b);
[DllImport("MathFuncs.dll",CallingConvention = CallingConvention.Cdecl)]
public static extern Double Div(Double a, Double b);
[DllImport("MathFuncs.dll",CallingConvention = CallingConvention.Cdecl)]
public static extern Double Bat(Double a, Double b);
I'm trying to pass a string from C# to C++, using platform invoke.
C++ code:
#include<string>
using namespace std;
extern "C"
{
double __declspec(dllexport) Add(double a, double b)
{
return a + b;
}
string __declspec(dllexport) ToUpper(string s)
{
string tmp = s;
for(string::iterator it = tmp.begin();it != tmp.end();it++)
(*it)-=32;
return tmp;
}
}
C# code:
[DllImport("TestDll.dll", CharSet = CharSet.Ansi, CallingConvention =CallingConvention.Cdecl)]
public static extern string ToUpper(string s);
static void Main(string[] args)
{
string s = "hello";
Console.WriteLine(Add(a,b));
Console.WriteLine(ToUpper(s));
}
I receive a SEHException. Is it impossible to use std::string like this? Should I use char* instead ?
Сorrect decision
C# side:
[DllImport("CppDll.dll", CallingConvention = CallingConvention.Cdecl)]
public static extern IntPtr GetString(string s);
public string GetString_(string s)
{
var ptr = GetString(s);
var answerStr = Marshal.PtrToStringAnsi(ptr);
return answerStr;
}
C++ side:
extern "C" __declspec(dllexport) const char* GetString(char* s)
{
string workStr(s);
int lenStr = workStr.length() + 1;
char* answer = new char[lenStr];
const char * constAnswer = new char[lenStr];
strcpy(answer, workStr.c_str());
constAnswer = answer;
return constAnswer;
}
And disable /sdl- in the settings of the cpp project.
One way of doing it without causing a memory leak is using a callback.
C# side:
private delegate bool DLLCallback(IntPtr message);
[DllImport(#"YourLibrary.dll", SetLastError = true, CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Auto)]
private static extern void Receive(DLLCallback callback);
private static bool callback(IntPtr ptr)
{
string result = Marshal.PtrToStringAnsi(ptr);
Console.WriteLine(result);
// If the Heap is used
// Marshal.FreeHGlobal(ptr);
return true;
}
private static void Main(string[] args) {
Receive(callback);
}
C++ side:
extern "C" {
typedef BOOL(__stdcall* OutputCallback)(const char* str);
__declspec(dllexport) void Receive(OutputCallback callback)
{
char buffer[BUFFER_SIZE];
ZeroMemory(buffer, BUFFER_SIZE);
BOOL callbackResult = callback(buffer);
}
}
There are other options. This is a good article about passing strings between managed and unmanaged code: article
I suggest to use char*. Here a possible solution.
If you create another C# function ToUpper_2 as follows
C# side:
[DllImport("TestDll.dll"), CallingConvention = CallingConvention.Cdecl]
private static extern IntPtr ToUpper(string s);
public static string ToUpper_2(string s)
{
return Marshal.PtrToStringAnsi(ToUpper(string s));
}
C++ side:
#include <algorithm>
#include <string>
extern "C" __declspec(dllexport) const char* ToUpper(char* s)
{
string tmp(s);
// your code for a string applied to tmp
return tmp.c_str();
}
you are done!
I'm trying to learn how to use DLL's in C#. I have a very simple DLL just to test the basics.
// MainForm.cs
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Runtime.InteropServices;
namespace DLL_Test
{
public partial class Form1 : Form
{
[DllImport("TestDLL.dll",
EntryPoint="?Add##YGHHH#Z",
ExactSpelling = true,
CallingConvention = CallingConvention.StdCall)]
public static extern int Add(int a, int b);
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
int num;
try
{
num = Add(2, 3);
richTextBox1.AppendText(num.ToString() + "\n");
}
catch (DllNotFoundException ex)
{
MessageBox.Show(ex.ToString());
}
catch (EntryPointNotFoundException ex)
{
MessageBox.Show(ex.ToString());
}
}
}
}
And the DLL code:
// TestDLL.cpp
__declspec(dllexport) int __stdcall Add(int a, int b) {
return(a + b);
}
dumpbin returns the following:
ordinal hint RVA name
1 0 00011005 ?Add##YGHHH#Z = #ILT+0(?Add##YGHHH#Z)
This (and other attempts listed below) have all returned the same exception:
System.EntryPointException: Unable to find entry point named "..."
So I am at a loss for how to solve this. Perhaps I do not understand how DllMain functions as the C# entry point for a DLL. TestDLL.dll works when I test it in a C++ application.
After searching for help, I've attempted the following changes:
// TestDLL.cpp
extern "C" __declspec(dllexport) int __stdcall Add(int a, int b) {
return(a + b);
}
Which results in this from dumpbin
ordinal hint RVA name
1 0 00011005 _Add#8 = #ILT+135(_Add#8)
Thus, I changed my C# code:
// MainForm.cs
...
[DllImport("TestDLL.dll",
EntryPoint="_Add",
ExactSpelling = true,
CallingConvention = CallingConvention.StdCall)]
public static extern int Add(int a, int b);
...
I've also tried __cdecl:
// TestDLL.cpp
extern "C" __declspec(dllexport) int __cdecl Add(int a, int b) {
return(a + b);
}
.
// MainForm.cs
...
[DllImport("TestDLL.dll",
EntryPoint="_Add",
ExactSpelling = true,
CallingConvention = CallingConvention.Cdecl)]
public static extern int Add(int a, int b);
...
Perhaps I'm misunderstanding the calling conventions. Any help would be very appreciated. Thank you.
use
extern "C" __declspec(dllexport) int __stdcall Add(int a, int b) { ... }
and
[DllImport("TestDLL.dll", CallingConvention = CallingConvention.Stdcall)]
public static extern int Add(int a, int b);
extern "C" will prevent name mangling with params and return type such as ?Add##YGHHH#Z.
__stdcall will prepend an _ and add #8 : _Add#8 (where 8 is the total size of arguments). Note that it also affects the way parameters are pushed on the stack.
In your DLLImport statement, since you specify CallingConvention.StdCall, you don't need to specify the name mangling. Just give the regular name (Add) and .NET will take care of name mangling (_Add#8).
Note that you must specify the CallingConvention or .NET wouldn't emit the correct code to push arguments on the stack
The following should work.
Unmanged:
extern "C" __declspec(dllexport) int Add(int a, int b)
{
return(a + b);
}
Managed:
class Program
{
[DllImport("TestDLL.dll")]
public static extern int Add(int a, int b);
static void Main()
{
Console.WriteLine(Add(1, 2));
}
}
For future reference: I had a similar problem, solved creating an EMPTY C++ dll project. Probably the standard Visual Studio template causes some trouble.
Refer to this link: http://www.codeproject.com/Articles/9826/How-to-create-a-DLL-library-in-C-and-then-use-it-w