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. javax.el.PropertyNotFoundException:

    javax.el.PropertyNotFoundException: Property 'ContextPath' not found on type org.apache.catalina.cor ...

  2. JavaScript:修改作用域外变量

    今天在看JavaScript学习指南的时候做的课后习题,也因此详细的对函数的传入参数进行比较深入的研究. 题目如下: 函数如何才能修改其作用域之外的变量?编写一个函数,由1~5的数字组成的数组作为参数 ...

  3. jQuery 简介

    jQuery 简介 jQuery 库可以通过一行简单的标记被添加到网页中. jQuery 库 - 特性 jQuery 是一个 JavaScript 函数库. jQuery 库包含以下特性: HTML ...

  4. 解决Oracle 11g ORA-01017错误代码

    在oracle的之前版本时, 你的用户名密码是大小写不敏感的, 但在11g中, 数据库默认密码的大小写是敏感的, 于是我们就知道了一个事实, 那就是jdbc在给oracle密码时, 会将其变成大写,真 ...

  5. python_selenium智联搜索

    python_selenium智联搜索 妹子要去招聘会工作,奈何网上仅仅提供招聘会的公司名字,没有提供招聘的职位,SO我写了个小代码给妹子在智联上面搜索职位,由于时间紧迫,前程的就不写了 #!/usr ...

  6. 通过 SMB 直通优化文件服务器的性能

    https://technet.microsoft.com/zh-cn/library/hh831487.aspx Windows Server 2012 内置新增功能,称为 SMB 直通,用来支持使 ...

  7. cuda-convnet windows8下编译

    编译环境: windows8.1 Anaconda python2.7 Visual studio 2012 CUDA6.0 Pthread for windows Intel Math Kernel ...

  8. MSER算法介绍

    MSER代码编译: matlabroot %如果是VS2010则解压VS2010MEX支持文件到MATLAB根目录 unzip('E:\Software\develop Tools\VS2010MEX ...

  9. 调试腾讯微博 win8 版 共享失败的问题

    我是社交控,喜欢分享内容.分享到 腾讯微博时总失败,心想不能就这么算了,要看看异常的细节. 在VS 2012里,我选择 Debug > Debug Installed App Package, ...

  10. Oracle用法集锦

    查询第一条数据 修改表名 ALTER TABLE tablename RENAME TO newtablename 修改列名: ALTER TABLE BD_PRI RENAME COLUMN EU_ ...