GetFileSizeEx 文件映射失败

发布于 2024-12-07 10:22:46 字数 974 浏览 0 评论 0原文

我在创建命名共享内存并检查其大小时遇到​​问题。当我在这样的函数中调用 GetFileSizeEx 函数时,它会失败。关于如何调试这个有什么想法吗?

void test_getsize(const char* lpName, int size){

    HANDLE handle = CreateFileMapping(
                     INVALID_HANDLE_VALUE,    // use paging file
                     NULL,                    // default security
                     PAGE_READWRITE,          // read/write access
                     0,                       // maximum object size (high-order DWORD)
                     size,                // maximum object size (low-order DWORD)
                     lpName);                 // name of mapping object

    if (handle== NULL || handle== INVALID_HANDLE_VALUE){
        last_error = get_error_from_errorno();
        *error_return =1;
    }


    LARGE_INTEGER new_size;

    err = GetFileSizeEx(handle, &new_size);

    if (err==0){ printf("err ");} else {printf("pass ");}
    printf("size=%lu\n", (unsigned long)new_size.QuadPart);
}

Im having trouble creating a named shared memory and inspecting its size. The function GetFileSizeEx fails when I call it in a function like this. Any ideas on how to debug this?

void test_getsize(const char* lpName, int size){

    HANDLE handle = CreateFileMapping(
                     INVALID_HANDLE_VALUE,    // use paging file
                     NULL,                    // default security
                     PAGE_READWRITE,          // read/write access
                     0,                       // maximum object size (high-order DWORD)
                     size,                // maximum object size (low-order DWORD)
                     lpName);                 // name of mapping object

    if (handle== NULL || handle== INVALID_HANDLE_VALUE){
        last_error = get_error_from_errorno();
        *error_return =1;
    }


    LARGE_INTEGER new_size;

    err = GetFileSizeEx(handle, &new_size);

    if (err==0){ printf("err ");} else {printf("pass ");}
    printf("size=%lu\n", (unsigned long)new_size.QuadPart);
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

回忆追雨的时光 2024-12-14 10:22:46

您传递给 GetFileSizeEx 的句柄必须是文件的句柄。您将一个文件句柄传递给它映射,这是完全不同的事情,所以它不起作用。

由于显然您需要文件映射的大小(称为节对象),并且我认为 Win32 API 不提供此功能,因此您需要使用名为 NtQuerySection。这是它如何工作的一个想法(我还没有尝试过):

typedef enum _SECTION_INFORMATION_CLASS
{
    SectionBasicInformation,
    SectionImageInformation
} SECTION_INFORMATION_CLASS;

typedef struct _SECTION_BASIC_INFORMATION {
  PVOID         Base;
  ULONG         Attributes;
  LARGE_INTEGER Size;
} SECTION_BASIC_INFORMATION;

typedef DWORD (WINAPI* NTQUERYSECTION)
    (HANDLE, SECTION_INFORMATION_CLASS, PVOID, ULONG, PULONG);
NTQUERYSECTION NtQuerySection =
    (NTQUERYSECTION)GetProcAddress(LoadLibrary("ntdll.dll"), "NtQuerySection");

SECTION_BASIC_INFORMATION SectionInfo = { 0 };
NTSTATUS = NtQuerySection(handle, SectionBasicInformation, &SectionInfo,
    sizeof(SectionInfo), 0);

The handle you pass to GetFileSizeEx must be a handle to a file. You are passing it a handle to a file mapping, which is a completely different thing, so it won't work.

Since apparently you want the size of the file mapping (called a section object), and I don't think the Win32 API provides this feature, you would need to use the native Windows API called NtQuerySection. Here's an idea of how it might work (I haven't tried it):

typedef enum _SECTION_INFORMATION_CLASS
{
    SectionBasicInformation,
    SectionImageInformation
} SECTION_INFORMATION_CLASS;

typedef struct _SECTION_BASIC_INFORMATION {
  PVOID         Base;
  ULONG         Attributes;
  LARGE_INTEGER Size;
} SECTION_BASIC_INFORMATION;

typedef DWORD (WINAPI* NTQUERYSECTION)
    (HANDLE, SECTION_INFORMATION_CLASS, PVOID, ULONG, PULONG);
NTQUERYSECTION NtQuerySection =
    (NTQUERYSECTION)GetProcAddress(LoadLibrary("ntdll.dll"), "NtQuerySection");

SECTION_BASIC_INFORMATION SectionInfo = { 0 };
NTSTATUS = NtQuerySection(handle, SectionBasicInformation, &SectionInfo,
    sizeof(SectionInfo), 0);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文