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/ 给结构体指针分配内存 但在很多 ...
随机推荐
- delegate和protocol
协议和代理对于一个新手来说确实不讨好理解,也有很多的iOS开发的老手对此是懂非懂的.网上的很多博文只是讲了怎么使用,并没有说的很明白.下面我谈一下我的理解. 1.你要先搞明白,协议和代理为什么会出现, ...
- WebAPI的Get和Post访问
/// <summary> /// MVC 调用webapi接口返回框架快捷按钮和一级菜单(Get) /// </summary> /// <param name=&qu ...
- 一些对新手有用的css技巧
一.表单部分 1.禁止textarea文本域的缩放 resize:none; 2.去除初始化textarea下拉条 overflow:auto; 3.如何让表单中的选项按钮,点击文字也能选中? < ...
- 匈牙利算法 DFS模板(了解度+1)
//算法核心是求最大匹配数 #include<bits/stdc++.h> #include<iostream> #include<cstdio> #include ...
- outscan 一键批量 get struct2 devMode (CNVD-2016-04656)
之前写的一个玩意 下载地址:http://pan.baidu.com/s/1i5jmEwP 密码:v8v3 一键批量 get struct2 devMode 支持百度.google(google有访问 ...
- win10突然不能使用usb大容量存储设备(移动硬盘)的解决方法
昨天开始使用usb硬盘,发现一块无法识别,一块识别好了以后不能打开. 可能是之前一次系统更新有bug,但是一直也没有用移动硬盘,所以没有发现. 开始尝试各种方案,已经尝试过并且无效的有以下几个: 1, ...
- MongoDB学习笔记七:管理
[启动和停止MongoDB]『从命令行启动』执行mongod,启动MongoDB服务器.mongod有很多可配置的启动选项:在命令行运行mongod --help可以查看所有选项.一些主要选项如下: ...
- centos6.3环境下升级python及MySQLdb的安装
近来突然想鼓捣下linux下的python,看下Python数据库方面的东西,想着在centos下测试下.然而安装的过程有很多坑.下面对整个流程进行下记录 1.python基本库的安装 在安装pyth ...
- golang调用c++的dll库文件
最近使用golang调用c++的dll库文件,简单了解了一下,特作此笔记:一.DLL 的编制与具体的编程语言及编译器无关 dll分com的dll和动态dll,Com组件dll:不管是何种语言写的都可以 ...
- SQLServer 数据库查看死锁、堵塞的SQL语句
http://www.cnblogs.com/zhuque/archive/2012/11/08/2763343.html 死锁和堵塞一直是性能测试执行中关注的重点. 下面是我整理的监控sql ser ...