Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/150.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/windows/17.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++;.NET framework从3.5升级到4.6后DLL失败_C#_C++_Windows 10 Desktop - Fatal编程技术网

C# C/C++;.NET framework从3.5升级到4.6后DLL失败

C# C/C++;.NET framework从3.5升级到4.6后DLL失败,c#,c++,windows-10-desktop,C#,C++,Windows 10 Desktop,我有一个名为“代理”的申请,该申请有两个项目: C#项目 C/C++DLL 整个应用程序在我们使用vs2008构建的.NET3.5中运行良好。 由于某些要求,我们将.Net Framework升级到4.6和vs 2019以构建此版本。 现在我的应用程序服务运行了,但看起来C/C++dll导出在某个地方失败了。 我所尝试的: C侧 其中,TAgtLib是包装类,它像这样声明这些C/C++函数 [DllImport("ccplusplus.dll", CharSet = CharSet.Ansi)]

我有一个名为“代理”的申请,该申请有两个项目:

  • C#项目
  • C/C++DLL
  • 整个应用程序在我们使用vs2008构建的.NET3.5中运行良好。 由于某些要求,我们将.Net Framework升级到4.6和vs 2019以构建此版本。 现在我的应用程序服务运行了,但看起来C/C++dll导出在某个地方失败了。 我所尝试的: C侧

    其中,TAgtLib是包装类,它像这样声明这些C/C++函数

    [DllImport("ccplusplus.dll", CharSet = CharSet.Ansi)]
        public static extern int TAStartAgent(int port, string name, string bindir, string rundir, string debugOption, string stdoutOption, int mDefaultLogDays, int encryptonly);
    
    C++定义:

    extern "C" int _stdcall TAStartAgent(int PortNum, char *Name, char *binDir, char *runDir, char *debugOption, char *stdoutOption, int mDefaultLogDays, int encryptonly);
    
    每当调试从c#命中这个函数时,它就会进入内存冲突

    当我们更改为binDir.ToArray()并将dllexport声明更改为char[]binDir,similari for runDir时,它没有给出访问冲突,但它进入了dll内部

    尽管C dll仍然存在问题

    有人能告诉我在从3.5迁移到4.6之后,c/c++dll会受到什么影响吗

    我是新手


    谢谢

    这很可能发生,因为.NET4改变了纠正不正确的p/Invoke调用约定的方式

    发件人:

    要提高与非托管代码的互操作性性能, 平台调用中不正确的调用约定现在会导致 申请失败。在以前的版本中,封送处理层 解决了堆栈上的这些错误

    要解决这个问题,您必须在p/Invoke声明中指定正确的调用约定,例如

    DllImport("ccplusplus.dll", CharSet = CharSet.Ansi, CallingConvention = CallingConvention.Cdecl)]
    public static extern int TAStartAgent(int port, string name, string bindir, string rundir, string debugOption, string stdoutOption, int mDefaultLogDays, int encryptonly);
    

    当然,您需要为DLL指定正确的调用约定。

    谢谢。我已更新,但仍然存在内存损坏错误。@user8750269但您是否尝试了所有不同的调用约定?您可能只是没有选择正确的一个。另外,如果您在调试器下运行它,它会告诉您要使用哪个调试器。是,请尝试。我的调试器没有从C#跳入C dll。我创建了一个测试c项目,调试程序的dll正在工作。在我的项目上也尝试了相同的设置,它不是!!!我仍然觉得它的问题是从C到C++的字符串。看起来除了这两个参数,默认封送处理对所有参数都有效。我还没有收到相同的错误,你能提供一个简单的示例,可以复制给我们吗?
    DllImport("ccplusplus.dll", CharSet = CharSet.Ansi, CallingConvention = CallingConvention.Cdecl)]
    public static extern int TAStartAgent(int port, string name, string bindir, string rundir, string debugOption, string stdoutOption, int mDefaultLogDays, int encryptonly);