Not that experienced with c++, so requesting some help here. What I got is a .net dll and I'm writing a wrapper, so that the .net dll can be used later in c++ and vb6 projects.
My code so far:
c# class I want to call:
public class App
{
public App(int programKey, List<string> filePaths)
{
//Do something
}
}
my c++ project:
static int m_programKey;
static vector<std::string> m_fileNames;
void __stdcall TicketReportAPI::TrStart(int iProgramKey)
{
m_programKey = iProgramKey;
};
void __stdcall TicketReportAPI::TrAddFile(const char* cFileName)
{
string filename(cFileName);
m_fileNames.push_back(filename);
}
void __stdcall TicketReportAPI::TrOpenDialog()
{
if(m_fileNames.size()> 0)
{
List<String^> list = gcnew List<String^>();
for(int index = 0; index < m_fileNames.size(); index++)
{
std::string Model(m_fileNames[index]);
String^ sharpString = gcnew String(Model.c_str());
list.Add(gcnew String(sharpString));
}
App^ app = gcnew App(m_programKey, list);
}
else
App^ app = gcnew App(m_programKey);
}
If I'm trying to compile the c++ project I get following error:
App(int,System::Collections::Generic::List ^)': Conversion from 'System::Collections::Generic::List' to 'System::Collections::Generic::List ^' not possible
Is it possible to pass a managed List from c++ to .net c#? If not, what do you guys suggest me to pass a string array to my c# assembly?
Every help is appreciated, Thanks in advance.
To convert a list to a string, use Python List Comprehension and the join() function. The list comprehension will traverse the elements one by one, and the join() method will concatenate the list's elements into a new string and return it as output.
To pass an array as an argument to a method, you just have to pass the name of the array without square brackets. The method prototype should match to accept the argument of the array type. Given below is the method prototype: void method_name (int [] array);
String in C programming is a sequence of characters terminated with a null character '\0'. Strings are defined as an array of characters. The difference between a character array and a string is the string is terminated with a unique character '\0'.
You're missing a ^
.
List<String^>^ list = gcnew List<String^>();
^-- right here
You'll also need to switch list.Add
to list->Add
.
You're using gcnew
, which is how you create something on the managed heap, and the resulting type is a managed handle, ^
. This is roughly equivalent to using new
to create an object on the unmanaged heap, and the resulting type is a pointer, *
.
Declaring a local variable of type List<String^>
(without the ^
) is valid C++/CLI: It makes the local variable use stack semantics. There's no C# equivalent to that variable type, so most of the .Net library doesn't work completely with it: For example, there's no copy constructors to deal with assignment to variables without the ^
. All of the managed APIs expect parameters with types that have the ^
, so most times, you'll want to use that for your local variables.
Important note: Everything in this answer applies to reference types in .Net (which are declared in C# as class
, or in C++/CLI as ref class
or ref struct
). It does not apply to value types (C# struct
, C++/CLI value class
or value struct
). Value types (such as int
, float
, DateTime
, etc) are always declared & passed without the ^
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With