Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/151.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
警告C4190 C-连杆-已指定 目前我正在使用C++制作一个DLL函数。之后,我使用DllImport将这个dll导入到我的C项目中。在C++项目中,我在第一个构建过程中得到了警告C4190。 Warning C4190: 'abc' has C-linkage specified, but returns UDT'std::basic_string' which is incompatible with C >强> C++部分:不能在外“C++”< /C>界面中使用命名空间和C++对象。尝试使用更多的原始类型,如 int ,浮点, char < /C> >指针(例如,代码> char */COD>),返回值和参数。_C++_C_Dll - Fatal编程技术网

警告C4190 C-连杆-已指定 目前我正在使用C++制作一个DLL函数。之后,我使用DllImport将这个dll导入到我的C项目中。在C++项目中,我在第一个构建过程中得到了警告C4190。 Warning C4190: 'abc' has C-linkage specified, but returns UDT'std::basic_string' which is incompatible with C >强> C++部分:不能在外“C++”< /C>界面中使用命名空间和C++对象。尝试使用更多的原始类型,如 int ,浮点, char < /C> >指针(例如,代码> char */COD>),返回值和参数。

警告C4190 C-连杆-已指定 目前我正在使用C++制作一个DLL函数。之后,我使用DllImport将这个dll导入到我的C项目中。在C++项目中,我在第一个构建过程中得到了警告C4190。 Warning C4190: 'abc' has C-linkage specified, but returns UDT'std::basic_string' which is incompatible with C >强> C++部分:不能在外“C++”< /C>界面中使用命名空间和C++对象。尝试使用更多的原始类型,如 int ,浮点, char < /C> >指针(例如,代码> char */COD>),返回值和参数。,c++,c,dll,C++,C,Dll,C#Part:您可以检查MSDN的转换。有两种方法: < P>首先要求Visual C++编译器具有 /CLR < /代码>选项: public ref struct CppLib { static System::String^ abc(System::String^ targetFile) { return gcnew System::String(somestring); } } C#将其视为用C#编写的函数 第二,任何语言都可以: extern "C" { _

C#Part:您可以检查MSDN的转换。

有两种方法:

< P>首先要求Visual C++编译器具有<代码> /CLR < /代码>选项:

public ref struct CppLib
{
  static System::String^ abc(System::String^ targetFile)
  { 
    return gcnew System::String(somestring);
  }
}
C#将其视为用C#编写的函数

第二,任何语言都可以:

extern "C"
{
  __declspec(dllexport) size_t abc(const char* szTargetFile, char* szResult, size_t nResultBytes)
  { 
    std::string targetFile(szTargetFile);
    strncpy(szResult, somestring, nResultBytes);
    return strlen(somestring);
  }
}
要从C#使用,需要使用
StringBuilder
对表示输出字符串的参数进行p/invoke声明

第三种方法是使用<代码> BSTR < /C>(<代码> SysLoStrys和 SysFryStry API函数)。在C++中有一个<代码> BSTRUTT < /COD>包装器。在C中设置了一个属性,让P/UngKing知道字符串是“代码> BSTR


重要的是,双方都同意如何分配和释放字符串。这意味着永远不要使用C++实现特定的分配函数,如“代码> STD::String < /Calp>构造函数/析构函数。”< / P>。(如果可能的话,我愿意接受这方面的教育。)。相反,只需导出一个使用char*作为参数的C函数。在web上搜索“P/Invoke”,您会发现一些关于如何执行此操作的建议。欢迎使用StackOverFlow…)

extern "C"
{
  __declspec(dllexport) size_t abc(const char* szTargetFile, char* szResult, size_t nResultBytes)
  { 
    std::string targetFile(szTargetFile);
    strncpy(szResult, somestring, nResultBytes);
    return strlen(somestring);
  }
}