“运行时检查失败 #0 - ESP 的值未在函数调用中正确保存"从 C++ 代码成功 C# 回调后

quot;Run-Time Check Failure #0 - The value of ESP was not properly saved across a function callquot; after successful C# callback from the C++ code(“运行时检查失败 #0 - ESP 的值未在函数调用中正确保存从 C++ 代码成功 C# 回调后)
本文介绍了“运行时检查失败 #0 - ESP 的值未在函数调用中正确保存"从 C++ 代码成功 C# 回调后的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在制作一个使用 GameSpy C 代码(GP 部分)的 C# 应用程序.C 代码成功调用回调(这是 C# 代码),但我收到此错误 Run-Time Check Failure #0 - The value was not proper saved across a function call 之后回调完成.我用 C 代码制作了一个 DLL,如下所示:

I'm making a C# application which is using GameSpy C code (the GP part). The C code is calling a callback (which is C# code) succesfully, but I get this error Run-Time Check Failure #0 - The value of ESP was not properly saved across a function call right after the callback is done. I've made a DLL out of C code, like this:


    // GPCallback
    /////////////
    __declspec(dllexport) typedef void (* GPCallback)(
      GPConnection * connection,
      void * arg,
      void * param
    );

    // gpConnect
    ////////////
    __declspec(dllexport) GPResult gpConnect
    (
      GPConnection * connection,
      const gsi_char nick[GP_NICK_LEN],
      const gsi_char email[GP_EMAIL_LEN],
      const gsi_char password[GP_PASSWORD_LEN],
      GPEnum firewall,
      GPEnum blocking,
      GPCallback callback,
      void * param
    );

C# 是这样称呼它的:

C# is calling it like this:


   unsafe public delegate void GPCallback(
   GPConnection * connection,
   //GPConnectResponseArg arg,
   IntPtr arg,
   IntPtr param
  );

  [DllImport("saketestd.dll")]
  unsafe static extern GPResult gpConnect(
   GPConnection * connection, 
   gsi_char nick, 
   gsi_char email, 
   gsi_char password, 
   GPEnum firewall,
   GPEnum blocking,
   GPCallback callback,
   IntPtr param
  );
  unsafe public bool gpConnectE() {
   bool ret = false;
   try {
    GPResult res;
    debug.AddLine(this.getMethodName() + ": " + "connection before connect: " + connection.ToString("x"));
    fixed (int* pconn = &connection) {
     res = gpConnect(
      pconn,
      this.NICK,
      this.EMAIL,
      this.PASSWORD,
      GPEnum.GP_NO_FIREWALL,
      GPEnum.GP_BLOCKING,
      new GPCallback(this.ConnectResponse),
      IntPtr.Zero
     );
    }
    debug.AddLine(this.getMethodName() + ": " + "connection after connect: " + connection.ToString("x"));
    if (res != GPResult.GP_NO_ERROR) {
     debug.AddLine(this.getMethodName() + ": " + "failed: " + res);
    } else {
     debug.AddLine(this.getMethodName() + ": " + "OK");
     ret = true;
    }
   } catch (Exception ex) {
    debug.Text += ex.ToString();
   }
   return ret;
  }

  unsafe public void ConnectResponse(
   GPConnection * connection,
   //GPConnectResponseArg arg,
   IntPtr argPtr,
   IntPtr param
  ) {
   debug.AddLine(this.getMethodName() + " called with connection: " + (*connection).ToString("x"));
   GPConnectResponseArg arg;
   arg = (GPConnectResponseArg)Marshal.PtrToStructure(argPtr, typeof(GPConnectResponseArg)); 
   if (arg.result == GPResult.GP_NO_ERROR) {
    debug.AddLine(this.getMethodName() + ": Connected to GP");
    this.profileid = arg.profile;
   } else {
    debug.AddLine(this.getMethodName() + ": failed");
    debug.AddLine(this.getMethodName() + ": result: " + arg.result);
    debug.AddLine(this.getMethodName() + ": profile: " + arg.profile);
    debug.AddLine(this.getMethodName() + ": uniquenick: " + arg.uniquenick);
   }
  }

我认为我需要清除回调中的堆栈或更改 DLL 中的调用约定(这可能吗?).还有其他想法吗?

I believe that I need to clear the stack in my callback or change the calling convention in the DLL (is that possible?). Any other ideas?

推荐答案

检查你正在使用的调用约定.

Check the calling conventions you are using.

 [UnmanagedFunctionPointer(CallingConvention.Cdecl)]

参考其他相关问题:从 C 函数回调 - 崩溃

这篇关于“运行时检查失败 #0 - ESP 的值未在函数调用中正确保存"从 C++ 代码成功 C# 回调后的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

DispatcherQueue null when trying to update Ui property in ViewModel(尝试更新ViewModel中的Ui属性时DispatcherQueue为空)
Drawing over all windows on multiple monitors(在多个监视器上绘制所有窗口)
Programmatically show the desktop(以编程方式显示桌面)
c# Generic Setlt;Tgt; implementation to access objects by type(按类型访问对象的C#泛型集实现)
InvalidOperationException When using Context Injection in ASP.Net Core(在ASP.NET核心中使用上下文注入时发生InvalidOperationException)
LINQ many-to-many relationship, how to write a correct WHERE clause?(LINQ多对多关系,如何写一个正确的WHERE子句?)