首页 新闻 会员 周边

c# 调c 内在损坏 用 Visual C++ win32 控制台应用程序生成dll

0
悬赏园豆:5 [已关闭问题] 关闭于 2014-12-10 16:38

C代码:

extern "C" unsigned int inet_encode(const unsigned char *dat, unsigned int size, unsigned int cmd, unsigned char *out)
{
    unsigned int length = 0;

    if (!out)
        return 0;

    if ((!dat || !size) && (!cmd))
        return 0;


    /*        [cmd] = 0:应用数据报文         >0:命令            */

    if (cmd)
        length = inet_cmd_packet(dat, size, cmd, out);
    else
        length = inet_data_packet(dat, size, out);

    return length;

}
View Code

 

1 extern "C" int add(int a, int b)
2 {
3     return a + b;
4 }
View Code

 

c#代码  用 Visual C++ win32 控制台应用程序生成APPDLL.dll 文件

   [DllImport("APPDLL.dll", EntryPoint = "inet_encode", ExactSpelling = false, CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi)]
        public static extern int inet_encode(ref char[] dat, int size, int cmd, ref char[] outDat);

 

使用

  char[] dat = new char[300];

            for (int i = 0; i < dat.Length; i++)
            {
                dat[i] = (char)i;
            }

            char[] outDat = new char[400];

            char[] deDat = new char[400];

            int c;
            int d;
            int cmd = 0;
            int size = 0; 

            c = TestAPPDll.inet_encode(ref dat, 255, 60000, ref  outDat);

我调用Add的没有问题。

 运行时遇到了错误。此错误的地址为 0x705b8343,在线程 0x2da8 上。错误代码为 0xc0000005。此错误可能是 CLR 中的 bug,或者是用户代码的不安全部分或不可验证部分中的 bug。此 bug 的常见来源包括用户对 COM-interop 或 PInvoke 的封送处理错误,这些错误可能会损坏堆栈。

武琥的主页 武琥 | 初学一级 | 园豆:5
提问于:2014-12-10 14:25
< >
分享
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册