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/ 给结构体指针分配内存 但在很多 ...
随机推荐
- CSS实现垂直居中的5种方法
利用 CSS 来实现对象的垂直居中有许多不同的方法,比较难的是选择那个正确的方法.我下面说明一下我看到的好的方法和怎么来创建一个好的居中网站. 使用 CSS 实现垂直居中并不容易.有些方法在一些浏览器 ...
- Html菜鸡大杂烩
<!DOCTYPE html> <html> <head lang="en"> <meta charset="UTF-8&quo ...
- android 编译
编译 Android完全编译,耗时 1 小时 25 分$ make编译当前目录下的模块,耗时 1 小时 31 分mm编译指定目录下的模块mmm 模块的根目录清除上次编译输出make clean单独编译 ...
- 利用IIS管理器模拟CDN
CDN(Content Delivery Network,内容分发网络).其含义,在百度百科上是这么写的:CDN 是构建在数据网络上的一种分布式的内容分发网.CDN 的作用是采用流媒体服务器集群技术, ...
- ViewPager和Fragment的结合使用fragment里包含着listview的常见问题
在我们开发的过程中可能会遇到类似需求,我们需要做一个左右滑动的tab导航,这个时候有些朋友可能会想到viewpager和fragment的结合来实现这个功能:当然实现的方法不单单着一种.我们这个随笔讨 ...
- Matlab 进阶学习记录
最近在看 Faster RCNN的Matlab code,发现很多matlab技巧,在此记录: 1. conf_proposal = proposal_config('image_means', ...
- 注册noip.com动态域名的步骤
1.先打开这个网址 http://www.noip.com/ 2.点击页面右上绿色的Sing Up,进行注册 3.会出现这个页面,把这些填上 Create my hostname later这一项不打 ...
- Javascript模式(第三章字面量与构造函数)------读书笔记
一 对象字面量 1.1对象字面量的语法 1,对象键值对哈希表,在其他的编程语言中称之为“关联数组”, 2 键值对里面的值,可以是原始类型也可以是其他类型的对象,称之为属性,函数称之为方法 3 自定义对 ...
- 自动化运维:flask-bootstrap + highstock整合
1.在flask-bootstrap的base.html模板中加载highstock.js 以下是base.html的源码中,调用js文件的例子. 文件路径:python2.7/site-p ...
- WinForm中使用XML文件存储用户配置及操作本地Config配置文件(zt)
因项目中采用CS结构读取Web.config文件,故参照一下的连接完成此功能,在此感谢原作者! 原文地址: http://www.cnblogs.com/zfanlong1314/p/3623622. ...