English 中文(简体)
仅在同一过程中多开临时档案
原标题:Multiple opening of temporary file only in same process

I have a question about FILE_ATTRIBUTE_TEMPORARY marked files. First of all, here is what I want to do:

I have a DLL, that takes a Filename, and opens that file internally and reads from it. I do not know how this file is handled inside. The file I want to give to that DLL will be created by my process. It must be a temporary file and its data must be held only in RAM and must not be accessed by other processes. So I use the Win32 function CreateFile() with the FILE_ATTRIBUTE_TEMPORARY and the FILE_FLAG_DELETE_ON_CLOSE. This so far works, fine.

我有一部tes子,如果我能够第二次查阅档案,同时仍然打开。 这里是:

HANDLE WINHandle = CreateFile("TempFileWIN.txt", (GENERIC_WRITE | GENERIC_READ) ,(FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE), 0, CREATE_ALWAYS, (FILE_ATTRIBUTE_TEMPORARY | FILE_FLAG_DELETE_ON_CLOSE), 0);

ifstream ifs("TempFileWIN.txt", (ios::in | ios::trunc));

if(ifs.is_open())
{
    cout << "Success!" << endl;
}
else if(ifs.fail())
{
    cout << "Failed!" << endl;
}

I am using the fstream to test if the file could be opened with a stream. That code up there doesn t work. The output is "Failed!". I know, that the file could be opened with the CreateFile a second time. I checked that out. But want to know if it is possible to open the file by an external DLL that works with (e.g.) a fstream.

I hope you can help me with this matter. Best regards.

Edit: Maybe a better question is how I can lock a file to my process and ensure, that it can never be accessed by an other process (even if my process is killed). The file must be openable with C++ fstream object.

问题回答

如果是你,我将保持公开档案的处理,将其交给《刑法》处理,而不使用档案名称,因为如果你试图利用正常档案查阅,在某个时候有可能进入一个临时的、删除的文档。

如本答复所述,可以在一个源头上使用Windows处理器:https://stackoverflow.com/a/476014/393701





相关问题
Undefined reference

I m getting this linker error. I know a way around it, but it s bugging me because another part of the project s linking fine and it s designed almost identically. First, I have namespace LCD. Then I ...

C++ Equivalent of Tidy

Is there an equivalent to tidy for HTML code for C++? I have searched on the internet, but I find nothing but C++ wrappers for tidy, etc... I think the keyword tidy is what has me hung up. I am ...

Template Classes in C++ ... a required skill set?

I m new to C++ and am wondering how much time I should invest in learning how to implement template classes. Are they widely used in industry, or is this something I should move through quickly?

Print possible strings created from a Number

Given a 10 digit Telephone Number, we have to print all possible strings created from that. The mapping of the numbers is the one as exactly on a phone s keypad. i.e. for 1,0-> No Letter for 2->...

typedef ing STL wstring

Why is it when i do the following i get errors when relating to with wchar_t? namespace Foo { typedef std::wstring String; } Now i declare all my strings as Foo::String through out the program, ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

Window iconification status via Xlib

Is it possible to check with the means of pure X11/Xlib only whether the given window is iconified/minimized, and, if it is, how?

热门标签