C++(Win 32)

C#

char**

作为输入参数转为char[],通过Encoding类对这个string[]进行编码后得到的一个char[]

作为输出参数转为byte[],通过Encoding类对这个byte[]进行解码,得到字符串

C++ Dll接口:

void CplusplusToCsharp(in char** AgentID, out char** AgentIP);

C#中的声明:

[DllImport("Example.dll")]

public static extern void CplusplusToCsharp(char[] AgentID, byte[] AgentIP);

C#中的调用:

Encoding encode = Encoding.Default;

byte[] tAgentID;

byte[] tAgentIP;

string[] AgentIP;

tAgentID = new byte[100];

tAgentIP = new byte[100];

CplusplusToCsharp(encode.GetChars(tAgentID), tAgentIP);

AgentIP[i] = encode.GetString(tAgentIP,i*Length,Length);

Handle

IntPtr

Hwnd

IntPtr

int*

ref int

int&

ref int

void*

IntPtr

unsigned char*

ref byte

BOOL

bool

DWORD

int 或 uint(int 更常用一些)

枚举类型

Win32:

BOOL MessageBeep(UINT uType // 声音类型); 其中的声音类型为枚举类型中的某一值。

C#:

用户需要自己定义一个枚举类型:

public enum BeepType

{

  SimpleBeep = -1,

  IconAsterisk = 0x00000040,

  IconExclamation = 0x00000030,

  IconHand = 0x00000010,

  IconQuestion = 0x00000020,

  Ok = 0x00000000,

}

C#中导入该函数:

[DllImport("user32.dll")]

public static extern bool MessageBeep(BeepType beepType);

C#中调用该函数:

MessageBeep(BeepType.IconQuestion);

结构类型

Win32:

使用结构指针作为参数的函数:

BOOL GetSystemPowerStatus(

 LPSYSTEM_POWER_STATUS lpSystemPowerStatus

);

Win32中该结构体的定义:

typedef struct _SYSTEM_POWER_STATUS {

BYTE  ACLineStatus;

BYTE  BatteryFlag;

BYTE  BatteryLifePercent;

BYTE  Reserved1;

DWORD BatteryLifeTime;

DWORD BatteryFullLifeTime;

} SYSTEM_POWER_STATUS, *LPSYSTEM_POWER_STATUS;

C#:

用户自定义相应的结构体:

struct SystemPowerStatus

{

  byte ACLineStatus;

  byte batteryFlag;

  byte batteryLifePercent;

  byte reserved1;

  int batteryLifeTime;

  int batteryFullLifeTime;

}

C#中导入该函数:

[DllImport("kernel32.dll")]

public static extern bool GetSystemPowerStatus(

  ref SystemPowerStatus systemPowerStatus);

C#中调用该函数:

SystemPowerStatus sps;

….sps初始化赋值……

GetSystemPowerStatus(ref sps);

字符串

对于字符串的处理分为以下几种情况:

1、  字符串常量指针的处理(LPCTSTR),也适应于字符串常量的处理,.net中的string类型是不可变的类型。

2、  字符串缓冲区的处理(char*),即对于变长字符串的处理,.net中StringBuilder可用作缓冲区

Win32:

BOOL GetFile(LPCTSTR lpRootPathName);

C#:

函数声明:

[DllImport("kernel32.dll", CharSet = CharSet.Auto)]

static extern bool GetFile (

 [MarshalAs(UnmanagedType.LPTStr)]

 string rootPathName);

函数调用:

string pathname;

GetFile(pathname);

备注:

DllImport中的CharSet是为了说明自动地调用该函数相关的Ansi版本或者Unicode版本

变长字符串处理:

C#:

函数声明:

[DllImport("kernel32.dll", CharSet = CharSet.Auto)]

public static extern int GetShortPathName(

  [MarshalAs(UnmanagedType.LPTStr)]

  string path,

  [MarshalAs(UnmanagedType.LPTStr)]

  StringBuilder shortPath,

  int shortPathLength);

函数调用:

StringBuilder shortPath = new StringBuilder(80);

int result = GetShortPathName(

@"d:\test.jpg", shortPath, shortPath.Capacity);

string s = shortPath.ToString();

struct

具有内嵌字符数组的结构:

Win32:

typedef struct _TIME_ZONE_INFORMATION {

  LONG    Bias;

  WCHAR   StandardName[ 32 ];

  SYSTEMTIME StandardDate;

  LONG    StandardBias;

  WCHAR   DaylightName[ 32 ];

  SYSTEMTIME DaylightDate;

  LONG    DaylightBias;

} TIME_ZONE_INFORMATION, *PTIME_ZONE_INFORMATION;

C#:

[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)]

struct TimeZoneInformation

{

  public int bias;

  [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 32)]

  public string standardName;

  SystemTime standardDate;

  public int standardBias;

  [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 32)]

  public string daylightName;

  SystemTime daylightDate;

  public int daylightBias;

}

具有回调的函数

Win32:

BOOL EnumDesktops(

 HWINSTA hwinsta,       // 窗口实例的句柄

 DESKTOPENUMPROC lpEnumFunc, // 回调函数

 LPARAM lParam        // 用于回调函数的值

);

回调函数DESKTOPENUMPROC的声明:

BOOL CALLBACK EnumDesktopProc(

 LPTSTR lpszDesktop, // 桌面名称

 LPARAM lParam    // 用户定义的值

);

C#:

将回调函数的声明转化为委托:

delegate bool EnumDesktopProc(

 [MarshalAs(UnmanagedType.LPTStr)]

  string desktopName,

  int lParam);

该函数在C#中的声明:

[DllImport("user32.dll", CharSet = CharSet.Auto)]
static extern bool EnumDesktops(
  IntPtr windowStation,
  EnumDesktopProc callback,
  int lParam);

该表对C#中调用win32函数,以及c++编写的dll时参数及返回值的转换做了一个小的总结,如果想进一步了解这方面内容的话,可以参照msdn中“互操作封送处理”一节。

C#调用dll时的类型转换总结的更多相关文章

  1. 外壳exe通过反射调用dll时

    外壳exe通过反射调用dll时,dll是 4.0的框架,外壳exe也需要编译成4.0的框架,如果dll本身有调用32位的dll,那么外壳exe也需要编译成32位. 调试时报的那个错,直接继续运行,不影 ...

  2. 当程序调用dll时获取dll路径,DLL中获取自身的句柄

    当程序调用dll时,获取dll路径的方法: HMODULE hMod = GetModuleHandle(_T("axload.dll")); if (hMod != NULL) ...

  3. C++ exe调用dll文件

    生成dll程序 extern "C"_declspec(dllexport) void maopao(int *p,int count);void maopao(int *p,in ...

  4. 在VC中创建并调用DLL

    转自:http://express.ruanko.com/ruanko-express_45/technologyexchange6.html 一.DLL简介 1.什么是DLL? 动态链接库英文为DL ...

  5. 在VS2012中采用C++中调用DLL中的函数 (4)

    这两天因为需要用到VS2012来生成一个DLL代码,但是之前并没有用过DLL相关的内容,从昨天开始尝试调试DLL的文件调用,起初笔者在网络上找到了3片采用VSXXX版本进行调试的例子,相关的内容见本人 ...

  6. 【原创】在VS2012中采用C++中调用DLL中的函数(4)

    这两天因为需要用到VS2012来生成一个DLL代码,但是之前并没有用过DLL相关的内容,从昨天开始尝试调试DLL的文件调用,起初笔者在网络上找到了3片采用VSXXX版本进行调试的例子,相关的内容见本人 ...

  7. VS2013环境生成和调用DLL动态链接库

    http://blog.csdn.net/u010273652/article/details/25514577 创建动态库方法: 创建动态库是生成 .dll .lib 两个个文件 文件 -> ...

  8. 在VS2012中采用C++中调用DLL中的函数(4)

    转自:http://www.cnblogs.com/woshitianma/p/3683495.html 这两天因为需要用到VS2012来生成一个DLL代码,但是之前并没有用过DLL相关的内容,从昨天 ...

  9. VS2013 c++ 生成和调用DLL动态链接库(.def 方法已验证OK)

    转载:https://blog.csdn.net/zhunianguo/article/details/52294339 .def 方法 创建动态库方法: 创建动态库是生成 .dll .lib 两个个 ...

随机推荐

  1. 如何让自己的电脑发布ASP http://jingyan.baidu.com/article/19192ad853224ce53f570748.html

    怎样在WIN7系统下安装IIS | 浏览:122821 | 更新:2012-03-03 14:07 | 标签:windows7 1 2 3 4 5 6 7 分步阅读 在此根据多年的网站开发经验,把如何 ...

  2. getDefinitionByName与ApplicationDomain.getDefinition

    主swf 定义:MC,被加载swf 定义:MC.MC1 ①父SWF的应用程序域的新建子域 (默认方式)称其为应用程序域的继承 var app:ApplicationDomain=new Applica ...

  3. 配置文件struts2Struts2配置文件模块化包含(include)与action总结

    本文是一篇关于配置文件struts2的帖子 <include>标签 当Struts配置文件比较多,需要模块化分别或分开成为多个配置文件时,这个功能比较好. 则需要使用<include ...

  4. poj 1087 C - A Plug for UNIX 网络流最大流

    C - A Plug for UNIXTime Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://acm.hust.edu.cn/vjudge/contes ...

  5. Android短信彩信收发流程(应用层)

    下图为ComposeMessageActivity中confirmSendMessageIfNeeded部分的信息发送流程.主要以接收者有效性的确认为主,然后转向sendMessage方法进行发送. ...

  6. MongoDB 主从复制小实验

    MongoDB 主从复制小实验 操作环境描述:WIN8  64位操作系统,内装虚拟机为CentOS 5.5 32位系统. 操作描述:跟其他关系型数据库类似,在主库进行数据操作,将数据同步到从节点,从节 ...

  7. 用perl对字符串进行全角转半角操作

    #全角转半角 #ljl use Encode; sub CheckSBC { my ($name)=@_; my @character; my $tmpname=$name; my $headname ...

  8. RedHat Enterprise Linux下配置yum源(尝试过的可行方案)

    转自:http://bbs.51cto.com/thread-861410-1.html 一.在linux 6.1中本地yum源配置:首先编辑yum源配置文件我们可以再这个目录中新创建一个配置文件,v ...

  9. log4net的应用

    1.下载log4net 2编写log4net的配置文件 <?xml version="1.0" encoding="utf-8" ?> <co ...

  10. Helpers\Password

    Helpers\Password The password class uses php 5 password_ functions. To create a hash of a password, ...