C/C++与C#之间类型的对应

9 篇文章 0 订阅
8 篇文章 0 订阅

C/C++

C#

HANDLE, LPDWORD, LPVOID, void*

IntPtr

LPCTSTR, LPCTSTR, LPSTR, char*, const char*, Wchar_t*, LPWSTR

String [in], StringBuilder [in, out]

DWORD, unsigned long, Ulong

UInt32, [MarshalAs(UnmanagedType.U4)]

bool

bool

LP<struct>

[In] ref <struct>

SIZE_T

uint

LPDWORD

out uint

LPTSTR

[Out] StringBuilder

PULARGE_INTEGER

out ulong

WORD

uInt16

Byte, unsigned char

byte

Short

Int16

Long, int

Int32

float

single

double

double

NULL pointer

IntPtr.Zero

Uint

Uint32

 

Windows Data Type

.NET Data Type

BOOL, BOOLEAN

Boolean or Int32

BSTR

String

BYTE

Byte

CHAR

Char

DOUBLE

Double

DWORD

Int32 or UInt32

FLOAT

Single

HANDLE (and all other handle types, such as HFONT and HMENU)

IntPtr, UintPtr or HandleRef

HRESULT

Int32 or UInt32

INT

Int32

LANGID

Int16 or UInt16

LCID

Int32 or UInt32

LONG

Int32

LPARAM

IntPtr, UintPtr or Object

LPCSTR

String

LPCTSTR

String

LPCWSTR

String

LPSTR

String or StringBuilder*

LPTSTR

String or StringBuilder

LPWSTR

String or StringBuilder

LPVOID

IntPtr, UintPtr or Object

LRESULT

IntPtr

SAFEARRAY

.NET array type

SHORT

Int16

TCHAR

Char

UCHAR

SByte

UINT

Int32 or UInt32

ULONG

Int32 or UInt32

VARIANT

Object

VARIANT_BOOL

Boolean

WCHAR

Char

WORD

Int16 or UInt16

WPARAM

IntPtr, UintPtr or Object

 

Wtypes.h 中的非托管类型

非托管 C 语言类型

托管类名

说明

HANDLE

void*

System.IntPtr

在 32 位 Windows 操作系统上为 32 位,在 64 位 Windows 操作系统上为 64 位。

BYTE

unsigned char

System.Byte

8 位

SHORT

short

System.Int16

16 位

WORD

unsigned short

System.UInt16

16 位

INT

int

System.Int32

32 位

UINT

unsigned int

System.UInt32

32 位

LONG

long

System.Int32

32 位

BOOL

long

System.Int32

32 位

DWORD

unsigned long

System.UInt32

32 位

ULONG

unsigned long

System.UInt32

32 位

CHAR

char

System.Char

用 ANSI 修饰。

LPSTR

char*

System.String 或   System.Text.StringBuilder

用 ANSI 修饰。

LPCSTR

Const char*

System.String 或   System.Text.StringBuilder

用 ANSI 修饰。

LPWSTR

wchar_t*

System.String 或   System.Text.StringBuilder

用 Unicode 修饰。

LPCWSTR

Const wchar_t*

System.String 或   System.Text.StringBuilder

用 Unicode 修饰。

FLOAT

Float

System.Single

32 位

DOUBLE

Double

System.Double

64 位

 

  • 2
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要在C#中调用C++接口,可以使用P/Invoke(Platform Invocation Services)来实现。首先,需要在C#代码中声明C++接口的函数签名和相关的DllImport属性。 根据提供的引用内容,我们可以看到C++接口中有三个函数:start、stop和send。其中,start函数接受一个回调函数作为参数,用于接收数据。stop函数没有参数,send函数接受一个字符串参数。 在C#中,我们需要声明与C++接口中函数相对应的函数签名,并使用DllImport属性指定C++动态链接库的名称和入口点。 对于start函数,我们需要声明一个与回调函数签名相匹配的委托类型,并在start函数的DllImport属性中指定回调函数的名称。 对于stop函数和send函数,我们只需要声明函数签名,并在DllImport属性中指定入口点即可。 下面是一个示例代码,展示了如何在C#中调用C++接口: ```csharp using System; using System.Runtime.InteropServices; using System.Text; public delegate void RecvDataCallback(string message); public static class CppInterface { \[DllImport("***.dll", EntryPoint = "start", CallingConvention = CallingConvention.Cdecl)\] public static extern int Start(RecvDataCallback callback); \[DllImport("***.dll", EntryPoint = "stop", CallingConvention = CallingConvention.Cdecl)\] public static extern int Stop(); \[DllImport("***.dll", EntryPoint = "send", CallingConvention = CallingConvention.Cdecl)\] public static extern int Send(string message); } public class Program { public static void Main() { RecvDataCallback callback = new RecvDataCallback(ReceiveData); CppInterface.Start(callback); // 调用其他函数 CppInterface.Send("Hello, C++!"); CppInterface.Stop(); } public static void ReceiveData(string message) { Console.WriteLine("Received data: " + message); } } ``` 在上述示例代码中,我们首先定义了一个名为RecvDataCallback的委托类型,用于定义回调函数的签名。然后,在CppInterface类中声明了与C++接口中函数相对应的静态方法,并使用DllImport属性指定了动态链接库的名称和入口点。在Main方法中,我们创建了一个RecvDataCallback类型的委托实例,并将其作为参数传递给Start函数。在ReceiveData方法中,我们定义了接收数据的逻辑。 请注意,上述示例代码中的动态链接库名称和入口点需要根据实际情况进行替换。 #### 引用[.reference_title] - *1* *2* *3* [C# 调用 C++/C 接口方法](https://blog.csdn.net/casic207/article/details/106156584)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^koosearch_v1,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值