Ho creato una DLL in C#. Ora voglio usare il R Environment per chiamare le funzioni in quella DLL. L'ambiente R supporta chiamate alle DLL non gestite di C/C++ ma non a quelle di .NET. Quindi la mia domanda è, posso chiamare le funzioni in una DLL C# da una DLL C/C++? In tal caso, hai un link per informazioni su come farlo?È possibile chiamare una DLL C# da una DLL C?
risposta
Il modo più semplice per farlo è esporre una delle classi C# nella DLL C# come un oggetto COM e quindi creare un'istanza dalla DLL C/C++. Se questa non è un'opzione accettabile, è necessario creare una DLL C++ in modalità mista (che contenga sia codice gestito che non gestito). La DLL C/C++ può chiamare funzioni esportate nella DLL in modalità mista, che a sua volta può inoltrare le chiamate alla classe C#.
Questo articolo potrebbe aiutarvi:
Aggiornato: C'è uno strumento chiamato mergebin fornito con il .NET SQLite wrapper è possibile utilizzare per creare un/DLL gestita modalità mista nativa. Afferra il codice sorgente da:
SQLite for ADO.NET 2.0 (SourceForge)
troverete l'exe nella cartella bin\tools
.
Kev
Grazie per il link Kev - lettura interessante. – Guy
In realtà è piuttosto semplice. Basta usare NuGet per aggiungere il pacchetto "UnmanagedExports" al tuo progetto .Net. Vedi https://sites.google.com/site/robertgiesecke/Home/uploads/unmanagedexports per i dettagli.
È quindi possibile esportare direttamente, senza dover fare un livello COM. Ecco l'esempio di codice C#:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;
using RGiesecke.DllExport;
class Test
{
[DllExport("add", CallingConvention = CallingConvention.Cdecl)]
public static int TestExport(int left, int right)
{
return left + right;
}
}
R dovrebbe essere in grado di caricare TextExport proprio come un normale C dll.
- 1. È possibile chiamare una DLL gestita da C++ non gestito?
- 2. Chiamare una DLL c da C++, C# e ruby
- 3. Chiamare DLL C++ da Python
- 4. Chiamare vb6 dll da C#
- 5. Una DLL può chiamare una DLL .NET?
- 6. Chiamare una DLL Delphi da un'applicazione C# .NET
- 7. Chiamare C# dll in vbscript
- 8. Chiamare una funzione DLL da Python
- 9. Chiamare una DLL da un'applet tramite JNI
- 10. Come chiamare DLL C++ in C#
- 11. È possibile utilizzare una dll in C# in un'applicazione golang
- 12. Come documentare una C# dll
- 13. Riferimento a una DLL da un'altra DLL
- 14. da un metodo C#, come chiamare ed eseguire una DLL, dove il nome della DLL deriva da una variabile String?
- 15. Come chiamare una DLL .NET da un processo Win32?
- 16. Chiamare una funzione DLL delphi dal codice C#
- 17. Come invertire una DLL nel codice C++?
- 18. Importare una DLL con C++ (Win32)
- 19. Chiamare una dll C++ (codice non gestito) da un servizio Windows C# (scritto nel codice gestito)
- 20. Debug C++ dll in C#
- 21. Utilizzo di dll C++ gestita da C#
- 22. Soluzione C# Visual Studio in una DLL
- 23. Come chiamare la funzione DLL C# da VBScript
- 24. Scrittura C# GUI su una DLL C++ o exe C++
- 25. Come usare una dll C++ in Unity3D?
- 26. Come utilizzare una dll C# in IronPython
- 27. Reverse engineer C++ DLL
- 28. Array VBA di oggetti definiti dall'utente da una DLL C#
- 29. Passare la stringa C# a una DLL C++ non gestita
- 30. Come creare una DLL NIM e lo chiamano da C#
Questa DLL in modalità mista C++ sembra fare il trucco: ho appena dato un'occhiata ai modelli disponibili per creare un progetto DLL in modalità mista utilizzando VS2008 ma non ne vedo uno. Come andresti sull'avvio di una DLL in modalità mista? – Guy
Se si crea una nuova libreria di classi C++, viene creato un progetto DLL C++ .NET. Finché l'impostazione "Common Language Runtime support" è impostata su "Common Language Runtime support (/ clr)", sei libero di utilizzare sia il codice gestito che non gestito nel progetto. – Andy