C# 在C中使用C dll中定义的typedef函数#
我有一个project.dll和头文件。定义如下:C# 在C中使用C dll中定义的typedef函数#,c#,c++,C#,C++,我有一个project.dll和头文件。定义如下: #ifdef PPSDK_EXPORTS #define PP_SDK_API __declspec(dllexport) #else #define PP_SDK_API __declspec(dllimport) #endif #ifndef __PP_SDK__ #define __PP_SDK__ typedef enum { PP_FALSE= 0x0, PP_TRUE = 0x01 } pp_bool; PP_S
#ifdef PPSDK_EXPORTS
#define PP_SDK_API __declspec(dllexport)
#else
#define PP_SDK_API __declspec(dllimport)
#endif
#ifndef __PP_SDK__
#define __PP_SDK__
typedef enum
{
PP_FALSE= 0x0,
PP_TRUE = 0x01
} pp_bool;
PP_SDK_API pp_bool SDK_Initialize(unsigned long*p_Status );
我在谷歌和这个网站上使用了一些帮助来在C#中使用这个dll,但它并不成功。
这是pp_bool类型上的错误。
这是我的代码:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Runtime.InteropServices;
namespace WindowsFormsApplication1
{
static class Program
{
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
[DllImport("project.dll")]
static extern pp_bool SDK_Initialize(unsigned long*p_Status );
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new Form1());
}
}
}
使用系统;
使用System.Collections.Generic;
使用System.Linq;
使用System.Threading.Tasks;
使用System.Windows.Forms;
使用System.Runtime.InteropServices;
命名空间Windows窗体应用程序1
{
静态类程序
{
///
///应用程序的主要入口点。
///
[状态线程]
[DllImport(“project.dll”)]
静态外部pp_bool SDK_初始化(无符号长*p_状态);
静态void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(新Form1());
}
}
}
。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。
你能帮我处理一下吗。
谢谢 您的p/Invoke定义不起作用,因为C中不存在
pp\u bool
类型。问题是,C++中的编译器决定了<代码>枚举>代码>的大小,因此我们不能确定大小是什么。它可以是int
上的byte
和short
。虽然它是用VisualC++作为C代码编译的。我的建议是尝试每种方法,并确定哪种方法有效
您还可以尝试将返回值声明为bool
,这将是Natural.NET类型。但是,返回值是32位(int size)
Windows中C++的p/Unjk定义中还有一个错误,
p_状态
应该是uint
而不是无符号long
另外,如果SDK\u Initialize
函数只返回一个无符号long
值,则不需要使用指针。改用ref
参数。封送拆收器将负责转换,您不必使用不安全的代码
最后,您需要移动p/Invoke定义。[STAThread]
属性应该在Main()
方法上,而不是在p/Invoke定义上。更不用说Main()
的文档注释正在应用于SDK\u Initialize
所以应该是这样的:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Runtime.InteropServices;
namespace WindowsFormsApplication1
{
static class Program
{
[DllImport("project.dll")]
static extern bool SDK_Initialize(ref uint p_Status );
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new Form1());
}
}
}
使用系统;
使用System.Collections.Generic;
使用System.Linq;
使用System.Threading.Tasks;
使用System.Windows.Forms;
使用System.Runtime.InteropServices;
命名空间Windows窗体应用程序1
{
静态类程序
{
[DllImport(“project.dll”)]
静态外部布尔SDK_初始化(参考单元p_状态);
///
///应用程序的主要入口点。
///
[状态线程]
静态void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(新Form1());
}
}
}
您需要确定pp\u bool
的类型。由于它是一个enum
,编译器可以使用它想要的任何整数类型,只要该类型可以适合所有的值(并且除非必须,否则它不会大于int)——因此它可以是bool、short、int、char等。您需要打印出sizeof(pp_bool)
,以了解特定编译器使用的类型,然后将其与C#中大小相等的整数类型匹配。此外,在C端无符号长*
应该是ref-uint
(假设C代码中有32位长)。