I work on my program, which I need for my bachelor work(C#/C++ interoperability) and I have problem with missing entry point in my code... I try create simply number generator, which will be genarated number in C++ class calling from C# ... At first I don't know how I pass a class, but then I found way how to do it on this page... Please help me fix it...
I added my code:
[C++]
#include <iostream>
#include <cstdlib>
#include <time.h>
using namespace std;
__declspec(dllexport) class Generator
{
private:
int zaciatok;
int koniec;
int pocetprvkov;
int *pole;
public:
Generator(){}
void Vytvor (int zaciatok, int koniec, int pocetprvkov)
{
srand((unsigned)time(0));
pole= new int [pocetprvkov];
}
void Napln()
{
for(int a=0; a<pocetprvkov; a++)
{
pole[a] = rand() % (koniec - zaciatok +1) + zaciatok;
}
}
void Vypis()
{
for(int a=0; a<pocetprvkov; a++)
cout << pole[a] << endl;
}
~Generator()
{
delete[] pole;
pole= 0;
}
};
extern "C"
{
__declspec(dllexport) Generator* Vytvor_Triedu() { return new Generator(); }
__declspec(dllexport) void Vytvor(Generator* prva) {prva->Vytvor(5,25,4); }
__declspec(dllexport) void Napln(Generator* prva) {prva->Napln(); }
__declspec(dllexport) void Vypis(Generator* prva) {prva->Vypis(); }
__declspec(dllexport) void Vymaz(Generator* prva) { delete prva; }
}
[C#]
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Runtime.InteropServices;
namespace GeneratorCsharp
{
class Program
{
[DllImport("DllTriedaGenerator.dll", CallingConvention = CallingConvention.Cdecl)]
public static extern IntPtr Vytvor_Triedu();
[DllImport("DllTriedaGenerator.dll", CallingConvention = CallingConvention.Cdecl)]
public static extern void Vytvor(IntPtr value);
[DllImport("DllTriedaGenerator.dll", CallingConvention = CallingConvention.Cdecl)]
public static extern void Napln(IntPtr value);
[DllImport("DllTriedaGenerator.dll", CallingConvention = CallingConvention.Cdecl)]
public static extern void Vypis(IntPtr value);
[DllImport("DllTriedaGenerator.dll", CallingConvention = CallingConvention.Cdecl)]
public static extern void Vymaz(IntPtr value);
static void Main(string[] args)
{
IntPtr trieda = Vytvor_Triedu();
Vytvor(trieda);
Napln(trieda);
Vypis(trieda);
Vymaz(trieda);
}
};
}
Thanks a lot!
Well, you should use dumpbin, find the mangled name of your function then add EntryPoint="yourMangledName" in your DllImport attribute.
Related
I am getting error as "'Cannot marshal 'parameter #1': Non-blittable generic types cannot be marshaled.'"
in C code I have
#include <stdio.h>
#include "pch.h"
typedef void(*RECV_CALLBACK)();
RECV_CALLBACK pfRecvCallBack;
typedef void(*RECV_CALLBACKINT_PARA)(int);
RECV_CALLBACKINT_PARA pfRecvCallBackIntPara;
extern "C"
{
__declspec(dllexport) void MethodWith_INT_Para(RECV_CALLBACKINT_PARA pfIntPara)
{
if (pfIntPara)
{
pfRecvCallBackIntPara = pfIntPara;
}
}
__declspec(dllexport) void MethodWithNo_Int_Para(RECV_CALLBACK pfRecv)
{
if (pfRecv)
{
pfRecvCallBack = pfRecv;
}
}
__declspec(dllexport) void Calling_INT_PARA(int a, int b)
{
pfRecvCallBackIntPara(a + b);
}
__declspec(dllexport) void Calling_without_Para(int a, int b)
{
pfRecvCallBack();
}
}
When i try to call this in C# call back without int parameter is working fine, but the callback with int parameter(line ->MethodWith_INT_Para(testWithIntPara);) is giving error as mention above.
C# code
using System.Runtime.InteropServices;
[DllImport("Sample.dll", CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi, EntryPoint = "MethodWithNo_Int_Para")]
static extern void MethodWithNo_Int_Para(Action ptr);
[DllImport("Sample.dll", CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi, EntryPoint = "MethodWith_INT_Para")]
static extern void MethodWith_INT_Para(Action<Int32> ptr);
[DllImport("Sample.dll", CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi, EntryPoint = "Calling_INT_PARA")]
static extern void Calling_INT_PARA(int x, int y);
[DllImport("Sample.dll", CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi, EntryPoint = "Calling_without_Para")]
static extern void Calling_without_Para();
Console.WriteLine("After calling Declare");
MethodWithNo_Int_Para(testWithoutIntPara);
Calling_without_Para();
void testWithoutIntPara()
{
Console.WriteLine("Without int para : called by Calling_without_Para");
}
MethodWith_INT_Para(testWithIntPara);
Calling_INT_PARA(1, 3);
void testWithIntPara(int x)
{
Console.WriteLine("With Int Para {x} : called by Calling_INT_PARA");
}
Console.ReadLine();
I am using Action to define callback function, please let me know if there is any other way to do this ?
Action<int> is generic, which cannot be marshalled. You need to create a custom delegate. Another problem is that C is expecting a function with the CDecl calling convention, bu the default is StdCall.
You need to use the following types, instead of Action and Action<int>
[UnmanagedFunctionPointer(CallingConvention.CDecl)]
public delegate void ActionCDecl();
[UnmanagedFunctionPointer(CallingConvention.CDecl)]
public delegate void ActionIntCDecl(int p1);
I'm sorry if this sounds too specific, but I need to get this done exactly this way and I'm stuck at it for days. In my real scenario, I have a MyStruct **ppObject which address must be passed to a third-party dll so that it will point to an array of structs. Afterwards, I must p/Invoke this same pointer to array, but I'm having problems with the contents of the struct. Here's a MCVE:
Unmanaged.h:
#ifndef _NATIVELIB_H_
#define _NATIVELIB_H_
#ifndef MCVE
#define MCVE
#endif
struct PlcVarValue
{
unsigned char byData[8];
};
#ifdef __cplusplus
extern "C" {
#endif
MCVE __declspec(dllexport) void FillArray(void);
MCVE __declspec(dllexport) void Clear(void);
MCVE __declspec(dllexport) PlcVarValue** GetValues(void);
#ifdef __cplusplus
}
#endif
#endif // _NATIVELIB_H_
Unmanaged.cpp
#include "stdafx.h"
#include "Unmanaged.h"
#include <iostream>
PlcVarValue** values;
MCVE __declspec(dllexport) void FillArray(void) {
values = (PlcVarValue**)malloc(sizeof(PlcVarValue*) * 5);
for (int i = 0; i < 5; i++)
{
values[i] = new PlcVarValue();
*values[i]->byData = i;
}
}
MCVE __declspec(dllexport) void Clear(void) {
delete *values;
free(values);
}
MCVE __declspec(dllexport) PlcVarValue** GetValues(void) {
return values;
}
PlcVarValue.cs
using System.Runtime.InteropServices;
namespace Managed
{
[StructLayout(LayoutKind.Sequential)]
public class PlcVarValue
{
[MarshalAs(UnmanagedType.ByValArray, SizeConst = 8)]
public byte[] Data;
}
}
ManagedClass.cs
using System;
using System.Collections.Generic;
using System.Runtime.InteropServices;
namespace Managed
{
public class ManagedClass
{
public ManagedClass()
{
FillArray();
}
[DllImport("Unmanaged.dll", CallingConvention = CallingConvention.Cdecl)]
private static extern void FillArray();
[DllImport("Unmanaged.dll", CallingConvention = CallingConvention.Cdecl)]
private static extern IntPtr GetValues();
[DllImport("Unmanaged.dll", CallingConvention = CallingConvention.Cdecl)]
private static extern void Clear();
public List<PlcVarValue> GetList()
{
int size = 5;
var list = new List<PlcVarValue>();
IntPtr ptr = GetValues();
var gch = GCHandle.Alloc(ptr, GCHandleType.Pinned);
try
{
int memSize = Marshal.SizeOf(typeof(PlcVarValue));
for (int i = 0; i < size; i++)
{
//I know this is wrong, it would work for a PlcVarValue* but I need it to work with a PlcVarValue**
list.Add((PlcVarValue)Marshal.PtrToStructure(new IntPtr(ptr.ToInt32() + memSize * i), typeof(PlcVarValue)));
}
}
finally
{
gch.Free();
}
return list;
}
public void FreeMemory()
{
Clear();
}
}
}
Program.cs
using Managed;
using System;
namespace Test
{
class Program
{
static void Main(string[] args)
{
var managed = new ManagedClass();
var list = managed.GetList();
foreach(var value in list)
Console.WriteLine(BitConverter.ToInt32(value.Data, 0));
managed.FreeMemory();
Console.ReadKey();
}
}
}
I hope that code is self-explanatory, I'll provide more information if needed. My problem is that the byte array (which I'm trying to marshal from the aforementioned unsigned char array) in the Program class prints different random data every time I run the program but when I check the C++ side with the debugger (before marshalling takes place), everything is fine. Like I mentioned in a comment in the code above, I believe the problem lies in this line in the ManagedClass class:
list.Add((PlcVarValue)Marshal.PtrToStructure(new IntPtr(ptr.ToInt32() + memSize * i), typeof(PlcVarValue)));
I know for a fact that this works fine with MyStruct *pObject where *pObject is an array of the same struct, but in this case I need a pointer to a pointer since what the third-party dll requires is actually a MyStruct ***pppObject (beats me, but that's what I have to deal with). I have tried to copy data from ppObject to a single pointer but, even though it worked, I was not satisfied with the result as it had some undesired side-effects in the real application. Also, should my usage of GCHandle be wrong, I'd gladly accept advice on how to fix it, but that's not the main focus of this question.
The C# side:
public List<PlcVarValue> GetList()
{
int size = 5;
var list = new List<PlcVarValue>(size);
IntPtr ptr = GetValues();
for (int i = 0; i < size; i++)
{
IntPtr ptrPlc = Marshal.ReadIntPtr(ptr, i * IntPtr.Size);
var plc = (PlcVarValue)Marshal.PtrToStructure(ptrPlc, typeof(PlcVarValue));
list.Add(plc);
}
return list;
}
and the C++ side (the only error was in the Clear()):
__declspec(dllexport) void Clear(void)
{
for (int i = 0; i < 5; i++)
{
delete values[i];
}
free(values);
}
BUT you shouldn't mix malloc and new!
If you need an explanation about what I'm doing, remember that you are returning a pointer to an array of pointers!
When trying to call a C++ DLL in C#, I encounter an exception (System.AccessViolationException) and I have no idea why. The C#, as well as the C++ Project are compiled in x64. In the Dll.cpp the dll-methods are exported. In this methods a class called OpcClient is used, which is located in the same project. My IDE is Visual Studio Express 2012.
Can anybody help?
C# Code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Runtime.InteropServices;
namespace OCTTester
{
class Program
{
static void Main(string[] args)
{
string[] array = new string[2];
array[0] = "Tag1";
array[1] = "Tag2";
Connect("IBHSoftec.IBHOPC.DA", array, 2, 0, 100);
Add("Tag1", 10);
Stop();
}
[DllImport(#"OPCClient.dll", CallingConvention = CallingConvention.Cdecl)]
static extern void Connect(string hostname, string[] tagnames, int amount_tags, int buffertype, int refreshrate);
[DllImport(#"OPCClient.dll", CallingConvention = CallingConvention.Cdecl)]
static extern void Add(string tagname, double value);
[DllImport(#"OPCClient.dll", CallingConvention = CallingConvention.Cdecl)]
static extern void Stop();
}
}
C++ Code (Dll.cpp):
#include "stdafx.h"
#include <exception>
#include "OpcClient.h"
using namespace std;
extern "C" { // we need to export the C interface
std::thread *output_thread = NULL;
_declspec(dllexport) int __cdecl Connect(const char *hostname, const char **tagnames, int amount_tags, int buffertype, int refreshrate)
{
OpcClient* opc_client = OpcClient::getInstance();
vector<string> vec_tagnames;
for (int i = 0; i < amount_tags; i++)
{
vec_tagnames.push_back(tagnames[i]);
}
try
{
opc_client->Connect(hostname, vec_tagnames, refreshrate, buffertype);
}
catch(exception &e)
{
std::cout << e.what() << "\n";
return -1;
}
output_thread = new thread(&OpcClient::Start, opc_client);
return 0;
}
_declspec(dllexport) int __cdecl Add(const char *tagname, double value)
{
OpcClient* opc_client = OpcClient::getInstance();
opc_client->AddValueToBuffer(tagname, value);
return 1;
}
_declspec(dllexport) int __cdecl Stop(void)
{
OpcClient* opc_client = OpcClient::getInstance();
opc_client->stop_ = true;
output_thread->join();
return 1;
}
}
Here is a sample of my C# code. Is there a way to decrease the amount of DllImport attributes?
namespace CSLib
{
class Program
{
static void Main(string[] args)
{
CLib.test();
CLib.test2(3);
A a = new A() { a = 9, b = 5 };
CLib.test3(ref a);
}
}
class CLib
{
[DllImport("path/to/CDLL", CallingConvention = CallingConvention.Cdecl)]
public static extern void test();
[DllImport("path/to/CDLL", CallingConvention = CallingConvention.Cdecl)]
public static extern void test2(int a);
[DllImport("path/to/CDLL", CallingConvention = CallingConvention.Cdecl)]
public static extern void test3(ref A a);
}
[StructLayout(LayoutKind.Sequential)]
struct A
{
[MarshalAs(UnmanagedType.I4)]
public int a, b;
}
}
Either expose the methods as COM methods, or create a C++/CLI wrapper around them.
I have a C++ assembly that I am importing using DLLImport.
I am attempting to call its method:
namespace Testing
{
class Test{
int Run(char* filePath, bool bEntry, double duration){//code}
};
}
by
[DllImport(dllName, CharSet = CharSet.Auto)]
public static extern int Run(string filePath, bool bEntry, double duration)
);
When I call its method, I get the error message:
Unable to find an entry point named Run in dll
The "Run" looks to be a non-static class method. Although, it's possible to call such methods from C# this is not the primary use-case. It would be way easier to consume it from .NET if you expose it via COM, or at-least as a plain C interface:
extern "C" __declspec(dllexport) void* Testing_Test_Create();
extern "C" __declspec(dllexport) void Testing_Test_Destroy(void* self);
extern "C" __declspec(dllexport) int Testing_Test_Run(void* self, char* filePath, bool bEntry, double duration);
And here is a sample how to call C++ class methods from C#:
// Test.cpp in NativeLib.dll
namespace Testing
{
class __declspec(dllexport) Test
{
public:
explicit Test(int data)
: data(data)
{
}
int Run(char const * path)
{
return this->data + strlen(path);
}
private:
int data;
};
}
// Program.cs in CSharpClient.exe
class Program
{
[DllImport(
"NativeLib.dll",
EntryPoint = "??0Test#Testing##QAE#H#Z",
CallingConvention = CallingConvention.ThisCall,
CharSet = CharSet.Ansi)]
public static extern void TestingTestCtor(IntPtr self, int data);
[DllImport(
"NativeLib.dll",
EntryPoint = "?Run#Test#Testing##QAEHPBD#Z",
CallingConvention = CallingConvention.ThisCall,
CharSet = CharSet.Ansi)]
public static extern int TestingTestRun(IntPtr self, string path);
static void Main(string[] args)
{
var test = Marshal.AllocCoTaskMem(4);
TestingTestCtor(test, 10);
var result = TestingTestRun(test, "path");
Console.WriteLine(result);
Marshal.FreeCoTaskMem(test);
}
}
Entry point names might be different for your build configuration/compiler, so use dumpbin utility to obtain them. Again, this is just a proof of concept, in real code it would be better to use COM.
See here: http://dotnetperls.com/dllimport
I'm not sure this will help if the function is a member of a class, but to locate the entry point by name, not ordinal, you'll need a .def file in your dll..
LIBRARY mylib
Run #1