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.
Related
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.
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.
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
I have a requirement that dictates use of an exported function from a C++ dll.
There is lots of stuff that needs to occur within the exported function, but I don't want to rewrite all of the C# code that I have written to do it.
I would like to just paste the C# code into the DLL and be done.
NOTE: I don't want to call a C# DLL, I want to put C# code INTO a C++ dll.
Here is the Exports.def file:
LIBRARY InstallCheckWin32
EXPORTS
IsConnectionPointValid #1
fnTest #2
Here is my .h File for the DLL:
// The following ifdef block is the standard way of creating macros which make exporting
// from a DLL simpler. All files within this DLL are compiled with the INSTALLCHECKWIN32_EXPORTS
// symbol defined on the command line. This symbol should not be defined on any project
// that uses this DLL. This way any other project whose source files include this file see
// INSTALLCHECKWIN32_API functions as being imported from a DLL, whereas this DLL sees symbols
// defined with this macro as being exported.
#ifdef INSTALLCHECKWIN32_EXPORTS
#define INSTALLCHECKWIN32_API __declspec(dllexport)
#else
#define INSTALLCHECKWIN32_API __declspec(dllimport)
#endif
INSTALLCHECKWIN32_API void CallCSharp();
Here is the .cpp file:
// InstallCheckWin32.cpp : Defines the exported functions for the DLL application.
//
#include "stdafx.h"
#include "InstallCheckWin32.h"
#include <tchar.h>
INSTALLCHECKWIN32_API void CallCSharp()
{
// this is where I want to use C# objects
// eg:
DateTime now = DateTime.Now;
}
I have set the General Configuration Property "Common Language Runtime Support" to Common Language Runtime Support(/clr)
What else do I need to set to use C# code in a C++ dll?
Thanks
No, you can't mix C# and C/C++ in same source file and expect compiler to somehow produce code for that.
In general mixing multiple coding languages in the same file has only limited support in some languages. With C/C++ you sometimes can mix assembly (as in mov ax,cx, not .Net assembly). Language/frameworks for site creation you frequently can mix in JavaScript (but not actually run at the same time)...
Fix: in most cases languages have comparable functionality/libraries - so it is frequently easier to rewrite code into one of the language. You can also interop between libraries written in different languages - how to do that depends on combination of languages. For some cases you can cross-compile source in one language to another, but generally it is limited to languages with same/similar frameworks (C++/C# is generally not falling into such bucket, but you still may find C# to C++ cross-compiler)
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.
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.