Share via


How to call C# dll from C++

Question

Friday, February 22, 2013 12:15 PM

Hi All,

How to call a c# dll from C++ code.

Please share any sample code or any link related to this which have code.

Thanks in advance.

Regards

Ashish

All replies (6)

Friday, February 22, 2013 12:47 PM âś…Answered | 2 votes

Hi Agarwal.ashish,

1) Use CCW (Com Callable Wraper). To mark your managed(c#) assembly with "Register for COM interop"  please follow below steps

Step 1. With a project selected in Solution Explorer, on the Project menu, click Properties.
Step 2. Click the Compile tab in Visual Basic. Click the Build tab in C#.
Step 3. Select the Register for COM interop check box.

else you can visit

      a) http://msdn.microsoft.com/en-us/library/ms404285.aspx

For more details on "How to call a managed DLL from native Visual C++ code

For example see here

http://www.codeproject.com/Articles/16206/Call-C-code-from-C-and-read-an-array-of-struct-whi

2) Use Reverse P/Invoke as shown below

http://tigerang.blogspot.in/2008/09/reverse-pinvoke.html

when you use Reverse P/Invoke be careful for marshalling of parameters.

may be it helps you.
If anything is unclear feel free to ask me... :)

Thanks,
Nans11

ENjoy ThE WorLD Of COdE


Friday, February 22, 2013 2:58 PM

When oyu say C++ do you mean Native Code (machine code) or C++ that creates .NET applications? (compiles to MSIL, not machien code).

For the first, Nans11 seems to know the way. What you want to do is "using a .NET/CLR Library in C++ native code".

Forthe later: Just like you would use it in C# - Add a reference, use the namespace.

Let's talk about MVVM: http://social.msdn.microsoft.com/Forums/en-US/wpf/thread/b1a8bf14-4acd-4d77-9df8-bdb95b02dbe2


Friday, August 9, 2013 4:39 AM | 1 vote

I'm having trouble with the C++ part of the CodeProject article. With VS 6.0, the C++ code won't compile (Win 7), so I decided to go with VS 2010. With VS 2010, the C++ code has a compilation error:

ITest* pTest = NULL;
Error 1 error C2065: 'ITest' : undeclared

I imported the .tlb file, and included the .tli file, which appears to have the declaration of ITest. So I'm baffled as to why the compiler does not know what ITest is. I was hoping a C++ guru could offer suggestions, or perhaps post something on the CodeProject article (from 2006), as the author provided just a snippet of C++ code, not the entire listing.

Thanks in advance.

 


Thursday, June 26, 2014 9:54 AM

Im able to invoke C# dll from my C++ exe but everything works in my machine, whereas in non developer machine (no visual studio) throws exception at CoInitialize(NULL) let me know where i going wrong.

Thanks,

Dinesh


Thursday, June 26, 2014 10:27 AM

Hello,

  Compile your C++ code with the /clr flag. With that, you can call into any .NET code with relative ease..

 if the reply help you mark it as your answer.
 Free No OLE C# Word PDFExcelPowerPoint Component(Create, Modify, Convert & Print) 


Thursday, February 6, 2020 8:48 PM

See here

https://stackoverflow.com/questions/60085809/c-sharp-dll-calling-soap-web-service-from-c-app/60101510#60101510

-- d.