C#的互操作性:缓冲区、结构、指针
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Runtime.InteropServices; namespace Interop
{
class Program
{
[DllImport("kernel32.dll", EntryPoint = "Beep")]
public static extern bool MyBeep(uint iFreq, uint iDuration);
//HMODULE WINAPI LoadLibrary( _In_ LPCTSTR lpFileName);
[DllImport("kernel32.dll")] public static extern IntPtr LoadLibrary(string dllName);
delegate int deleMessageBox(IntPtr hWnd, string text, string caption, uint type);
//GetProcAddress函数检索指定的动态链接库(DLL)中的输出库函数地址。
//FARPROC GetProcAddress(
// HMODULE hModule, // DLL模块句柄
// LPCSTR lpProcName // 函数名
// );
[DllImport("kernel32.dll")]
public static extern IntPtr GetProcAddress(IntPtr hModule, string procName);
//CharSet = CharSet.Auto
[DllImport("user32.dll", CharSet = CharSet.Auto)]
public static extern int MessageBox(IntPtr hWnd, string text, string caption, uint type);
//DWORD GetCurrentDirectory(DWORD nBufferLength, //sizeofdirectorybuffer
//LPTSTR lpBuffer //directorybuffer
//);
[DllImport("kernel32.dll", CharSet = CharSet.Auto)]
public static extern int GetCurrentDirectory(int BufferLength, System.Text.StringBuilder lpBuffer);
//LPSTR GetCommandLine()
[DllImport("kernel32.dll", CharSet = CharSet.Auto)]
public static extern System.IntPtr GetCommandLine();
//结构体
//typedef struct{
// int wStructSize;
// int x;
// int y;
// int dx;
// int dy;
// int wMax;
// TCHAR rgchMember[2];
//}HELPWININFO;
[StructLayout(LayoutKind.Sequential)]
public struct HELPWININFO
{
int wStructSize;
int x;
int y;
int dx;
int dy;
int wMax;
[MarshalAs(UnmanagedType.ByValArray, SizeConst = )]
public char[] rgchMember;
}
static void Main(string[] args)
{
MyBeep(, );
//函数需要修改内存缓冲区,必须用StringBuilder,因为String类型是只读的
StringBuilder sb = new StringBuilder();
GetCurrentDirectory(, sb);
Console.WriteLine(sb);
//使用IntPtr类将返回的字符串保存到string中
IntPtr ptr = GetCommandLine();
string cmdline = Marshal.PtrToStringAuto(ptr);
Console.WriteLine(cmdline);
//GetProcAddress
IntPtr ptrKernel32 = LoadLibrary("user32.dll");
IntPtr ptrProcMessageBox = GetProcAddress(ptrKernel32, "MessageBoxA");
deleMessageBox messageBox = Marshal.GetDelegateForFunctionPointer(ptrProcMessageBox, typeof(deleMessageBox)) as deleMessageBox;
messageBox(IntPtr.Zero, @"public static extern IntPtr GetProcAddress(IntPtr hModule, string procName);", "LoadLibrary", 0x40);
MessageBox(IntPtr.Zero, "Content Here!", "Caption", 0x40);
}
}
}
C#的互操作性:缓冲区、结构、指针的更多相关文章
- Delphi 记录类型- 结构指针
unit Unit1; interface uses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms ...
- typedef struct LNode命名结构指针(线性表的链式存储)
一.typedef 关键字 1. 简介: typedef工具是一个高级数据特性,利用typedef可以为某一些类型自定义名称. 2. 工作原理: 例如我们定义链表的存储结构时,需要定义结点的存储数据元 ...
- c++ 结构指针和双向链表
结构指针 为结构指针动态分配内存 结构中的结构 双向链表 结构指针 struct mytime { //char name[256]; int hour;//时 int min; //分 i ...
- 指向结构的指针 struct结构名称 *结构指针变量名
//指向结构的指针 struct结构名称 *结构指针变量名 //(*结构指针变量名).成员变量名//结构指针变量->成员变量名 1 #include<stdio.h> 2 #incl ...
- DS实验题 Old_Driver UnionFindSet结构 指针实现邻接表存储
题目见前文:DS实验题 Old_Driver UnionFindSet结构 这里使用邻接表存储敌人之间的关系,邻接表用指针实现: // // main.cpp // Old_Driver3 // // ...
- C语言笔记--传递结构指针以及值传递,址传递
#include <stdio.h> #include <windows.h> #include <mmsystem.h> #include <string. ...
- 【C语言入门教程】7.3 结构体指针的定义和引用
C 语言中指针的操作非常灵活,它也能指向结构体变量对结构体变量进行操作.在学习结构指针之前,需要再次加深对指针的认识.声明指针变量时所使用的数据类型修饰符实际上的作用是定义指针访问内存的范围,如果指针 ...
- C语言 文件操作4--文件结构体FILE的理解以及缓冲区再讲
//文件结构体FILE的理解以及缓冲区再讲 #include<stdio.h> #include<stdlib.h> //要点:文件结构 //struct _iobuf { / ...
- (八)C语言结构体和指针
指针也可以指向一个结构体变量.定义的一般形式为: struct 结构体名 *变量名; 前面已经定义了一个结构体 stu: struct stu { char *name; int num; char ...
- Delphi 给结构体指针分配内存,用new(p),释放用dispose(p)
来自:http://blog.163.com/zhangzhifeng688%40126/blog/static/1652627582010102261748481/ 给结构体指针分配内存 但在很多 ...
随机推荐
- h5移动端-1
iphone3 : 设备分辨率 : 320*480 屏幕分辨率 : 320*480 iphone4 : 设备分辨率 : 320*480 屏幕分辨率 : 640*960 iphone5 : 设备分辨率 ...
- 如何处理json数据
1. 前台处理方式之一: ★jQuery.parseJSON(json) var parsej = $.parseJSON(data); ...
- Unix系统小知识(转)
Unix操作系统的小知识 2.VI添加行号/翻页/清屏 .在对话模式时(即输完Esc再输入: ),输入“:set number”可以将编辑的文本加上行号.跟玩俄罗斯方块一样方便的上下左右移动箭头的快捷 ...
- CE驱动动态加载卸载
加载: #define DEV_KEY TEXT("Drivers\\Builtin\\WCDMA") BOOL CGPRSCTRLDlg::Load() { //DWORD dw ...
- js问题
1.原型链问题 1.js中万物皆对象,但对象也分为普通对象和函数对象,Object,Function都是js自带的函数对象,凡是通过 new Function() 创建的对象都是函数对象,其他的都是普 ...
- 【PKU1001】Exponentiation(高精度乘法)
Exponentiation Time Limit: 500MS Memory Limit: 10000K Total Submissions: 145642 Accepted: 35529 ...
- CSS中的class与id区别及用法
转自http://www.divcss5.com/rumen/r3.shtml及http://www.jb51.net/css/35927.html 我们平常在用DIV CSS制作Xhtml网页页面时 ...
- kickstart+ftp+tftp+dhcp+PXE
##########yum less install.log #看安装log yum install system-config-kickstart* -y yum install tftp* -y ...
- Hibernate的save()和persist()的区别
hibernate之所以提供与save()功能几乎完全类似的persist()方法,一方面是为了照顾JPA的用法习惯.另一方面,save()和 persist()方法还有一个区别:使用 save() ...
- Oracle索引碎片检查及定期重建常用表的索引
背景说明: 今天查阅书籍时,偶然间发现“在对某个索引行执行删除操作时,只是为该行增加了一个删除标记,这个索引行并不会释放它的存储空间,Insert产生的新的索引行也不能被插入到该位置.索引列的修改过程 ...