函数问题

wshcdr 2004-04-05 02:38:40
我的文件中包含了MapUserPhysicalPages
尽管INCLUDE了Windows.h,还是死活编译不过

哪个大侠说说呢
...全文
6 点赞 收藏 13
写回复
13 条回复
切换为时间正序
请发表友善的回复…
发表回复
byf2002 2004-04-14
另外:


MapUserPhysicalPages
The MapUserPhysicalPages function maps previously allocated physical memory pages at the specified address within an Address Windowing Extensions (AWE) region.

To perform "batch" mapping and unmapping of multiple regions, use the MapUserPhysicalPagesScatter function.

BOOL MapUserPhysicalPages(
PVOID lpAddress, // starting address of region
ULONG_PTR NumberOfPages, // size of physical memory
PULONG_PTR UserPfnArray // array of page frame numbers
);
Parameters
lpAddress
[in] Pointer to the starting address of the region of memory to remap. The value of lpAddress must be within the address range returned by the VirtualAlloc function when the AWE region was allocated.
NumberOfPages
[in] Specifies the size, in pages, of the physical memory (and virtual address space) to establish translations for. The virtual address range is contiguous starting at lpAddress. The physical frames are specified by the UserPfnArray. The total number of pages cannot extend from the starting address beyond the end of the range specified in AllocateUserPhysicalPages.
UserPfnArray
[in] Specifies the address of an array of physical page frame numbers. These frames will be mapped by the argument lpAddress upon return from this function. The size of the memory allocated should be at least NumberOfPages times the size of the data type ULONG_PTR.
Do not attempt to modify this buffer. It contains operating system data, corruption of which could be catastrophic. There is no information in it that is useful to your application.

Specifying NULL for this parameter results in the specified address range being unmapped. (The specified physical pages are not freed. You must call FreeUserPhysicalPages to free them.)

Return Values
If the function succeeds, the return value is TRUE.

If the function fails, the return value is FALSE and no mapping (partial or otherwise) will have been done. To get extended error information, call GetLastError.

Remarks
Any number of physical memory pages can be specified, provided the memory would not extend outside the virtual address space allocated by VirtualAlloc. Any existing address maps are automatically overwritten with the new translations, and the old translations are unmapped.

You cannot map physical memory pages outside the range specified in AllocateUserPhysicalPages.

Note The physical pages are unmapped but they are not freed. You must call FreeUserPhysicalPages to free the physical pages.

Physical pages can reside at any physical address. You should make no assumptions about the contiguity of the physical pages.

To simply unmap the current address range, specify NULL as the physical memory page array parameter. Any currently mapped pages are unmapped, but are not freed. You must call FreeUserPhysicalPages to free the physical pages.

In a multiprocessor environment, this function maintains hardware translation buffer coherence. Upon return from this function, all threads on all processors are guaranteed to see the correct mapping.

Requirements
Windows NT/2000: Requires Windows 2000.
Windows 95/98: Unsupported.
Header: Declared in Winbase.h; include Windows.h.
Library: Use Kernel32.lib.

回复
byf2002 2004-04-14
给你个例子吧:

#include <windows.h>
#include <stdio.h>

#define MEMORY_REQUESTED 1024*1024 // request a megabyte

BOOL
LoggedSetLockPagesPrivilege ( HANDLE hProcess,
BOOL bEnable);

void _cdecl main()
{
BOOL bResult; // generic boolean value
ULONG_PTR NumberOfPages, // number of pages we will request
NumberOfPagesInitial; // initial number of pages requested
ULONG_PTR *aPFNs; // page info; holds opaque data
PVOID lpMemReserved; // AWE window
SYSTEM_INFO sSysInfo; // useful system information
int PFNArraySize; // memory to request for PFN array

GetSystemInfo(&sSysInfo); // populate the system information structure

printf ("This computer has a page size of %d.\n", sSysInfo.dwPageSize);

// Calculate the number of pages of memory we'll request.

NumberOfPages = MEMORY_REQUESTED/sSysInfo.dwPageSize;
printf ("Requesting %d pages of memory.\n", NumberOfPages);

// Calculate the size of the user PFN array.

PFNArraySize = NumberOfPages * sizeof (ULONG_PTR);

printf ("Requesting a PFN array of %d bytes.\n", PFNArraySize);

aPFNs = (ULONG_PTR *) HeapAlloc (GetProcessHeap (), 0, PFNArraySize);

if (aPFNs == NULL) {
printf ("Failed to allocate on heap.\n");
return;
}

// Enable the privilege.

if( ! LoggedSetLockPagesPrivilege( GetCurrentProcess(), TRUE ) ) {
return;
}

// Allocate the physical memory we want.

NumberOfPagesInitial = NumberOfPages;
bResult = AllocateUserPhysicalPages( GetCurrentProcess(),
&NumberOfPages,
aPFNs );

if( bResult != TRUE )
{
printf("Cannot allocate physical pages, error %u.\n", GetLastError() );
return;
}

if( NumberOfPagesInitial != NumberOfPages )
{
printf("Allocated only %p pages.\n", NumberOfPages );
return;
}

// Reserve the virtual memory.

lpMemReserved = VirtualAlloc( NULL,
MEMORY_REQUESTED,
MEM_RESERVE | MEM_PHYSICAL,
PAGE_READWRITE );

if( lpMemReserved == NULL )
{
printf("Cannot reserve memory.\n");
return;
}

// Map the physical memory into our window.

bResult = MapUserPhysicalPages( lpMemReserved,
NumberOfPages,
aPFNs );

if( bResult != TRUE )
{
printf("MapUserPhysicalPages failed to map, error %u.\n", GetLastError() );
return;
}

// unmap

bResult = MapUserPhysicalPages( lpMemReserved,
NumberOfPages,
NULL );

if( bResult != TRUE )
{
printf("MapUserPhysicalPages failed to unmap, error %u.\n", GetLastError() );
return;
}

// Free the physical pages.

bResult = FreeUserPhysicalPages( GetCurrentProcess(),
&NumberOfPages,
aPFNs );

if( bResult != TRUE )
{
printf("Cannot free physical pages, error %u.\n", GetLastError() );
return;
}

// Free virtual memory.

bResult = VirtualFree( lpMemReserved,
0,
MEM_RELEASE );
}

/*****************************************************************
LoggedSetLockPagesPrivilege: a function to obtain, if possible, or
release the privilege of locking physical pages.

Inputs:

HANDLE hProcess: Handle for the process for which we seek the
privilege

BOOL bEnable: Shall we enable (TRUE) or disable?

Return value: TRUE indicates success, FALSE failure.

*****************************************************************/
BOOL
LoggedSetLockPagesPrivilege ( HANDLE hProcess,
BOOL bEnable)
{
struct {
DWORD Count;
LUID_AND_ATTRIBUTES Privilege [1];
} Info;

HANDLE Token;
BOOL Result;

// Open the token.

Result = OpenProcessToken ( hProcess,
TOKEN_ADJUST_PRIVILEGES,
& Token);

if( Result != TRUE ) {
printf( "Cannot open process token.\n" );
return FALSE;
}

// Enable or disable?

Info.Count = 1;
if( bEnable )
{
Info.Privilege[0].Attributes = SE_PRIVILEGE_ENABLED;
}
else
{
Info.Privilege[0].Attributes = 0;
}

// Get the LUID.

Result = LookupPrivilegeValue ( NULL,
SE_LOCK_MEMORY_NAME,
&(Info.Privilege[0].Luid));

if( Result != TRUE )
{
printf( "Cannot get privilege value for %s.\n", SE_LOCK_MEMORY_NAME );
return FALSE;
}

// Adjust the privilege.

Result = AdjustTokenPrivileges ( Token, FALSE,
(PTOKEN_PRIVILEGES) &Info,
NULL, NULL, NULL);

// Check the result.

if( Result != TRUE )
{
printf ("Cannot adjust token privileges, error %u.\n", GetLastError() );
return FALSE;
}
else
{
if( GetLastError() != ERROR_SUCCESS )
{
printf ("Cannot enable SE_LOCK_MEMORY privilege, please check the local policy.\n");
return FALSE;
}
}

CloseHandle( Token );

return TRUE;
}
回复
byf2002 2004-04-14
编译不通过,出现什么提示啊.

讲详细点

你惜字如金哪.
回复
wshcdr 2004-04-14
用 GetLastError 看看返回什么错误.
===========

编译都不过的,怎么看?
我是不知道了
回复
王国凡 2004-04-07
用 GetLastError 看看返回什么错误.
回复
wshcdr 2004-04-07
还有就是只能在2000以后的系统中使用

=========
是在WIN2K下使用的
回复
wshcdr 2004-04-06
已经包含了Kernerl32.lib

还是不行

新建了一个Win32 Application的工程
也是编译不过

郁闷
回复
pomelowu 2004-04-06
还有就是只能在2000以后的系统中使用
回复
pomelowu 2004-04-06
在选项中看看有没有添加Kernel32.lib所在的路径。
回复
pomelowu 2004-04-05
是Windows.h,但是还要包含库文件Kernel32.lib.

回复
QunKangLi 2004-04-05
W2K以上版本才支持.
在#include <Windows.h>前加上:
#define _WIN32_WINNT 0x0500
回复
快乐鹦鹉 2004-04-05
可是MapUserPhysicalPages函数好像不在WIndow.h中啊。
回复
酒红绿叶 2004-04-05
新建一个程序,只写这一个东西!
看看是不是由于别的东西造成的!

我也遇到过这种情况!
虽然不是这个,
就是死活不对!



回复
发动态
发帖子
VC/MFC
创建于2007-09-28

1.5w+

社区成员

VC/MFC相关问题讨论
申请成为版主
社区公告
暂无公告