Click to See Complete Forum and Search --> : using CreateFileMapping on window98


cuit
June 13th, 2005, 12:02 PM
Hi gurus.

I want to use a API CreateFileMapping on window98.

I find a API CreateFileMapping to use a Common Memory Block between two programs.

I want to use CreateFileMapping without mapped file. so I set the filehandle( first argument of CreateFileMapping ) 0xFFFFFFFF.

I successed on window XP and 2003. but I can't on window 98.


this is my code.

m_hMapping_gt = CreateFileMapping(0xFFFFFFFF, sa, PAGE_READWRITE, 0, c_gt_net_queue_size, "ExMemMap_gt")
lpMapView_gt = MapViewOfFile(m_hMapping_gt, FILE_MAP_WRITE, 0, 0, 0)

on window XP and 2003, CreateFileMapping return valid value.

but it return 0 on window 98.

What is a problem? plz help me..

MrViggy
June 13th, 2005, 12:23 PM
Why are you using 0xFFFFFFFF as the file handle? You prolly should use the macro 'INVALID_HANDLE_VALUE', like the MSDN (http://msdn.microsoft.com/library/default.asp?url=/library/en-us/fileio/fs/createfilemapping.asp) says. It's possible that 'INVALID_HANDLE_VALUE's actual value is different on Win98.

Viggy

Andreas Masur
June 13th, 2005, 03:03 PM
A SDK function (e.g. CreateDirectory) failed. What I'm doing wrong? (http://www.codeguru.com/forum/showthread.php?t=318721)

SuperKoko
June 13th, 2005, 03:34 PM
It's possible that 'INVALID_HANDLE_VALUE's actual value is different on Win98.
No, it is not possible, because it would mean that applications written with Win32 API must be compiled under each version of the OS, and of course, the purpose of the Win32 API is to have only one executable for all theses OSes.
But, of course, it is far better to use INVALID_HANDLE_VALUE, because it could be different with Win64 API.
Using all types defined, in windef.h, and constants defined in winuser.h, allow to write more easily applications that can be compiled under Win16, Win32, and Win64.

Moreover, to be sure, i looked in the header files.

SuperKoko
June 13th, 2005, 03:41 PM
m_hMapping_gt = CreateFileMapping(0xFFFFFFFF, sa, PAGE_READWRITE, 0, c_gt_net_queue_size, "ExMemMap_gt");
I think that the problem is that you omit a flag:
You must specify SEC_COMMIT, or SEC_RESERVE, for example.
You probably want SEC_COMMIT:

m_hMapping_gt = CreateFileMapping(0xFFFFFFFF, sa, PAGE_READWRITE | SEC_COMMIT, 0, c_gt_net_queue_size, "ExMemMap_gt");


flProtect

Specifies the protection desired for the file view, when the file is mapped. This parameter can be one of the following values:

Value Description
PAGE_READONLY Gives read-only access to the committed region of pages. An attempt to write to or execute the committed region results in an access violation. The file specified by the hFile parameter must have been created with GENERIC_READ access.
PAGE_READWRITE Gives read-write access to the committed region of pages. The file specified by hFile must have been created with GENERIC_READ and GENERIC_WRITE access.
PAGE_WRITECOPY Gives copy on write access to the committed region of pages. The files specified by the hFile parameter must have been created with GENERIC_READ and GENERIC_WRITE access.
In addition, an application can specify certain section attributes by combining (using the bitwise OR operator) one or more of the following section attribute values with one of the preceding page protection values:

Value Description
SEC_COMMIT Allocates physical storage in memory or in the paging file on disk for all pages of a section. This is the default setting.
SEC_IMAGE The file specified for a section's file mapping is an executable image file. Because the mapping information and file protection are taken from the image file, no other attributes are valid with SEC_IMAGE.
SEC_NOCACHE All pages of a section are to be set as non-cacheable. This attribute is intended for architectures requiring various locking structures to be in memory that is never fetched into the processor's. On 80x86 and MIPS machines, using the cache for these structures only slows down the performance as the hardware keeps the caches coherent. Some device drivers require noncached data so that programs can write through to the physical memory. SEC_NOCACHE requires either the SEC_RESERVE or SEC_COMMIT to also be set.
SEC_RESERVE Reserves all pages of a section without allocating physical storage. The reserved range of pages cannot be used by any other allocation operations until it is released. Reserved pages can be committed in subsequent calls to the VirtualAlloc function. This attribute is valid only if the hFile parameter is (HANDLE)0xFFFFFFFF; that is, a file mapping object backed by the operating sytem paging file.

It says that you must specify one or more SEC_ flags.

I hope that it will now works!

Arjay
June 13th, 2005, 04:10 PM
Hi gurus.

I want to use a API CreateFileMapping on window98.

I find a API CreateFileMapping to use a Common Memory Block between two programs.

I want to use CreateFileMapping without mapped file. so I set the filehandle( first argument of CreateFileMapping ) 0xFFFFFFFF.

I successed on window XP and 2003. but I can't on window 98.


this is my code.

m_hMapping_gt = CreateFileMapping(0xFFFFFFFF, sa, PAGE_READWRITE, 0, c_gt_net_queue_size, "ExMemMap_gt")
lpMapView_gt = MapViewOfFile(m_hMapping_gt, FILE_MAP_WRITE, 0, 0, 0)

on window XP and 2003, CreateFileMapping return valid value.

but it return 0 on window 98.

What is a problem? plz help me.. Try

m_hMapping_gt = CreateFileMapping( INVALID_HANDLE_VALUE,
NULL,
PAGE_READWRITE, // | SEC_COMMIT is implied as it's the default
0,
c_gt_net_queue_size,
"ExMemMap_gt" );


This should do it. The security attributes aren't used on Win9x - not sure if this caused the failure, but I've always used NULL here with success on 9x.

Btw, did you happen to check the GetLastError() for the cause of the failure?

Arjay

cuit
June 15th, 2005, 11:06 AM
Thanks a lot MrViggy, Andreas Masur, SuperKoko.
and Special Thanks Arjay.

I solved this problem through the help of you.

I changed security attributes to NULL, and succeeded on window98.