3

I have been looking for a way to call a function written in a C# EXE project (VS 2008) from a C++ DLL project. I can include the C# code as part of the C++ project itself if that is possible. All that I have been able to find is calling the C# DLL from C++.

Ultimately I want to call C# code from VB6 but I ask the question this way because I don't believe the later way is possible without an intermediate step.

Thanks,

Ian

2 Answers 2

6

Ultimately I want to call C# code from VB6 but I ask the question this way because I don't believe the later way is possible without an intermediate step.

You can register the C# classes to be visible to COM, and then call them directly from C++ or VB6.

For details, see the Example COM Class on MSDN, as well as Interoperability.

Sign up to request clarification or add additional context in comments.

8 Comments

@mydogisbox For calling from VB6, this is by far the easiest option... For using from C++, C++/CLI is easier - but if the only purpose is to use in VB, this isn't really hideous
Thanks, but what I would prefer to do is wrap the C# code so it is callable by VB later on. It doesn't have to be C++, I just want everything in a DLL. Any ideas? Thanks again.
@IanReynolds That's what the COM visible stuff does - it makes your assembly (DLL) directly usable by VB6 (and C++, and anything else that can use COM). This is the only way to have your C# code usable as a single DLL - wrapping in C++ would cause you to have 2 DLLs (the C# + the wrapper)
I apologize for my inexperience. Currently, the C# code doesn't assemble as a DLL. The C# builds an executable.
@IanReynolds You can use COM still, but typically, if you want to "wrap" it, you'd make the C# code a library. YOu can have a separate C# project make an executable that uses the library, as well as the VB side.
|
1

You can compile your c++ project with the /CLR option and then call C# from within it. To do that you need to include vcclr.h and then add a using statement for each dll you need to call from.

#include <vcclr.h>
#using <System.dll>

1 Comment

To avoid having to recompile your entire project with /clr, you could also develop just an assembly/DLL, written in C++/CLI that exposes an unmanaged interface to your C# code

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.