为什么不't此P/Invoke签名工作
Why doesn't this P/Invoke signature work?
我试图从WinApi函数封送C#代码。。但我理解。。为什么她不工作!文件路径-正确,句柄被占用。有人能帮我吗?
using System;
using System.Collections.Generic;
using System.Runtime.InteropServices;
using System.Linq;
using System.Text;
namespace ChangeFileTime
{
class Program
{
[DllImport("kernel32.dll", SetLastError = true)]
[return: MarshalAs(UnmanagedType.Bool)]
public static extern bool SetFileTime(IntPtr hFile, ref long lpCreationTime,
ref long lpLastAccessTime,
ref long lpLastWriteTime);
[DllImport("kernel32.dll")]
public static extern IntPtr CreateFile(string lpFilename,
uint dwDesiredAccess,
uint dwShareMode,
IntPtr SecurityAttributes,
uint dwCreationDisposition,
uint dwFlagsAndAttributes,
IntPtr hTemplateFile);
[DllImport("kernel32.dll")]
public static extern bool CloseHandle(IntPtr hObject);
static void Main(string[] args)
{
const uint GENERIC_READ = 0x80000000;
const uint OPEN_EXISTING = 3;
const uint FILE_SHARE_WRITE = 0x00000002;
const uint FILE_ATTRIBUTE_NORMAL = 128;
IntPtr ptr = CreateFile("C:\file.txt", GENERIC_READ,
FILE_SHARE_WRITE,
IntPtr.Zero,
OPEN_EXISTING,
FILE_ATTRIBUTE_NORMAL,
IntPtr.Zero);
DateTime creation_time = new DateTime(1990, 12, 14);
long file_time = creation_time.ToFileTime();
DateTime time = DateTime.FromFileTime(file_time);
SetFileTime(ptr, ref file_time, ref file_time, ref file_time);
int a = 20;
}
}
}
我想我弄错了。。我试着写C++代码,她做得很好。。但是为什么C#不起作用呢?
来自MSDN:
句柄必须是使用带有的CreateFile函数创建的FILE_WRITE_ATTRIBUTES
这应该有效:
const uint FILE_WRITE_ATTRIBUTES = 0x0100;
IntPtr ptr = CreateFile("C:\file.txt", FILE_WRITE_ATTRIBUTES, //...
相关文章:
- QSqlquery prepare()和bindvalue()不工作
- 导入库可以跨dll版本工作吗
- 为什么使用 P/Invoke 调用 dll 时,某些计算机中的 LoadLibrary 失败?
- 以螺旋方式打印矩阵的程序.(工作不好)
- 对象指针在c++中是如何工作的
- 为什么在Windows上的VS 2019和Clang 9中"size_t"在没有标题的情况下工作
- VSOMEIP-2个设备之间的通信(TCP/UDP)不工作
- 为字符串中每 N 个字符插入空格的函数没有按照我认为的方式工作?
- C++为线程工作动态地分割例程
- 为什么我的 std::ref 无法按预期工作?
- 布尔比较运算符是如何在C++中工作的
- SampleConsensusPrerejective(ext.RANSAC)是如何真正工作的
- 不确定要在我的main中放入什么才能使我的代码正常工作
- 为什么std::condition_variable notify_all的工作速度比notify_one快(对于随机请
- <<操作员在下面的行中工作
- 有人能解释一下为什么下界是这样工作的吗C++的
- ExtractIconEx:可以工作,但偶尔会崩溃
- C++中的memset函数工作不正常
- 为什么不't此P/Invoke签名工作
- std::invoke(C++1z) 如何工作