C#调用windows api 函数GetShortPathName
来源:互联网 发布:4d软件 编辑:程序博客网 时间:2024/06/05 07:26
其实我们的议题应该叫做C#如何直接调用非托管代码,通常有2种方法:
1. 直接调用从 DLL导出的函数。
2. 调用 COM对象上的接口方法
我主要讨论从dll中导出函数,基本步骤如下:
1.使用 C#关键字static和extern声明方法。
2.将DllImport属性附加到该方法。DllImport属性允许您指定包含该方法的 DLL的名称。
3.如果需要,为方法的参数和返回值指定自定义封送处理信息,这将重写 .NET Framework 的默认封送处理。
好,我们开始
1.首先我们查询MSDN找到GetShortPathName的定义
The GetShortPathName function retrieves the short path form of the specified path.
DWORD GetShortPathName(
LPCTSTR lpszLongPath,
LPTSTR lpszShortPath,
DWORD cchBuffer
);
public class CShortPath { [DllImport("kernel32.dll", CharSet = CharSet.Auto)] public static extern int GetShortPathName( [MarshalAs(UnmanagedType.LPTStr)]string path, [MarshalAs(UnmanagedType.LPTStr)]StringBuilder short_path, int short_len ); public static string GetShortPath(string name) { int lenght = 0; lenght = GetShortPathName(name, null, 0); if (lenght == 0) { //new nghmp.GenericErrorForm("Can't get short path name", name, true); return name; } StringBuilder short_name = new StringBuilder(lenght); lenght = GetShortPathName(name, short_name, lenght); if (lenght == 0) { //new nghmp.GenericErrorForm("Can't get short path name", name, true); return name; } return short_name.ToString(); }//GetShortPath }//class CShortPath
- C#调用windows api 函数GetShortPathName
- C#调用Windows API函数
- C#调用Windows API函数
- C#调用Windows API函数
- C#调用Windows API函数
- C#调用windows api函数
- C#调用Windows API函数
- 在C#中调用windows API函数
- 用Visual C#调用Windows API函数
- 用Visual C#调用Windows API函数
- 用Visual C#调用Windows API函数
- 用Visual C#调用Windows API函数
- 用Visual C#调用Windows API函数
- 用Visual C#调用Windows API函数
- 用Visual C#调用Windows API函数
- 用Visual C#调用Windows API函数
- 用Visual C#调用Windows API函数
- 在C#中调用windows API函数
- 登陆注册+分页查询(hibernate+struts2)
- Flickr架构
- android实现涂鸦,保存涂鸦后的图片,清屏
- java job(spring)
- Fedroa 14 环境下安装 ns-2.34
- C#调用windows api 函数GetShortPathName
- Linux网络编程--4. 完整的读写函数
- HTTP Status 500 - java.lang.ClassNotFoundException: org.apache.jsp.book_jsp
- dos进入中文文件夹
- 视图间坐标转换
- java HashMap 用法
- 你应该知道的10种软件工具
- push or pull 与hadoop 的机制
- Linux网络编程--5. 用户数据报发送