从 C# 程序访问 C++ DLL 中的多个函数
Accessing more than one functions in C++ DLL from C# program
我在 Visual C++ DLL 项目中实现了两个函数。这些函数将从 C# 程序中调用。
这些是我在 C++ DLL 中的函数(不是实际实现)。 mytest.cpp 项目。
extern "C"
{
__declspec(dllexport)void print_DB(null *head)
{
/*print all nodes*/
return;
}
__declspec(dllexport)void* add_node(void *head, int data)
{
/*Add node to data base*/
return (head);
}
}
我的C#程序如下
namespace example
{
class test
{
[DllImport("mytest.dll", CallingConvention = CallingConvention.Cdecl)]
unsafe public static extern void* add_node(void *head, int data);
unsafe public static extern void print_DB(void *head);
unsafe static void Main(string[] args)
{
/*initilialization*/
head = add_node(head, a)
head = add_node(head, b)
head = add_node(head, c)
printDB(head);
}
}
}
我可以一次使用一个函数。即,如果我从 C# 程序评论 print_DB() 减速,add_node() 功能正在运行。如果我评论 add_node() 函数 print_DB() 函数正在运行。通过这种方式,两个函数都给出了预期的结果。
如果我同时使用这两个函数,最后声明的函数会给出如下错误。调用或不调用函数对行为没有任何影响。
Could not load type 'ConsoleApplication2.Program' from assembly
'ConsoleApplication2, Version=1.0.0.0, Culture=neutral,
PublicKeyToken=null' because the method 'printDB' has no
implementation (no RVA).
其中 "ConsoleApplication2.Program" 是我的 C# 程序的名称。
如果我改变函数减速的顺序,其他函数也会得到同样的错误。
这些是我的问题
1)我是 C# 编程新手。我的期望是无论我们在 C# 程序中声明了多少个函数,这个函数都应该起作用。这是预期的行为吗?
2)如果不是预期的行为我做错了什么?
DllImport
行必须出现在每个导入的函数声明之前,如下所示:
namespace example
{
class test
{
[DllImport("mytest.dll", CallingConvention = CallingConvention.Cdecl)]
unsafe public static extern void* add_node(void *head, int data);
[DllImport("mytest.dll", CallingConvention = CallingConvention.Cdecl)]
unsafe public static extern void print_DB(void *head);
unsafe static void Main(string[] args)
{
/*initilialization*/
head = add_node(head, a)
head = add_node(head, b)
head = add_node(head, c)
printDB(head);
}
}
}
我在 Visual C++ DLL 项目中实现了两个函数。这些函数将从 C# 程序中调用。 这些是我在 C++ DLL 中的函数(不是实际实现)。 mytest.cpp 项目。
extern "C"
{
__declspec(dllexport)void print_DB(null *head)
{
/*print all nodes*/
return;
}
__declspec(dllexport)void* add_node(void *head, int data)
{
/*Add node to data base*/
return (head);
}
}
我的C#程序如下
namespace example
{
class test
{
[DllImport("mytest.dll", CallingConvention = CallingConvention.Cdecl)]
unsafe public static extern void* add_node(void *head, int data);
unsafe public static extern void print_DB(void *head);
unsafe static void Main(string[] args)
{
/*initilialization*/
head = add_node(head, a)
head = add_node(head, b)
head = add_node(head, c)
printDB(head);
}
}
}
我可以一次使用一个函数。即,如果我从 C# 程序评论 print_DB() 减速,add_node() 功能正在运行。如果我评论 add_node() 函数 print_DB() 函数正在运行。通过这种方式,两个函数都给出了预期的结果。
如果我同时使用这两个函数,最后声明的函数会给出如下错误。调用或不调用函数对行为没有任何影响。
Could not load type 'ConsoleApplication2.Program' from assembly 'ConsoleApplication2, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null' because the method 'printDB' has no implementation (no RVA).
其中 "ConsoleApplication2.Program" 是我的 C# 程序的名称。
如果我改变函数减速的顺序,其他函数也会得到同样的错误。
这些是我的问题
1)我是 C# 编程新手。我的期望是无论我们在 C# 程序中声明了多少个函数,这个函数都应该起作用。这是预期的行为吗?
2)如果不是预期的行为我做错了什么?
DllImport
行必须出现在每个导入的函数声明之前,如下所示:
namespace example
{
class test
{
[DllImport("mytest.dll", CallingConvention = CallingConvention.Cdecl)]
unsafe public static extern void* add_node(void *head, int data);
[DllImport("mytest.dll", CallingConvention = CallingConvention.Cdecl)]
unsafe public static extern void print_DB(void *head);
unsafe static void Main(string[] args)
{
/*initilialization*/
head = add_node(head, a)
head = add_node(head, b)
head = add_node(head, c)
printDB(head);
}
}
}