Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/142.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
c++;和带有DLL的C# 我正在寻找我的C++代码中的字符串和我的C语言形式。_C#_C++_Dll - Fatal编程技术网

c++;和带有DLL的C# 我正在寻找我的C++代码中的字符串和我的C语言形式。

c++;和带有DLL的C# 我正在寻找我的C++代码中的字符串和我的C语言形式。,c#,c++,dll,C#,C++,Dll,这是我在C#程序中的代码: >和我的C++程序中的代码:< /P> IHM类 我的外人 函数lireExpr() 当我执行Visual时,我说我尝试访问内存中受保护的部分。首先,您不能在dll中使用希望从其他语言访问的std::string或std::wstring。因此,必须将这些成员更改为char*或wchar\u t*首先,您不能在dll中使用希望从其他语言访问的std::string或std::wstring。因此,必须将这些对象更改为char*或wchar\u t*您无法(轻松)跨托

这是我在C#程序中的代码:

<> >和我的C++程序中的代码:< /P> IHM类 我的外人 函数lireExpr()
当我执行Visual时,我说我尝试访问内存中受保护的部分。

首先,您不能在dll中使用希望从其他语言访问的
std::string
std::wstring
。因此,必须将这些成员更改为
char*
wchar\u t*
首先,您不能在dll中使用希望从其他语言访问的
std::string
std::wstring
。因此,必须将这些对象更改为
char*
wchar\u t*
您无法(轻松)跨托管边界封送
std::string
对象<代码>标准::字符串和
系统::字符串
(由C#使用)不可相互转换。一个更好的解决方案是将字符串作为指针指向<代码> char < /> >,或者在C++中使用<代码> BSTR < /C> >类型。或者,您可以在C++/CLI中编写互操作层。这有帮助吗:您不能(轻松地)跨托管边界封送
std::string
对象<代码>标准::字符串和
系统::字符串
(由C#使用)不可相互转换。一个更好的解决方案是将字符串作为指针指向<代码> char < /> >,或者在C++中使用<代码> BSTR < /C> >类型。或者,您可以用C++/CLI编写一个互操作层!谢谢你的帮助!投反对票的不是我。。。我尝试在代码中实现它,现在我没有任何错误(很好!),但我的字符串是空的。有什么想法吗?在尝试连接C++和C++之前,我检查我的C++代码,我的字符串是OK。OK,你确定你使用的是char *而不是C++中的STD::string吗?您确定要在C#中使用StringBuilder而不是字符串吗?嗨!谢谢你的帮助!投反对票的不是我。。。我尝试在代码中实现它,现在我没有任何错误(很好!),但我的字符串是空的。有什么想法吗?在尝试连接C++和C++之前,我检查我的C++代码,我的字符串是OK。OK,你确定你使用的是char *而不是C++中的STD::string吗?您确定要使用StringBuilder而不是C#中的字符串吗?
[DllImport("libDLL.dll", CallingConvention = CallingConvention.Cdecl)]
            public static extern string valeurExpr(IntPtr pImg);

            public unsafe string objetLibValeurCarteExpr()
            {
                return valeurExpr(ClPtr);
            }
 class IHM {
 private:
        std::string             card_number_str;
        std::string             card_expr_str;
        std::string             card_porteur_str;
extern "C" _declspec(dllexport) std::string valeurExpr(IHM* pImg) {     return pImg->lireExpr(); }
 _declspec(dllexport) std::string lireExpr() const {
        return card_expr_str;
    }
void foo(char *str, int len)
{
    //write here content of string
}
[DllImport("...", CallingConvention = CallingConvention.Cdecl)
static extern void foo(StringBuilder str, int len);
void callFoo()
{
  StringBuilder sb = new StringBuilder(10); //allocate memory for string
  foo(sb, sb.Capacity);
}
void foo(const char *str)
{
    //do something with this str
}
[DllImport("...", CallingConvention = CallingConvention.Cdecl)
static extern void foo(string str);
void callFoo(string str)
{
   foo(str);
}
//class in C++
class Foo 
{
public:
  int Bar();
};

//now you will have to define non member function to create an instance of this class:
Foo* Foo_Create() 
{ 
    return new Foo(); 
}

//and now you will have to create non member function that will call Bar() method from a object:
int Foo_Bar(Foo* pFoo) 
{ 
    return pFoo->Bar(); 
}

//in the end you will have to create a non member function to delete your object:
void Foo_Delete(Foo* pFoo) 
{ 
    delete pFoo; 
}
[DllImport("Foo.dll")]
public static extern IntPtr Foo_Create();

[DllImport("Foo.dll")]
public static extern int Foo_Bar(IntPtr value);

[DllImport("Foo.dll")]
public static extern void Foo_Delete(IntPtr value);