在C#中使用DLL库(用C语言制作)时出错 [英] Error while using a DLL library (made in C) with C#

查看:82
本文介绍了在C#中使用DLL库(用C语言制作)时出错的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在此处尝试了一个示例,并使其正常工作,但是用途有限,因为它没有args也没有返回值,并且printf不起作用.但是我确实得到了它的链接,并且没有错误地运行.

但是,当我尝试添加参数和返回值时,出现错误:
检测到PInvokeStackImbalance
消息:调用PInvoke函数"ConsoleApplication1!ConsoleApplication1.Program :: addit"使堆栈不平衡.这可能是因为托管PInvoke签名与非托管目标签名不匹配.检查PInvoke签名的调用约定和参数是否与目标非托管签名匹配.

我在互联网上四处张望,到目前为止,仍然没有找到有关问题所在的帮助.
我将Visual C#Studio 2010 Express用于C#,并将www.codelite.org用于C编译.会有某种不兼容吗?
如果我能使它工作,我计划写出我所发现的有关如何使DLL与各种组合一起工作的信息,并将其发布到网络上,以帮助其他人避免绊脚石. DLL已经存在了很长时间,有人必须知道使它们起作用的魔力.

更具体地说,这是我正在使用的内容:

I''ve tried an example here and got that to work, but it''s of limited usefulness because it has no args and no return value, and the printf doesn''t work. But I did get it to link and run without errors.

However when I try to add arguments and a return value, I get the error:
PInvokeStackImbalance was detected
Message: A call to PInvoke function ''ConsoleApplication1!ConsoleApplication1.Program::addit'' 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.

I''ve looked all over the internet and so far haven''t found any help as to what the problem is.
I''m using Visual C# Studio 2010 Express for C#, and www.codelite.org for the C compile. Could there be some kind of incompatibility?
If I do get it working, I''m planning to write up what I find about how to make DLLs work with various combinations and post it on the web to help others avoid stumbling where I''ve stumbled. DLLs have been around for so long, somebody must know the magic to get them to work.

More specifically, here''s what I''m using:

#include <string.h>
#include <stdio.h>

extern "C"
{  __declspec(dllexport) int addit( int a, int b ); }
int addit (int a, int b) { return a+b;}


这是C#代码:


And here''s the C# code:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.InteropServices;     // DLL support
namespace ConsoleApplication1
{
    class Program
    {
        [DllImport(&quot;testsub.dll&quot;)]
        static extern int addit(int a, int b);
        //public static extern void addit();
        static void Main(string[] args)
        {
            int x = 2; int y = 3;
            int answer = addit(x, y);
            Console.WriteLine("hello world " + answer + "\n");
            Console.WriteLine("another hello world" + x + "\n");
        }
    }
}


我还尝试在C代码中将``int''更改为``long'',因为C的int是16位,而C#的int是32位,但这没什么区别.

P.S.:这是对我尝试过的文章的扩展,因此是对所有人的疑问.如果有人需要了解,文章链接只是一个参考.


I also tried changing the ''int'' to ''long'' in the C code because int for C is 16 bits and int for C# is 32 bits, but that didn''t make a difference.

P.S.: This is an extension to an article that I tried and thus a question to all. Article link is just a reference if someone needs to know.

推荐答案

从早期版本中取得了一些成功.但是我直接从http://www.adp-gmbh.ch/win/misc/mingw/dll.html网站打了一个例子,却得到了同样的错误.
还有其他想法吗?
broke something from an earlier version that did work. But I typed one example from http://www.adp-gmbh.ch/win/misc/mingw/dll.html site directly and it gets the same error.
Any other ideas?


如果C代码是16位整数,而C#是32,那么您需要在C#中使用short代替int:
If the C code is 16 bit integer, and the C# is 32, then you need to use short instead of int in the C#:
[DllImport("testsub.dll")]
static extern short addit(short a, short b);

您也可以使用Int16-short是同义词.

You could also use Int16 - short is a synonym.


OriginalGriff是正确的.通过使用C代码中的sizeof(int)来确保int的大小.然后使用short返回2,或者使用int返回4.

另外,您可以尝试使用不同的调用约定.例如:
OriginalGriff is right. Make sure of the size of int by using sizeof(int) from you C code. Then use short if it returns 2, or int it it returns 4.

Additionnaly you may try using different calling conventions. For example:
[DllImport("testsub.dll", CallingConvention=CallingConvention.StdCall)]
static extern short addit(short a, short b);


这篇关于在C#中使用DLL库(用C语言制作)时出错的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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