我的环境是vs2005/xp sp2
现给出提示的错误.
A call to PInvoke function 'CardBoard!CardBoard.cardsdll::cdtDraw' 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.错误停止在
        public bool drawCardBack(IntPtr hdc, int x, int y, eBACK back)
        {
            return cdtDraw(hdc, x, y, (int)back, mdFaceDown, 0);
        }错误的时候第一个纸牌的背面已经绘制出来了.具体代码如下:
首先有一个cardsdll.cs文件.这个文件主要是调用系统纸牌游戏带的cards.dll文件.是网上下载回来的
using System;
using System.Drawing;
using System.Runtime.InteropServices;namespace CardBoard
{
    public enum eSUIT : int
    {
        CLUBS = 0,//梅花
        DIAMOND = 1,//方块
        HEARTS = 2,//红心
        SPADES = 3 //黑桃
    }    public enum eRank : int
    {
        ACE = 0, //A
        TWO = 1,//2
        THREE = 2,//3
        FOUR = 3,//4
        FIVE = 4,//5
        SIX = 5,//6
        SEVEN = 6,//7
        EIGHT = 7,//8
        NINE = 8,//9
        TEN = 9, //10
        JACK = 10, //J
        QUEEN = 11,//Q
        KING = 12 //K
    }    public enum eBACK : int
    {
        CROSSHATCH = 53, /* XP = CROSSHATCH */
        WEAVE1 = 54, /* XP = SKY */
        WEAVE2 = 55, /* XP = MINERAL */
        ROBOT = 56, /* XP = FISH */
        FLOWERS = 57, /* XP = FROG */
        VINE1 = 58, /* XP = MOONFLOWER */
        VINE2 = 59, /* XP = ISLAND */
        FISH1 = 60, /* XP = SQUARES */
        FISH2 = 61, /* XP = MAGENTA */
        SHELLS = 62, /* XP = SANDDUNES */
        CASTLE = 63, /* XP = SPACE */
        ISLAND = 64, /* XP = LINES */
        CARDHAND = 65, /* XP = TOYCARS */
        UNUSED = 66, /* XP = UNUSED */
        THE_X = 67, /* XP = THE_X */
        THE_O = 68  /* XP = THE_0 */
    }    public class cardsdll
    {
        /*****************************************************************************************
        * Interface to cards.dll                                                                 *
        *****************************************************************************************/        [DllImport("cards.dll")]
        private static extern bool cdtInit(ref int width, ref int height);        [DllImport("cards.dll")]
        private static extern void cdtTerm();        [DllImport("cards.dll")]
        private static extern bool cdtDraw(IntPtr hdc, int x, int y, int card, int mode, long color);        [DllImport("cards.dll")]
        private static extern bool cdtDrawExt(IntPtr hdc, int x, int y, int dx, int dy, int card, int mode, long color);        [DllImport("cards.dll")]
        private static extern bool cdtAnimate(IntPtr hdc, int cardback, int x, int y, int frame);        /*****************************************************************************************
        * Constant declarations                                                                  *
        *****************************************************************************************/        /* mode parameters */
        const int mdFaceUp = 0; /* Draw card face up */
        const int mdFaceDown = 1; /* Draw card face down */
        const int mdHilite = 2; /* Same as FaceUp except drawn inverted */
        const int mdGhost = 3; /* Draw a ghost card -- for ace piles */
        const int mdRemove = 4; /* draw background specified by color */
        const int mdInvisibleGhost = 5; /* ? */
        const int mdDeckX = 6; /* Draw X */
        const int mdDeckO = 7; /* Draw O */        /*****************************************************************************************
        * Public Interface                                                                       *
        *****************************************************************************************/        public cardsdll()
        {
            int width = 71;
            int height = 95;
            if (!cdtInit(ref width, ref height))
                throw new Exception("cards.dll did not load");
        }        public void Dispose()
        {
            cdtTerm();
        }        public bool drawCard(IntPtr hdc, int x, int y, int card, int mode, long color)
        {
            return cdtDraw(hdc, x, y, card, mode, color);
        }        public bool drawCardBack(IntPtr hdc, int x, int y, eBACK back)
        {
            return cdtDraw(hdc, x, y, (int)back, mdFaceDown, 0);
        }        public bool drawAnimatedBack(IntPtr hdc, int x, int y, int card, int frame)
        {
            return cdtAnimate(hdc, card, x, y, frame);
        }        public bool drawInvertedCard(IntPtr hdc, int x, int y, int card)
        {
            return cdtDraw(hdc, x, y, card, mdHilite, 0);
        }        public bool drawEmptyCard(IntPtr hdc, int x, int y, long color)
        {
            return cdtDraw(hdc, x, y, 1, mdGhost, color);
        }        public bool drawExtrudedCard(IntPtr hdc, int x, int y, int dx, int dy, int card, int mode, long color)
        {
            return cdtDrawExt(hdc, x, y, dx, dy, card, mode, color);
        }        public static eSUIT getSuit(int card)
        {
            int suit = card % 4;
            return (eSUIT)suit;
        }        public static eRank getRank(int card)
        {
            int rank = card / 4;
            return (eRank)rank;
        }
    }
}
然后form1.cs如下方法调用.using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;namespace CardBoard
{
    public partial class Form1 : Form
    {
        private cardsdll cardHandle;
        private IntPtr hdc = new IntPtr();        public Form1()
        {
            InitializeComponent();
            cardHandle = new cardsdll();
        }        // Override Form's OnPaint method
        protected override void OnPaint(PaintEventArgs e)
        {
            Graphics x = e.Graphics;            hdc = x.GetHdc();
            x.ReleaseHdc(hdc);
            Draw();
        }        // Draw card
        private void Draw()
        {
            /* First Row */
            cardHandle.drawCard(hdc, 10, 10, (int)eBACK.CROSSHATCH, 1, 207);
            cardHandle.drawCardBack(hdc, 90, 10, eBACK.WEAVE1);
            cardHandle.drawCardBack(hdc, 170, 10, eBACK.WEAVE2);
            cardHandle.drawCardBack(hdc, 250, 10, eBACK.ROBOT);            /* Second Row */
            cardHandle.drawCardBack(hdc, 10, 110, eBACK.FLOWERS);
            cardHandle.drawCardBack(hdc, 90, 110, eBACK.VINE1);
            cardHandle.drawCardBack(hdc, 170, 110, eBACK.VINE2);
            cardHandle.drawCardBack(hdc, 250, 110, eBACK.FISH1);            /* Third Row */
            cardHandle.drawCardBack(hdc, 10, 210, eBACK.FISH2);
            cardHandle.drawCardBack(hdc, 90, 210, eBACK.SHELLS);
            cardHandle.drawCardBack(hdc, 170, 210, eBACK.CASTLE);
            cardHandle.drawCardBack(hdc, 250, 210, eBACK.ISLAND);            /* Fourth Row */
            cardHandle.drawCardBack(hdc, 10, 310, eBACK.CARDHAND);
            cardHandle.drawCardBack(hdc, 90, 310, eBACK.UNUSED);
            cardHandle.drawCardBack(hdc, 170, 310, eBACK.THE_X);
            cardHandle.drawCardBack(hdc, 250, 310, eBACK.THE_O);
        }    }
}

解决方案 »

  1.   

    最新发现:
    在ide中调试会出现这个错误.直接运行生辰的文件却没有错误.
      

  2.   

    代码太长,每看,错误的意思是指你的dll函数的拖管声明和这个函数的本身声明不一致
      

  3.   

    不是没错误而是错误不明显,
    A call to PInvoke function 'CardBoard!CardBoard.cardsdll::cdtDraw' has unbalanced the stack
    说明你C#传递参数的入栈的次序,或者方式,有问题,调式版的程序一般会有严格的检查,而发行版会把这些优化掉。
      

  4.   

    受托管的dll函数与中本身的声明不符