Call native C++ function from C# [duplicate] - c#

This question already has answers here:
Is it possible to embed C code in a C# project?
(4 answers)
Closed 6 years ago.
I have C++ legacy code with functions in the form:
double func(double)
I have this as a source file - it isnt in the form of a DLL although it could be turned into one if necessary
How can I call func in my C# code (maybe over managed C++)? I heard of Marshalling and DllImport but I did not find MSDN very helpful.

You have to compile C++ code as DLL library and then DllImport is way to go.
I don't know what problem you have with it. On MSDN I found this https://msdn.microsoft.com/en-us/library/e59b22c5.aspx
And also this article: http://www.mono-project.com/docs/advanced/pinvoke/ seems to be quite useful.

Related

DllImport parameters char *TagIDs[] [duplicate]

This question already exists:
c# static extern function [duplicate]
Closed 6 years ago.
Hi Im trying to import c++ library in c# code
his definition:
void IRFR_GetUIDs(char *reply, char *TagIDs[], CString *TagNum);
how my function Should look like
Thanks
This function cannot be imported with p/invoke. It has a CString parameter which is a native C++ class and thus inaccessible to p/invoke. Either wrap the DLL with a other DLL exposing a p/invoke friendly interface, or wrap the DLL with a C++/CLI assembly.

Importing an unmanaged DLL in C# when name is not known at compile time [duplicate]

This question already has answers here:
How can I specify a [DllImport] path at runtime?
(10 answers)
Closed 6 years ago.
I am hoping this is a simple question but I have not been able to find the solution in my searching. I have a C# application that needs to load data from several DLLs. Each DLL is guaranteed to have the same function foo(). But I want these DLLs to be plug and play at run time. The way I usually handle DLLs (where I know the name) is using:
[DllImport("my_dll.dll", CallingConvention = CallingConvention.Cdecl)]
public static extern int foo();
How do I do this with a dynamic string for a file name instead (e.g., "my_dll_3934.dll")? Also, there may be more than 1 dll that must be loaded that matches the same signature (e.g., "my_dll_3934.dll" and "my_dll_3935.dll").
The DLLs that will be used are generated by me but I want the end-user to just drop the DLL in as updates/new dlls become available without updating the application. I will be doing appropriate error checking and exception handling.
Thank you in advance.
Answer to my question can be found here:
How can I specify a [DllImport] path at runtime?
in the comment by user Ran. Thanks to Hackerman for pointing me to the correct solution.

How can I call C# DLL from C? [duplicate]

This question already has answers here:
Can you call a C# DLL from a C DLL?
(3 answers)
Calling C# from C
(4 answers)
Closed 9 years ago.
I am trying to use this solution. I can successfully create a C# DLL, but I have no idea how I can call it from C (no experience with this language). Can somebody help me please?
I have already seen similar questions, but they do not provide an answer to my one. I already know how I can create an unmanaged DLL with C#. The thing I do not know - how can I use it in C code?

Convert System::String^ (C# string) to C++ std::string [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
C++/CLI Converting from System::String^ to std::string
I am using a C# DLL in my C++ code (Visual Studio 2010). How do I convert a System::String^ to a C++ std::string?
System.Runtime.InteropServices.Marshal has several methods that convert a string to an array of characters, which you could then use any manner of methods to get a std::string.
I believe the recommendation is to use StringToBSTR eventually calling FreeBSTR once you have copied the data to a different object.
The question I linked to as a comment recommends StringToCoTaskMemUni.

How to make a .net(c#) library which can be used in delphi [duplicate]

This question already has answers here:
Closed 13 years ago.
Possible Duplicate:
Create a C# DLL That Can Be Imported in a Delphi App Using stdcall - Possible?
I am creating a c# library and like to make this library as com component which can be accessed from delphi. Please tell the how to achieve this.
How to make .Net (C#) classes accessible through COM is described here. Basicly your classes need to have a default constructor without parameters and you need to decorate your classes with some specific attributes. Then you need to register your assembly using regasm.
Then you can import the ActiveX/COM library in Delphi and call it like it was a regular ActiveX/COM Library.
I won't rewrite, but there is a solution on how to do this here : http://forums.devshed.com/delphi-programming-90/c-dll-need-to-be-used-in-delphi-231122.html
If you aren't opposed to using VB instead of C# there's a cleaner solution. In VB you can use the ComClass item template which does virtually all the work to expose your .NET type to COM for you.

Categories