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#的互操作性:缓冲区、结构、指针的更多相关文章

  1. Delphi 记录类型- 结构指针

    unit Unit1; interface uses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms ...

  2. typedef struct LNode命名结构指针(线性表的链式存储)

    一.typedef 关键字 1. 简介: typedef工具是一个高级数据特性,利用typedef可以为某一些类型自定义名称. 2. 工作原理: 例如我们定义链表的存储结构时,需要定义结点的存储数据元 ...

  3. c++ 结构指针和双向链表

    结构指针 为结构指针动态分配内存 结构中的结构 双向链表 结构指针 struct mytime { //char name[256]; int hour;//时 int min; //分 i ...

  4. 指向结构的指针 struct结构名称 *结构指针变量名

    //指向结构的指针 struct结构名称 *结构指针变量名 //(*结构指针变量名).成员变量名//结构指针变量->成员变量名 1 #include<stdio.h> 2 #incl ...

  5. DS实验题 Old_Driver UnionFindSet结构 指针实现邻接表存储

    题目见前文:DS实验题 Old_Driver UnionFindSet结构 这里使用邻接表存储敌人之间的关系,邻接表用指针实现: // // main.cpp // Old_Driver3 // // ...

  6. C语言笔记--传递结构指针以及值传递,址传递

    #include <stdio.h> #include <windows.h> #include <mmsystem.h> #include <string. ...

  7. 【C语言入门教程】7.3 结构体指针的定义和引用

    C 语言中指针的操作非常灵活,它也能指向结构体变量对结构体变量进行操作.在学习结构指针之前,需要再次加深对指针的认识.声明指针变量时所使用的数据类型修饰符实际上的作用是定义指针访问内存的范围,如果指针 ...

  8. C语言 文件操作4--文件结构体FILE的理解以及缓冲区再讲

    //文件结构体FILE的理解以及缓冲区再讲 #include<stdio.h> #include<stdlib.h> //要点:文件结构 //struct _iobuf { / ...

  9. (八)C语言结构体和指针

    指针也可以指向一个结构体变量.定义的一般形式为: struct 结构体名 *变量名; 前面已经定义了一个结构体 stu: struct stu { char *name; int num; char ...

  10. Delphi 给结构体指针分配内存,用new(p),释放用dispose(p)

    来自:http://blog.163.com/zhangzhifeng688%40126/blog/static/1652627582010102261748481/ 给结构体指针分配内存  但在很多 ...

随机推荐

  1. 一个大数据的demo

    package test.admin; import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.Fil ...

  2. UVa 673 平衡的括号

    题意:给出包含"()"和"[]"的括号序列,判断是否合法. 用栈来完成,注意空串就行. #include<iostream> #include< ...

  3. CentOS上yum安装JDK

    转: http://blog.csdn.net/onepiecehuiyu/article/details/17189571

  4. 使用"立即执行函数"(Immediately-Invoked Function Expression,IIFE)

    一.原始写法 模块就是实现特定功能的一组方法. 只要把不同的函数(以及记录状态的变量)简单地放在一起,就算是一个模块. function m1(){ //... } function m2(){ // ...

  5. C语言字符串操作总结大全(超详细)

    本篇文章是对C语言字符串操作进行了详细的总结分析,需要的朋友参考下 1)字符串操作  strcpy(p, p1) 复制字符串  strncpy(p, p1, n) 复制指定长度字符串  strcat( ...

  6. 自定义滚动条 - mCustomScrollbar

    项目中需要使用自定义滚动条,但是试用了很多都功能不够全,今天发现一个比较全而且用法很简单的 -- mCustomScrollbar http://manos.malihu.gr/jquery-cust ...

  7. python之错误和异常

    错误 分为语法错误和逻辑错误,如下: 语法错误指示软件的结构上有错误,导致不能被解释器解释或编译器编译. 逻辑错误可能是由于不完整或是不合法的输入所致,或者是无法生成.计算.或是输出结果需要的过程无法 ...

  8. 得到APP【每天听本书】微信交流群(每天更新)

    得到APP[每天听本书]微信交流群,每天更新下载学习资料 添加个人微信号:zhidu10000 进入微信群. “坚持每天读一本书,每天进步1.01,每年进步37.8倍” 2016年书单合辑点此链接,查 ...

  9. Kylin(三): Saiku

    Saiku是一个轻量级的OLAP分析引擎,可以方便的扩展.嵌入和配置.Saiku通过REST API连接OLAP系统,利用其友好的界面为用户提供直观的分析数据的方式,它是基于jQuery做的前端界面. ...

  10. C++中typename和class的区别

    C++中typename和class的区别 在c++Template中很多地方都用到了typename与class这两个关键字,而且好像可以替换,是不是这两个关键字完全一样呢? 相信学习C++的人对c ...