如何将参数从 C++ 传递给 C# 的函数?
How to give the parameter to C#'s function from C++?
调用方:C++代码
被调用者:C#代码
C#代码:
public int CSharpMethodName(string str, params object[] args)
{
....
}
C++代码:
bool CPPMethodName(const CString& cstr, CString parameters[])
{
//The object is already initialized with gcroot in order to access to C# code.
//Here I want to give parameters to the CSharpMethodName
result = obj->CSharpMethodName(gcnew System::String(sqlQuery), parameters);
}
这是作为参考添加到 C++ 项目时的 C# 方法签名:
CSharpMethodName(System::String^, ... array<System::Object^>^)
如何将参数的CString数组类型传递给CSharp方法?如果有什么不清楚的地方请告诉我。
谢谢
我发现我必须将非托管数组转换为托管数组。
这是适合我的完整代码:
bool CPPMethodName(const CString& cstr, const CString parameters[], size_t size)
{
cli::array<System::String^>^ managedParametersArray = GetManagedArray(parameters, size);
result = obj->CSharpMethodName(gcnew System::String(sqlQuery), managedParametersArray );
return result;
}
cli::array<System::String^>^ AdoHelperWrapper::GetManagedArray(const CString paramaters[], size_t size)
{
cli::array<System::String^>^ result = gcnew cli::array<System::String^>(size);
for (size_t i = 0; i < size; i++)
{
result[i] = gcnew System::String(paramaters[i]);
}
return result;
}
调用方:C++代码
被调用者:C#代码
C#代码:
public int CSharpMethodName(string str, params object[] args)
{
....
}
C++代码:
bool CPPMethodName(const CString& cstr, CString parameters[])
{
//The object is already initialized with gcroot in order to access to C# code.
//Here I want to give parameters to the CSharpMethodName
result = obj->CSharpMethodName(gcnew System::String(sqlQuery), parameters);
}
这是作为参考添加到 C++ 项目时的 C# 方法签名:
CSharpMethodName(System::String^, ... array<System::Object^>^)
如何将参数的CString数组类型传递给CSharp方法?如果有什么不清楚的地方请告诉我。
谢谢
我发现我必须将非托管数组转换为托管数组。
这是适合我的完整代码:
bool CPPMethodName(const CString& cstr, const CString parameters[], size_t size)
{
cli::array<System::String^>^ managedParametersArray = GetManagedArray(parameters, size);
result = obj->CSharpMethodName(gcnew System::String(sqlQuery), managedParametersArray );
return result;
}
cli::array<System::String^>^ AdoHelperWrapper::GetManagedArray(const CString paramaters[], size_t size)
{
cli::array<System::String^>^ result = gcnew cli::array<System::String^>(size);
for (size_t i = 0; i < size; i++)
{
result[i] = gcnew System::String(paramaters[i]);
}
return result;
}