I have to call C# managed class library in C++CLI which has the files with Wrapper.cpp and Wrapper.h.
My managed class library includes the code like:
namespace MyClassLbrary
{
public class MyImage
{
public void TestImage(DrawImage drawImage)
{
}
}
public class DrawImage
{
}
}
Next Step should be
I have to call this TestImage(DrawImage darwImage) method from C++CLI i.e.in Wrapper.cpp.
I have to write all the unmanaged code in my Wrapper.h file. related to this. but the problem is I have to declare this TestImage(DrawImage drawImage) in my Wrapper.h file which contains the object of my c# managed class .I am importing that C# class library in my C++CLI. Then how to call that method in my Wrapper.h file which contains only the things related to unmanaged code. Please let me know if you have any kind of generalized solution so that I can call my Methods like TestImage.
Use separate header files, and put your actual unmanaged code in .cpp files.
Related
I'm trying to interact with a piece of hardware using my own code. The vendor offers a dll on his website with some sample code in C#, the DLL seems to be compiled from Visual Basic.
I'm wondering how to use this DLL in C++. Is C++/CLI the only way to do this? Or is there some other way that allows me to keep my code Cross-Platform compatible?
simple example: suppose we have a managed class Hello in a .NET assembly (hello.dll).
//This is c# but it could be also vb. Compilator of c# and VB.net translate it to same thing .Net assembly dll. Which is internaly exactly the same as c# dll.
class Hello
{
public void HelloWorld()
{
Console.Writeline("Hello World!");
}
}
Now we need to define a native proxy class for Hello:
class Hello
{
public:
Hello() : wrapper_("hello.dll", "namespace name") {}
void HelloWorld()
{
wrapper_("Hello");
}
private:
nativeAdapter::NativeProxy wrapper_;
};
}
In our main function, we can use the proxy as if it were the managed class:
int main(int, char **)
{
Namespace::Hello hello;
hello.Hello();
return 0;
}
I'm trying out using a C# library from a managed C++ project (CLR) for the first time and am experiencing compile-time problems.
The C# library in question is the Optional library from Nils Lück (https://github.com/nlkl/Optional).
I created a simple demo project so I can paste the code here for you to see. Basically, I have a C# class that has a public method which returns an Option<string, MyExceptionType> which is a generic type from the Optional library. Then I call this method from the C++/CLR project.
First, this are the compile-time error messages I get:
And here the code:
C# class
namespace OptionalAndCpp
{
using Optional;
public class MyException
{
public string Message { get; set; }
}
public class Something
{
public Option<string, MyException> DoStuff()
{
return Option.Some<string, MyException>("Hello");
}
}
}
Wrapper header
#pragma once
class TheWrappingClass
{
public:
void DoStuffFromWrapper();
};
Wrapper implementation
#include "stdafx.h"
#include "TheWrapper.h"
#using <OptionalAndCpp.dll>
#using <Optional.dll>
void TheWrappingClass::DoStuffFromWrapper()
{
OptionalAndCpp::Something^ sth = gcnew OptionalAndCpp::Something();
auto result = sth->DoStuff();
}
That's all. And when I hit compile, I get the errors shown in the screenshot above.
I took a look at the source code of the library and saw that it relies extensively on generics and extension methods, so it made me wonder whether it's a similar problem as with exporting C++ function templates from a DLL (which you can't), but I'm sincerely clueless.
Is it possible that some C# libraries are not usable from a C++/CLR project at all for some reason? As I already mentioned this is the first time I'm doing this and I have a lot more experience with C# than with C++.
Has anyone ever encountered a similar problem?
I'm grateful for any help and/or insights you may have.
I'm trying to use a class on Access VBA 7.0 which implements an Interface I made and I'm still getting "Automation error" even after adding the usual headers.
DropBox.cs
[ComVisible(true)]
[ClassInterface(ClassInterfaceType.None)]
[Guid("E2F07CD4-CE73-4102-B35D-119362624C47")]
[ComDefaultInterface(typeof(ICloudFileProvider))]
[ProgId("CloudFiles.dll")]
public class DropBox : ICloudFileProvider
{
public DropBox()
{
ConectaDropbox("TokenLongChicken");
}
public DropBox(string tokenUsuario)
{ //This was the original and good constructor. I know I can't use constructors with arguments on VBA. Just keeping it to compile with Test
ConectaDropbox(tokenUsuario);
}
public void ConectaDropbox(string tokenUsuario)
{
}
// This method and others come implemented from an interface (ICloudFileProvider)
public string SubirArchivo(string rutaLocal, string carpetaCloud, Tipos.TipoSobreescritura tipoSobreescritura)
{
}
This didn't work, so I saw I have to "create" a header for the Interface and I did it on the Interface itself. I'm still getting the error, so on the DropBox class, I added another header "enumerating" the methods I'm using on this class (which has no sense, but I've read another questions on SO which concretes you have to do so).
So I added this at the end of the DropBox class, noting it is as well on the ICloudFileProvider Interface (the real one).
[ComVisible(true)]
[Guid("E2F11CD4-CE73-4102-B35D-119362624C47")]
[InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
public interface IExposedClass
{
void DropBox();
void DropBox(string tokenUsuario);
void ConectaDropbox(string tokenUsuario);
string SubirArchivo(string rutaLocal, string carpetaCloud, Tipos.TipoSobreescritura tipoSobreescritura);
decimal ConvertBytesToMegabytes(long bytes);
void Descargar(string link, string carpetaLocal);
string GenerarLink(string rutaCloud);
void EliminarArchivo(string link);
}
I guess I'm doing something wrong but I'm kind of lost in this DLL and TLB hell. This project just works fine on C# but I need to integrate it on VBA and I see no examples with projects which uses real interfaces. I'm not really sure if the "InterfaceIsIUnkown" I add at the end of the DropBox class makes any sense, but I saw it on every example I found on the Internet (but none of them used a real Interface).
Could anybody help? Thanks.
P.S: yes, I perform the RegAsm.exe export to .TLB and then I add it to my Access, with no export errors apparently.
I'm using IronPython for fetching inner classes from C# dll.
for example:
namespace Platform.CardHost {
internal class ExtensionManager : IExtensionManager, IDisposable {
//... other code
IronPython Code
import clr
clr.AddReference('Platform.CardHost')
import Platform.CardHost.ExtensionManager
# raise ImportError: No module named ExtensionManager
# if it add to ref
clr.AddReference('Platform.CardHost.ExtensionManager')
# raise Error
# IOError: System.IO.IOException: Could not add reference to assembly
# Platform.CardHost.ExtensionManager
How can I import ExtensionManager? Or is this not possible?
So like I already wrote:
make ExtensionManager public if you want to access it from somewhere else than your assembly.
The definition of internalis
The type or member can be accessed by any code in the same assembly, but not from another assembly.
what you could do, to make it only available for another assembly is, to make it visible for a friend assembly:
using System.Runtime.CompilerServices;
using System;
[assembly:InternalsVisibleTo("my_friend_assembly")]
internal class ExtensionManager : IExtensionManager, IDisposable
{
}
else, I don't see any reason why making it internal but trying to access it from another assembly/your ironpython-script. Yeah, for friend assemblies, there are reasons, for sure.
for your new update: "I can't change class":
so maybe, the guy who wrote that class doesn't want you to import the class from elsewhere? That's the use of internal,protected,private and public.
Imho, it would be really bad to define in C# a class as internal, so you can't import it from C#, but IronPython still lets you import it.
for sure, you could try getting the code from the assembly, change it and make it again to a new assembly, like you wrote. But that's a lot of work and possibly in the end, it won't work.
Thanks to Matthias Burger for a fact that prompted the idea.
i try to decompile dll. because the file were large after he disassemble, it can't assemble without problem.
I wrote to the guy, he say me use C# interface ICardHost.
here how i use it, maybe for someone who meet similar problem.
clr.AddReference('Platform.CardHost')
from Platform import CardHost
from Platform.CardHost import ICardHost
host = CardHost.CardHost.CreateInstance(session)
# ExtensionManager is internal class but it available by interface
# here how to use C# interface
em = ICardHost.ExtensionManager.__get__(host)
as it in C#
// cardHost
public sealed class CardHost : Component, ICardHost
// ICardHost
public interface ICardHost {
IExtensionManager ExtensionManager { get; }
I am creating a class library and have different functions inside. I also have a Console application that can access this functions once they reference the class library. I would like to know how to make a function "invisible" so the client won't be able to see it exist and they can only use it if they write it out perfectly.
I have this function in my class Library :
public string custommessage(string messagetosend)
{
string receivedmessage = CallServer(messagetosend);
return receivedmessage;
}
and basicly when I am in a different program referencing the library I dont want to see this function in my list of avaiable functions to chose from :
Append
[System.ComponentModel.EditorBrowsable(System.ComponentModel.EditorBrowsableState.Never)]
to your method as an attribute. This will hide the function from intellisense.