
c#引用c++动态库并调用其中的函数
namespaceWindowsFormsApplication1{publicpartialclassForm1:Form{publicclassCdll{[DllIm...
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
public class Cdll
{
[DllImport("ZREADER.dll", EntryPoint = "Sum", SetLastError = true, CharSet = CharSet.Unicode, ExactSpelling = true, CallingConvention = CallingConvention.Winapi)]
public extern static void Zreader();
}
private void btnShow_Click(object sender, EventArgs e)
{
int icdev;
icdev = ZT_reader_init(255,0);
MessageBox.Show("返回值为:" + icdev);
}
}
}
在网上查的资料首先是使用 [DllImport("ZREADER.dll", EntryPoint = "Sum", SetLastError = true, CharSet = CharSet.Unicode, ExactSpelling = true, CallingConvention = CallingConvention.Winapi)]加载c++的动态库。调用之后,我想通过一个click事件执行里面的初始化函数ZT_reader_init(255,0),如果成功的话会返回0,然后把0 输出。
private void btnShow_Click(object sender, EventArgs e)
{
int icdev;
icdev = ZT_reader_init(255,0);
MessageBox.Show("返回值为:" + icdev);
}
这段代码有错误,
错误 1 当前上下文中不存在名称“ZT_reader_init” F:\c# connect c++\WindowsFormsApplication1\WindowsFormsApplication1\Form1.cs 32 21 WindowsFormsApplication1
请问不可以直接使用还是其它问题吗?求高手指点,我是一个新手,刚接触这方面,还希望大家多多指点,或者提供相关解答的连接,谢谢!! 展开
{
public partial class Form1 : Form
{
public class Cdll
{
[DllImport("ZREADER.dll", EntryPoint = "Sum", SetLastError = true, CharSet = CharSet.Unicode, ExactSpelling = true, CallingConvention = CallingConvention.Winapi)]
public extern static void Zreader();
}
private void btnShow_Click(object sender, EventArgs e)
{
int icdev;
icdev = ZT_reader_init(255,0);
MessageBox.Show("返回值为:" + icdev);
}
}
}
在网上查的资料首先是使用 [DllImport("ZREADER.dll", EntryPoint = "Sum", SetLastError = true, CharSet = CharSet.Unicode, ExactSpelling = true, CallingConvention = CallingConvention.Winapi)]加载c++的动态库。调用之后,我想通过一个click事件执行里面的初始化函数ZT_reader_init(255,0),如果成功的话会返回0,然后把0 输出。
private void btnShow_Click(object sender, EventArgs e)
{
int icdev;
icdev = ZT_reader_init(255,0);
MessageBox.Show("返回值为:" + icdev);
}
这段代码有错误,
错误 1 当前上下文中不存在名称“ZT_reader_init” F:\c# connect c++\WindowsFormsApplication1\WindowsFormsApplication1\Form1.cs 32 21 WindowsFormsApplication1
请问不可以直接使用还是其它问题吗?求高手指点,我是一个新手,刚接触这方面,还希望大家多多指点,或者提供相关解答的连接,谢谢!! 展开
1个回答
展开全部
把你的C++ 动态链接库的函数原型贴出来。至少我从你的代码上下文发现你的外部函数原型声明是错误的。
比如,你的外部函数原型声明是这样的:
public extern static void Zreader();
而,你的调用形式是这样的:
icdev = ZT_reader_init(255,0);
单从上述的问题来看,存在以下两个错误:
1、调用方使用的函数名与被声明的函数名不一致;
2、外部函数原型声明应该是有形参列表的,而你的没有指定。
比如,你的外部函数原型声明是这样的:
public extern static void Zreader();
而,你的调用形式是这样的:
icdev = ZT_reader_init(255,0);
单从上述的问题来看,存在以下两个错误:
1、调用方使用的函数名与被声明的函数名不一致;
2、外部函数原型声明应该是有形参列表的,而你的没有指定。
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询