包括C DLL到C#时PInvoke的函数调用具有不平衡栈 [英] A call to PInvoke function has unbalanced the stack when including a C DLL into C#

查看:219
本文介绍了包括C DLL到C#时PInvoke的函数调用具有不平衡栈的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我写了一个C DLL和一些C#代码来测试,包括该DLL,并从它执行的功能。我不是太熟悉这个过程,而且我收到PInvokeStackImbalance例外,每当我的DLL函数从C#源代码中调用。代码如下(我有评论大部分的代码进行隔离这个问题):



C#纳入代码:

 使用系统;使用System.Runtime.InteropServices 
;
:使用System.IO;

命名空间TestConsoleGrids
{
类节目
{

函数[DllImport(LibNonthreaded.dll,入口点=流程)]
公共不安全的静态外部无效过程(长高,长低);

静态无效的主要(字串[] args)
{
的System.Console.WriteLine(启动3×3格计划);

过程((长)0,(长)0);

System.Console.ReadKey();
}
}
}



C ++ DLL函数代码

 的externC__declspec(dllexport)的无效​​处理(长高,长低); 

无效过程(长高,长低)
{
//所有代码都注释掉
}

Visual Studio中生成的DllMain代码(我不明白这个结构,所以我包括它)

  // dllmain.cpp:定义DLL应用程序的入口点。 
的#includestdafx.h中

BOOL APIENTRY的DllMain(HMODULE HMODULE,
DWORD ul_reason_for_call,
LPVOID lpReserved

$ { b $ b开关(ul_reason_for_call)
{
情况下DLL_PROCESS_ATTACH:
情况下DLL_THREAD_ATTACH:
情况下DLL_THREAD_DETACH:
情况下DLL_PROCESS_DETACH:
中断;
}
返回TRUE;
}



异常的细节是:




一个调用的PInvoke函数TestConsoleGrids!TestConsoleGrids.Program ::进程的不平衡有堆栈。这可能是因为托管的PInvoke签名不非托管的目标签名匹配。检查调用约定和的PInvoke签名的参数与非托管的目标签名。



解决方案

调用约定是错误的。如果删除整型参数不跳闸的MDA那么它是CDECL:

 函数[DllImport(LibNonthreaded.dll,CallingConvention = CallingConvention.Cdecl)] 
公共静态外部无效过程(INT高,低INT);

这是不是调用约定导出的DLL函数的标准,你可能会考虑在C更改它/ C ++代码,如果你能。


I have written a C DLL and some C# code to test including this DLL and executing functions from it. I am not too familiar with this process, and am receiving a PInvokeStackImbalance exception whenever my DLL function is called from the C# source code. The code is as follows (I have commented most code out to isolate this problem):

C# Inclusion code:

using System;
using System.Runtime.InteropServices;
using System.IO;

namespace TestConsoleGrids
{
    class Program
    {

        [DllImport("LibNonthreaded.dll", EntryPoint = "process")]
            public unsafe static extern void process( long high, long low);

        static void Main(string[] args)
        {
            System.Console.WriteLine("Starting program for 3x3 grid");

            process( (long)0, (long)0 );

            System.Console.ReadKey();
        }
    }
}

C++ DLL Function Code

extern "C" __declspec(dllexport) void process( long high, long low );

void process( long high, long low )
{
    // All code commented out
}

Visual Studio generated dllmain code (I do not understand this construct, so I am including it)

// dllmain.cpp : Defines the entry point for the DLL application.
#include "stdafx.h"

BOOL APIENTRY DllMain( HMODULE hModule,
                       DWORD  ul_reason_for_call,
                       LPVOID lpReserved
      )
{
 switch (ul_reason_for_call)
 {
 case DLL_PROCESS_ATTACH:
 case DLL_THREAD_ATTACH:
 case DLL_THREAD_DETACH:
 case DLL_PROCESS_DETACH:
  break;
 }
 return TRUE;
}

The details of the exception are:

A call to PInvoke function 'TestConsoleGrids!TestConsoleGrids.Program::process' has unbalanced the stack. This is likely because the managed PInvoke signature does not match the unmanaged target signature. Check that the calling convention and parameters of the PInvoke signature match the target unmanaged signature.

解决方案

The calling convention is wrong. If removing the int arguments doesn't trip the MDA then it is Cdecl:

 [DllImport("LibNonthreaded.dll", CallingConvention = CallingConvention.Cdecl)]
 public static extern void process(int high, int low);

This isn't the standard calling convention for exported DLL functions, you might consider changing it in the C/C++ code, if you can.

这篇关于包括C DLL到C#时PInvoke的函数调用具有不平衡栈的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆