(转载)C#调用非托管Win 32 DLL
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了(转载)C#调用非托管Win 32 DLL,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含28065字,纯文字阅读大概需要41分钟。
内容图文
![(转载)C#调用非托管Win 32 DLL](/upload/InfoBanner/zyjiaocheng/1233/a5ff98b6b9fb4e87a316eeec8d5e5543.jpg)
转载学习收藏,原文地址http://www.cnblogs.com/mywebname/articles/2291876.html
背景
在项目过程中,有时候你需要调用非C#编写的DLL文件,尤其在使用一些第三方通讯组件的时候,通过C#来开发应用软件时,就需要利用DllImport特性进行方法调用。本篇文章将引导你快速理解这个调用的过程。
步骤
1. 创建一个CSharpInvokeCPP的解决方案:
2. 创建一个C++的动态库项目:
3. 在应用程序设置中,选择“DLL”,其他按照默认选项:
最后点击完成,得到如图所示项目:
我们可以看到这里有一些文件,其中dllmain.cpp作为定义DLL应用程序的入口点,它的作用跟exe文件有个main或者WinMain入口函数是一样的,它就是作为DLL的一个入口函数,实际上它是个可选的文件。它是在静态链接时或动态链接时调用LoadLibrary和FreeLibrary时都会被调用。详细内容可以参考(http://blog.csdn.net/benkaoya/archive/2008/06/02/2504781.aspx)。
4. 现在我们打开CSharpInvokeCPP.CPPDemo.cpp文件:
现在我们加入以下内容:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
|
// CSharpInvokeCPP.CPPDemo.cpp : 定义 DLL 应用程序的导出函数。
//
#include "stdafx.h"
extern
"C"
__declspec
(
dllexport
)
int
Add(
int
x,
int
y)
{
return
x + y;
}
extern
"C"
__declspec
(
dllexport
)
int
Sub(
int
x,
int
y)
{
return
x - y;
}
extern
"C"
__declspec
(
dllexport
)
int
Multiply(
int
x,
int
y)
{
return
x * y;
}
extern
"C"
__declspec
(
dllexport
)
int
Divide(
int
x,
int
y)
{
return
x / y;
}
|
extern "C" 包含双重含义,从字面上即可得到:首先,被它修饰的目标是“extern”的;其次,被它修饰的目标是“C”的。而被extern "C"修饰的变量和函数是按照C语言方式编译和连接的。
__declspec(dllexport)的目的是为了将对应的函数放入到DLL动态库中。
extern "C" __declspec(dllexport)加起来的目的是为了使用DllImport调用非托管C++的DLL文件。因为使用DllImport只能调用由C语言函数做成的DLL。
5. 编译项目程序,最后在Debug目录生成CSharpInvokeCPP.CPPDemo.dll和CSharpInvokeCPP.CPPDemo.lib
我们用反编译工具PE Explorer查看下该DLL里面的方法:
可以发现对外的公共函数上包含这四种“加减乘除”方法。
6. 现在来演示下如何利用C#项目来调用非托管C++的DLL,首先创建C#控制台应用程序:
7. 在CSharpInvokeCSharp.CSharpDemo项目上新建一个CPPDLL类,编写以下代码:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
public
class
CPPDLL
{
[DllImport(
"CSharpInvokeCPP.CPPDemo.dll"
)]
public
static
extern
int
Add(
int
x,
int
y);
[DllImport(
"CSharpInvokeCPP.CPPDemo.dll"
)]
public
static
extern
int
Sub(
int
x,
int
y);
[DllImport(
"CSharpInvokeCPP.CPPDemo.dll"
)]
public
static
extern
int
Multiply(
int
x,
int
y);
[DllImport(
"CSharpInvokeCPP.CPPDemo.dll"
)]
public
static
extern
int
Divide(
int
x,
int
y);
}
|
DllImport作为C#中对C++的DLL类的导入入口特征,并通过static extern对extern “C”进行对应。
8. 另外,记得把CPPDemo中生成的DLL文件拷贝到CSharpDemo的bin目录下,你也可以通过设置【项目属性】->【配置属性】->【常规】中的输出目录:
这样编译项目后,生成的文件就自动输出到CSharpDemo中了。
9. 然后在Main入口编写测试代码:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
|
static
void
Main(
string
[] args)
{
int
result = CPPDLL.Add(10, 20);
Console.WriteLine(
"10 + 20 = {0}"
, result);
result = CPPDLL.Sub(30, 12);
Console.WriteLine(
"30 - 12 = {0}"
, result);
result = CPPDLL.Multiply(5, 4);
Console.WriteLine(
"5 * 4 = {0}"
, result);
result = CPPDLL.Divide(30, 5);
Console.WriteLine(
"30 / 5 = {0}"
, result);
Console.ReadLine();
}
|
运行结果:
方法得到调用。
10. 以上的方法只能通过静态方法对于C++中的函数进行调用。那么怎样通过静态方法去调用C++中一个类对象中的方法呢?现在我在CPPDemo项目中添加一个头文件userinfo.h:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
class
UserInfo {
private
:
char
* m_Name;
int
m_Age;
public
:
UserInfo(
char
* name,
int
age)
{
m_Name = name;
m_Age = age;
}
virtual
~UserInfo(){ }
int
GetAge() {
return
m_Age; }
char
* GetName() {
return
m_Name; }
};
|
在CSharpInvokeCPP.CPPDemo.cpp中,添加一些代码:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
|
#include "malloc.h"
#include "userinfo.h"
typedef
struct
{
char
name[32];
int
age;
} User;
UserInfo* userInfo;
extern
"C"
__declspec
(
dllexport
) User* Create(
char
* name,
int
age)
{
User* user = (User*)
malloc
(
sizeof
(User));
userInfo =
new
UserInfo(name, age);
strcpy
(user->name, userInfo->GetName());
user->age = userInfo->GetAge();
return
user;
}
|
这里声明一个结构,包括name和age,这个结构是用于和C#方面的结构作个映射。
注意:代码中的User*是个指针,返回也是一个对象指针,这样做为了防止方法作用域结束后的局部变量的释放。
strcpy是个复制char数组的函数。
11. 在CSharpDemo项目中CPPDLL类中补充代码:
1
2
3
4
5
6
7
8
9
10
11
|
[DllImport(
"CSharpInvokeCPP.CPPDemo.dll"
)]
public
static
extern
IntPtr Create(
string
name,
int
age);
[StructLayout(LayoutKind.Sequential)]
public
struct
User
{
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 32)]
public
string
Name;
public
int
Age;
}
|
其中这里的结构User就和C++中的User对应。
12. 在Program.cs中补充代码:
1
2
3
|
IntPtr ptr = CPPDLL.Create(
"李平"
, 27);
<STRONG><FONT color=#ff0000>CPPDLL.User user = (CPPDLL.User)Marshal.PtrToStructure(ptr,
typeof
(CPPDLL.User));</FONT></STRONG>
Console.WriteLine(
"Name: {0}, Age: {1}"
, user.Name, user.Age);
|
注意:红色字体部分,这里结构指针首先转换成IntPtr句柄,然后通过Marshal.PtrToStructrue转换成你所需要的结构。
运行结果:
转自liping13599168/archive/2011/03/31/2000320.html
以下是调用时的一些类型转换等注意事项:
C++
<SPAN style=‘font-family: "Times New Roman"; font-size: 10.5pt;‘
lang="EN-US"> |
<SPAN style=‘line-height: 21px; font-family: "Times New Roman";‘>C++(Win 32)
<SPAN style=‘line-height: 21px; font-family: "Times New Roman";‘>C#
<SPAN style=‘line-height: 21px; font-family: "Times New Roman";‘>char**
作为输入参数转为 <SPAN style=‘line-height: 19px; font-family: "Times New Roman";‘>char[] , 通过 Encoding 类对这个 string[] 进行编码后得到的一个 char[]
作为输出参数转为 <SPAN style=‘line-height: 19px; font-family: "Times New Roman";‘>byte[] , 通过 Encoding 类对这个 byte[] 进行解码,得到字符串
<SPAN style=‘line-height: 19px; font-family: "Times New Roman";‘>C++ Dll 接口:
<SPAN style=‘line-height: 21px; font-family: "Times New Roman";‘>void CplusplusToCsharp(in char** AgentID, out char** AgentIP);
<SPAN style=‘line-height: 19px; font-family: "Times New Roman";‘>C# 中的声明:
<SPAN style=‘line-height: 21px; font-family: "Times New Roman";‘>[DllImport("Example.dll")]
<SPAN style=‘line-height: 21px; font-family: "Times New Roman";‘>public static extern void CplusplusToCsharp(char[] AgentID, byte[] AgentIP);
<SPAN style=‘line-height: 19px; font-family: "Times New Roman";‘>C# 中的调用:
<SPAN style=‘line-height: 21px; font-family: "Times New Roman";‘>Encoding encode = Encoding.Default;
<SPAN style=‘line-height: 21px; font-family: "Times New Roman";‘>byte[] tAgentID;
<SPAN style=‘line-height: 21px; font-family: "Times New Roman";‘>byte[] tAgentIP;
<SPAN style=‘line-height: 21px; font-family: "Times New Roman";‘>string[] AgentIP;
<SPAN style=‘line-height: 21px; font-family: "Times New Roman";‘>tAgentID = new byte[100];
<SPAN style=‘line-height: 21px; font-family: "Times New Roman";‘>tAgentIP = new byte[100];
<SPAN style=‘line-height: 21px; font-family: "Times New Roman";‘>CplusplusToCsharp(encode.GetChars(tAgentID), tAgentIP);
<SPAN style=‘line-height: 21px; font-family: "Times New Roman";‘>AgentIP[i] = encode.GetString(tAgentIP,i*Length,Length);
<SPAN style=‘line-height: 21px; font-family: "Times New Roman";‘>Handle
<SPAN style=‘line-height: 21px; font-family: "Times New Roman";‘>IntPtr
<SPAN style=‘line-height: 21px; font-family: "Times New Roman";‘>Hwnd
<SPAN style=‘line-height: 21px; font-family: "Times New Roman";‘>IntPtr
<SPAN style=‘line-height: 21px; font-family: "Times New Roman";‘>int*
<SPAN style=‘line-height: 21px; font-family: "Times New Roman";‘>ref int
<SPAN style=‘line-height: 21px; font-family: "Times New Roman";‘>int&
<SPAN style=‘line-height: 21px; font-family: "Times New Roman";‘>ref int
<SPAN style=‘line-height: 21px; font-family: "Times New Roman";‘>void*
<SPAN style=‘line-height: 21px; font-family: "Times New Roman";‘>IntPtr
<SPAN style=‘line-height: 21px; font-family: "Times New Roman";‘>unsigned char*
<SPAN style=‘line-height: 21px; font-family: "Times New Roman";‘>ref byte
<SPAN style=‘line-height: 21px; font-family: "Times New Roman";‘>BOOL
<SPAN style=‘line-height: 21px; font-family: "Times New Roman";‘>bool
<SPAN style=‘line-height: 21px; font-family: "Times New Roman";‘>DWORD
<SPAN style=‘line-height: 19px; font-family: "Times New Roman";‘>int 或 <SPAN style=‘line-height: 19px; font-family: "Times New Roman";‘> uint ( <SPAN style=‘line-height: 19px; font-family: "Times New Roman";‘>int 更常用一些)
枚举类型
<SPAN style=‘line-height: 19px; font-family: "Times New Roman";‘>Win32 :
<SPAN style=‘line-height: 19px; font-family: "Times New Roman";‘>BOOL MessageBeep(UINT uType // 声音类型 <SPAN style=‘line-height: 19px; font-family: "Times New Roman";‘>); 其中的声音类型为枚举类型中的某一值。
<SPAN style=‘line-height: 19px; font-family: "Times New Roman";‘>C# :
用户需要自己定义一个枚举类型:
<SPAN style=‘line-height: 21px; font-family: "Times New Roman";‘>public enum BeepType
<SPAN style=‘line-height: 21px; font-family: "Times New Roman";‘>{
<SPAN style=‘line-height: 19px; font-family: "Times New Roman";‘> SimpleBeep = -1,
<SPAN style=‘line-height: 19px; font-family: "Times New Roman";‘> IconAsterisk = 0x00000040,
<SPAN style=‘line-height: 19px; font-family: "Times New Roman";‘> IconExclamation = 0x00000030,
<SPAN style=‘line-height: 19px; font-family: "Times New Roman";‘> IconHand = 0x00000010,
<SPAN style=‘line-height: 19px; font-family: "Times New Roman";‘> IconQuestion = 0x00000020,
<SPAN style=‘line-height: 19px; font-family: "Times New Roman";‘> Ok = 0x00000000,
<SPAN style=‘line-height: 21px; font-family: "Times New Roman";‘>}
<SPAN style=‘line-height: 19px; font-family: "Times New Roman";‘>C# 中导入该函数:
<SPAN style=‘line-height: 21px; font-family: "Times New Roman";‘>[DllImport("user32.dll")]
<SPAN style=‘line-height: 21px; font-family: "Times New Roman";‘>public static extern bool MessageBeep(BeepType beepType);
<SPAN style=‘line-height: 19px; font-family: "Times New Roman";‘>C# 中调用该函数:
<SPAN style=‘line-height: 21px; font-family: "Times New Roman";‘>MessageBeep(BeepType.IconQuestion);
结构类型
<SPAN style=‘line-height: 19px; font-family: "Times New Roman";‘>Win32 :
使用结构指针作为参数的函数:
<SPAN style=‘line-height: 21px; font-family: "Times New Roman";‘>BOOL GetSystemPowerStatus(
<SPAN style=‘line-height: 19px; font-family: "Times New Roman";‘>LPSYSTEM_POWER_STATUS lpSystemPowerStatus
<SPAN style=‘line-height: 21px; font-family: "Times New Roman";‘>);
<SPAN style=‘line-height: 19px; font-family: "Times New Roman";‘>Win32 中该结构体的定义:
<SPAN style=‘line-height: 21px; font-family: "Times New Roman";‘>typedef struct _SYSTEM_POWER_STATUS {
<SPAN style=‘line-height: 19px; font-family: "Times New Roman";‘>BYTE <SPAN style=‘line-height: 19px; font-family: "Times New Roman";‘> ACLineStatus;
<SPAN style=‘line-height: 19px; font-family: "Times New Roman";‘>BYTE <SPAN style=‘line-height: 19px; font-family: "Times New Roman";‘> BatteryFlag;
<SPAN style=‘line-height: 19px; font-family: "Times New Roman";‘>BYTE <SPAN style=‘line-height: 19px; font-family: "Times New Roman";‘> BatteryLifePercent;
<SPAN style=‘line-height: 19px; font-family: "Times New Roman";‘>BYTE <SPAN style=‘line-height: 19px; font-family: "Times New Roman";‘> Reserved1;
<SPAN style=‘line-height: 19px; font-family: "Times New Roman";‘>DWORD <SPAN style=‘line-height: 19px; font-family: "Times New Roman";‘>BatteryLifeTime;
<SPAN style=‘line-height: 19px; font-family: "Times New Roman";‘>DWORD <SPAN style=‘line-height: 19px; font-family: "Times New Roman";‘>BatteryFullLifeTime;
<SPAN style=‘line-height: 21px; font-family: "Times New Roman";‘>} SYSTEM_POWER_STATUS, *LPSYSTEM_POWER_STATUS;
<SPAN style=‘line-height: 19px; font-family: "Times New Roman";‘>C# :
用户自定义相应的结构体:
<SPAN style=‘line-height: 21px; font-family: "Times New Roman";‘>struct SystemPowerStatus
<SPAN style=‘line-height: 21px; font-family: "Times New Roman";‘>{
<SPAN style=‘line-height: 19px; font-family: "Times New Roman";‘> byte ACLineStatus;
<SPAN style=‘line-height: 19px; font-family: "Times New Roman";‘> byte batteryFlag;
<SPAN style=‘line-height: 19px; font-family: "Times New Roman";‘> byte batteryLifePercent;
<SPAN style=‘line-height: 19px; font-family: "Times New Roman";‘> byte reserved1;
<SPAN style=‘line-height: 19px; font-family: "Times New Roman";‘> int batteryLifeTime;
<SPAN style=‘line-height: 19px; font-family: "Times New Roman";‘> int batteryFullLifeTime;
<SPAN style=‘line-height: 21px; font-family: "Times New Roman";‘>}
<SPAN style=‘line-height: 19px; font-family: "Times New Roman";‘>C# 中导入该函数:
<SPAN style=‘line-height: 21px; font-family: "Times New Roman";‘>[DllImport("kernel32.dll")]
<SPAN style=‘line-height: 21px; font-family: "Times New Roman";‘>public static extern bool GetSystemPowerStatus(
<SPAN style=‘line-height: 19px; font-family: "Times New Roman";‘> ref SystemPowerStatus systemPowerStatus);
<SPAN style=‘line-height: 19px; font-family: "Times New Roman";‘>C# 中调用该函数:
<SPAN style=‘line-height: 21px; font-family: "Times New Roman";‘>SystemPowerStatus sps;
<SPAN style=‘line-height: 19px; font-family: "Times New Roman";‘>….sps 初始化赋值 <SPAN style=‘line-height: 19px; font-family: "Times New Roman";‘>……
<SPAN style=‘line-height: 21px; font-family: "Times New Roman";‘>GetSystemPowerStatus(ref sps);
字符串
对于字符串的处理分为以下几种情况:
<SPAN style=‘line-height: 21px; font-family: "Times New Roman";‘>1、<SPAN style=‘font: 7pt/normal "Times New Roman"; font-size-adjust: none; font-stretch: normal;‘> 字符串常量指针的处理( <SPAN style=‘line-height: 19px; font-family: "Times New Roman";‘>LPCTSTR ),也适应于字符串常量的处理, <SPAN style=‘line-height: 19px; font-family: "Times New Roman";‘>.net 中的 <SPAN style=‘line-height: 19px; font-family: "Times New Roman";‘>string 类型是不可变的类型。
<SPAN style=‘line-height: 21px; font-family: "Times New Roman";‘>2、<SPAN style=‘font: 7pt/normal "Times New Roman"; font-size-adjust: none; font-stretch: normal;‘> 字符串缓冲区的处理( <SPAN style=‘line-height: 19px; font-family: "Times New Roman";‘>char* ),即对于变长字符串的处理, <SPAN style=‘line-height: 19px; font-family: "Times New Roman";‘>.net 中 <SPAN style=‘line-height: 19px; font-family: "Times New Roman";‘>StringBuilder 可用作缓冲区
<SPAN style=‘line-height: 19px; font-family: "Times New Roman";‘>Win32 :
<SPAN style=‘line-height: 21px; font-family: "Times New Roman";‘>BOOL GetFile(LPCTSTR lpRootPathName);
<SPAN style=‘line-height: 19px; font-family: "Times New Roman";‘>C# :
函数声明:
<SPAN style=‘line-height: 21px; font-family: "Times New Roman";‘>[DllImport("kernel32.dll", CharSet = CharSet.Auto)]
<SPAN style=‘line-height: 21px; font-family: "Times New Roman";‘>static extern bool GetFile (
<SPAN style=‘line-height: 19px; font-family: "Times New Roman";‘>[MarshalAs(UnmanagedType.LPTStr)]
<SPAN style=‘line-height: 19px; font-family: "Times New Roman";‘>string rootPathName);
函数调用:
<SPAN style=‘line-height: 21px; font-family: "Times New Roman";‘>string pathname;
<SPAN style=‘line-height: 21px; font-family: "Times New Roman";‘>GetFile(pathname);
备注:
<SPAN style=‘line-height: 19px; font-family: "Times New Roman";‘>DllImport 中的 <SPAN style=‘line-height: 19px; font-family: "Times New Roman";‘>CharSet 是为了说明自动地调用该函数相关的 <SPAN style=‘line-height: 19px; font-family: "Times New Roman";‘>Ansi 版本或者 <SPAN style=‘line-height: 19px; font-family: "Times New Roman";‘>Unicode 版本
变长字符串处理:
<SPAN style=‘line-height: 19px; font-family: "Times New Roman";‘>C# :
函数声明:
<SPAN style=‘line-height: 21px; font-family: "Times New Roman";‘>[DllImport("kernel32.dll", CharSet = CharSet.Auto)]
<SPAN style=‘line-height: 21px; font-family: "Times New Roman";‘>public static extern int GetShortPathName(
<SPAN style=‘line-height: 19px; font-family: "Times New Roman";‘> [MarshalAs(UnmanagedType.LPTStr)]
<SPAN style=‘line-height: 19px; font-family: "Times New Roman";‘> string path,
<SPAN style=‘line-height: 19px; font-family: "Times New Roman";‘> [MarshalAs(UnmanagedType.LPTStr)]
<SPAN style=‘line-height: 19px; font-family: "Times New Roman";‘> StringBuilder shortPath,
<SPAN style=‘line-height: 19px; font-family: "Times New Roman";‘> int shortPathLength);
函数调用:
<SPAN style=‘line-height: 21px; font-family: "Times New Roman";‘>StringBuilder shortPath = new StringBuilder(80);
<SPAN style=‘line-height: 21px; font-family: "Times New Roman";‘>int result = GetShortPathName(
<SPAN style=‘line-height: 21px; font-family: "Times New Roman";‘>@"d:\test.jpg", shortPath, shortPath.Capacity);
<SPAN style=‘line-height: 21px; font-family: "Times New Roman";‘>string s = shortPath.ToString();
<SPAN style=‘line-height: 21px; font-family: "Times New Roman";‘>struct
具有内嵌字符数组的结构:
<SPAN style=‘line-height: 19px; font-family: "Times New Roman";‘>Win32 :
<SPAN style=‘line-height: 21px; font-family: "Times New Roman";‘>typedef struct _TIME_ZONE_INFORMATION {
<SPAN style=‘line-height: 19px; font-family: "Times New Roman";‘>LONG <SPAN style=‘line-height: 19px; font-family: "Times New Roman";‘> Bias;
<SPAN style=‘line-height: 19px; font-family: "Times New Roman";‘>WCHAR <SPAN style=‘line-height: 19px; font-family: "Times New Roman";‘>StandardName[ 32 ];
<SPAN style=‘line-height: 19px; font-family: "Times New Roman";‘>SYSTEMTIME StandardDate;
<SPAN style=‘line-height: 19px; font-family: "Times New Roman";‘>LONG <SPAN style=‘line-height: 19px; font-family: "Times New Roman";‘> StandardBias;
<SPAN style=‘line-height: 19px; font-family: "Times New Roman";‘>WCHAR <SPAN style=‘line-height: 19px; font-family: "Times New Roman";‘>DaylightName[ 32 ];
<SPAN style=‘line-height: 19px; font-family: "Times New Roman";‘>SYSTEMTIME DaylightDate;
<SPAN style=‘line-height: 19px; font-family: "Times New Roman";‘>LONG <SPAN style=‘line-height: 19px; font-family: "Times New Roman";‘> DaylightBias;
<SPAN style=‘line-height: 21px; font-family: "Times New Roman";‘>} TIME_ZONE_INFORMATION, *PTIME_ZONE_INFORMATION;
<SPAN style=‘line-height: 19px; font-family: "Times New Roman";‘>C# :
<SPAN style=‘line-height: 21px; font-family: "Times New Roman";‘>[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)]
<SPAN style=‘line-height: 21px; font-family: "Times New Roman";‘>struct TimeZoneInformation
<SPAN style=‘line-height: 21px; font-family: "Times New Roman";‘>{
<SPAN style=‘line-height: 19px; font-family: "Times New Roman";‘> public int bias;
<SPAN style=‘line-height: 19px; font-family: "Times New Roman";‘> [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 32)]
<SPAN style=‘line-height: 19px; font-family: "Times New Roman";‘> public string standardName;
<SPAN style=‘line-height: 19px; font-family: "Times New Roman";‘> SystemTime standardDate;
<SPAN style=‘line-height: 19px; font-family: "Times New Roman";‘> public int standardBias;
<SPAN style=‘line-height: 19px; font-family: "Times New Roman";‘> [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 32)]
<SPAN style=‘line-height: 19px; font-family: "Times New Roman";‘> public string daylightName;
<SPAN style=‘line-height: 19px; font-family: "Times New Roman";‘> SystemTime daylightDate;
<SPAN style=‘line-height: 19px; font-family: "Times New Roman";‘> public int daylightBias;
<SPAN style=‘line-height: 21px; font-family: "Times New Roman";‘>}
具有回调的函数
<SPAN style=‘line-height: 19px; font-family: "Times New Roman";‘>Win32 :
<SPAN style=‘line-height: 21px; font-family: "Times New Roman";‘>BOOL EnumDesktops(
<SPAN style=‘line-height: 19px; font-family: "Times New Roman";‘>HWINSTA hwinsta, <SPAN style=‘line-height: 19px; font-family: "Times New Roman";‘> // 窗口实例的句柄
<SPAN style=‘line-height: 19px; font-family: "Times New Roman";‘>DESKTOPENUMPROC lpEnumFunc, <SPAN style=‘line-height: 19px; font-family: "Times New Roman";‘>// 回调函数
<SPAN style=‘line-height: 19px; font-family: "Times New Roman";‘>LPARAM lParam <SPAN style=‘line-height: 19px; font-family: "Times New Roman";‘>// 用于回调函数的值
<SPAN style=‘line-height: 21px; font-family: "Times New Roman";‘>);
回调函数 <SPAN style=‘line-height: 19px; font-family: "Times New Roman";‘>DESKTOPENUMPROC 的声明:
<SPAN style=‘line-height: 21px; font-family: "Times New Roman";‘>BOOL CALLBACK EnumDesktopProc(
<SPAN style=‘line-height: 19px; font-family: "Times New Roman";‘>LPTSTR lpszDesktop, <SPAN style=‘line-height: 19px; font-family: "Times New Roman";‘>// 桌面名称
<SPAN style=‘line-height: 19px; font-family: "Times New Roman";‘>LPARAM lParam <SPAN style=‘line-height: 19px; font-family: "Times New Roman";‘>// 用户定义的值
<SPAN style=‘line-height: 21px; font-family: "Times New Roman";‘>);
<SPAN style=‘line-height: 19px; font-family: "Times New Roman";‘>C# :
将回调函数的声明转化为委托:
<SPAN style=‘line-height: 21px; font-family: "Times New Roman";‘>delegate bool EnumDesktopProc(
<SPAN style=‘line-height: 19px; font-family: "Times New Roman";‘>[MarshalAs(UnmanagedType.LPTStr)]
<SPAN style=‘line-height: 19px; font-family: "Times New Roman";‘> string desktopName,
<SPAN style=‘line-height: 19px; font-family: "Times New Roman";‘> int lParam);
该函数在 <SPAN style=‘line-height: 19px; font-family: "Times New Roman";‘>C# 中的声明:
[DllImport("user32.dll", CharSet = CharSet.Auto)] static extern bool EnumDesktops( IntPtr windowStation, EnumDesktopProc callback, int lParam);
该表对<SPAN style=‘line-height: 21px; font-family: "Times New Roman"; font-size: 10.5pt;‘ lang="EN-US">C#中调用<SPAN style=‘line-height: 21px; font-family: "Times New Roman"; font-size: 10.5pt;‘ lang="EN-US">win32函数,以及<SPAN style=‘line-height: 21px; font-family: "Times New Roman"; font-size: 10.5pt;‘ lang="EN-US">c++编写的<SPAN style=‘line-height: 21px; font-family: "Times New Roman"; font-size: 10.5pt;‘ lang="EN-US">dll时参数及返回值的转换做了一个小的总结,如果想进一步了解这方面内容的话,可以参照<SPAN style=‘line-height: 21px; font-family: "Times New Roman"; font-size: 10.5pt;‘ lang="EN-US">msdn中“互操作封送处理”一节。
内容总结
以上是互联网集市为您收集整理的(转载)C#调用非托管Win 32 DLL全部内容,希望文章能够帮你解决(转载)C#调用非托管Win 32 DLL所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。