Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ssis/2.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++;来自C++;和C# 我有一个C++应用程序,我必须转换成一个DLL。我有所有的消息来源_C#_C++_Interop_Pinvoke - Fatal编程技术网

调用C++;来自C++;和C# 我有一个C++应用程序,我必须转换成一个DLL。我有所有的消息来源

调用C++;来自C++;和C# 我有一个C++应用程序,我必须转换成一个DLL。我有所有的消息来源,c#,c++,interop,pinvoke,C#,C++,Interop,Pinvoke,我的职能是 外部“C” __declspec(dllexport)intmymain(inti,std::wstring myArgs) 我需要能够从C++或C语言包装器中传递参数。我可以从C++控制台应用程序调用它而不会出错。我现在正试着从C#给它打电话 这是我的c#代码: } 当我运行它时,我得到 System.Runtime.InteropServices.SehexException:“外部组件引发了异常。” 我没有主意了 TIA根据您上次的评论,您可能需要: [DllImport("r

我的职能是 外部“C” __declspec(dllexport)intmymain(inti,std::wstring myArgs)

我需要能够从C++或C语言包装器中传递参数。我可以从C++控制台应用程序调用它而不会出错。我现在正试着从C#给它打电话

这是我的c#代码:

}

当我运行它时,我得到

System.Runtime.InteropServices.SehexException:“外部组件引发了异常。”

我没有主意了


TIA

根据您上次的评论,您可能需要:

[DllImport("rep.dll", CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Unicode)]

无论如何,由于我没有RelpDLL,所以很难猜到

< P>指定Unicode,而且,在C或C++函数中,使用 Prtff<代码>用“%s”(大写字母S表示宽字符串)。或
std::wcout

否则,它可能会打印奇怪的字符或在找到的第一个空字符处终止。此外,您可能希望实际传递字符串的长度,但这完全取决于您

< C++函数的签名使用<代码> LPCWSTR (<代码> const WCHARYT*<代码> >)用于<代码> MyARGs<代码>参数

public static class DllHelper
{

    [DllImport("rep.dll", CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Unicode)]
    public static extern int mymain(int iArgs, string aArgs);
}

class Program
{
    static void Main(string[] args)
    {
        string s = "my string data";
        DllHelper.mymain(0, s);
    }
}

#ifdef __cplusplus
extern "C" {
#endif

int __declspec(dllexport) mymain(int i, const wchar_t* myArgs)
{
    #ifdef __cplusplus
    std::wcout<<std::wstring(myArgs)<<L"\n";
    #else
    printf(L"%S\n", myArgs);
    #endif
}

#ifdef __cplusplus
}
#endif
公共静态类DllHelper
{
[DllImport(“rep.dll”,CallingConvention=CallingConvention.Cdecl,CharSet=CharSet.Unicode)]
公共静态外部int-mymain(int-iArgs、string-aArgs);
}
班级计划
{
静态void Main(字符串[]参数)
{
string s=“我的字符串数据”;
DllHelper.mymain(0,s);
}
}
#ifdef_uucplusplus
外部“C”{
#恩迪夫
整数declspec(dllexport)mymain(整数i,常量wchar\u t*myArgs)
{
#ifdef_uucplusplus

std::wcout代码中使用的命名:

让我觉得您正在尝试传递一个字符串数组(类似于
wmain(int-argc,wchar\u t**argv)

如果这是您想要的,那么在本机DLL端,您的函数原型将如下所示:

extern "C" int __declspec(dllexport) mymain(int iArgs, wchar_t** aArgs)
[DllImport("rep.dll", 
    CallingConvention=CallingConvention.Cdecl, 
    CharSet=CharSet.Unicode)]
public static extern int mymain(int iArgs, [In] string[] aArgs);
string[] test = { "C64", "Bravo", "Charlie" };
int returnCode = mymain(test.Length, test);
在C#端,你可以写一个PInvoke声明如下:

extern "C" int __declspec(dllexport) mymain(int iArgs, wchar_t** aArgs)
[DllImport("rep.dll", 
    CallingConvention=CallingConvention.Cdecl, 
    CharSet=CharSet.Unicode)]
public static extern int mymain(int iArgs, [In] string[] aArgs);
string[] test = { "C64", "Bravo", "Charlie" };
int returnCode = mymain(test.Length, test);
可以在C#中这样调用:

extern "C" int __declspec(dllexport) mymain(int iArgs, wchar_t** aArgs)
[DllImport("rep.dll", 
    CallingConvention=CallingConvention.Cdecl, 
    CharSet=CharSet.Unicode)]
public static extern int mymain(int iArgs, [In] string[] aArgs);
string[] test = { "C64", "Bravo", "Charlie" };
int returnCode = mymain(test.Length, test);

STW::WString是C++类……你不能期望C字串被转换成“自动”。这里我执行了你的建议,只留下C++调用,但是希望一个LPTSTR作为C++ DLL中的参数。我可以调用它,但是我在DLL中得到的数据是垃圾MyAgSPTr= 0x000,F3CABEC80L。倯瑡⁨㩣瑜浥屰獪⹲硴⁴䈯捡畫印捥牵瑩⁹獪⹲p“