There's code using this in Roadmap, and it works. 
Included below.

        Danny

const char *roadmap_file_map (const char *path,
                              const char *name,
                              const char *mode,
                              RoadMapFileContext *file)
{
        RoadMapFileContext context;
        DWORD file_size;
        const char *full_name;


        context = malloc (sizeof(*context));
        roadmap_check_allocated(context);

        context->hFile = INVALID_HANDLE_VALUE;
        context->base = NULL;
        context->name = NULL;
        context->size = 0;

        if (name[0] == '\\') {
           full_name = name;
        } else {
           full_name = roadmap_path_join (path, name);
        }
        context->name = ConvertToUNICODE(full_name);

        context->hFile = CreateFileForMapping(
                        context->name,
                        GENERIC_READ ,
                        FILE_SHARE_READ, NULL, OPEN_EXISTING,
                        FILE_ATTRIBUTE_NORMAL, NULL);

        if (context->hFile == INVALID_HANDLE_VALUE ) {
                roadmap_log (ROADMAP_INFO, "cannot open file %s",
full_name);
                roadmap_file_unmap (&context);
                return NULL;
        }

        file_size = GetFileSize(context->hFile, NULL);

        if (file_size == INVALID_FILE_SIZE) {
                roadmap_log (ROADMAP_ERROR, "cannot get size of file %
s", full_name);
                roadmap_file_unmap (&context);
                return NULL;
        }
        context->size = file_size;

        context->hFile = CreateFileMapping(
                context->hFile,
                NULL,
                PAGE_READONLY,
                0,0,0);
        if (context->hFile == INVALID_HANDLE_VALUE) {
                roadmap_log (ROADMAP_INFO, "cannot open file %s",
full_name);
                roadmap_file_unmap (&context);
                return NULL;
        }

        context->base = MapViewOfFile(context->hFile, FILE_MAP_READ, 0,
0, 0 );

        if (context->base == NULL) {
                roadmap_log (ROADMAP_ERROR, "cannot map file %s",
full_name);
                roadmap_file_unmap (&context);
                return NULL;
        }

        *file = context;
        roadmap_log (ROADMAP_INFO, "Mapped file %s", full_name);

        return context->base;
}


On Sun, 2008-06-08 at 09:37 +0200, Vincent Torri wrote:
> Hey,
> 
> I want to simulate mmap on windows ce. I've already written a code that 
> works on Windows XP. I thought that there would be no problem on Windows 
> CE, but it seems that CreateFileMapping() returns always an error.
> 
> Here is my complete test code :
> 
> /* arm-wince-cegcc-gcc -O3 -Wall -mwin32 -o cegcc_test1.exe cegcc_test1.c 
> -Wl,--enable-auto-import -Wl,-s */
> 
> #include <stdlib.h>
> #include <stdio.h>
> 
> #include <windows.h>
> # include <wchar.h>
> 
> 
> wchar_t *
> evil_char_to_wchar(const char *text)
> {
>     wchar_t *wtext;
>     int      wsize;
> 
>     wsize = MultiByteToWideChar(CP_ACP, 0, text, strlen(text) + 1, NULL, 
> 0);
>     if ((wsize == 0) ||
>         (wsize > (int)(ULONG_MAX/sizeof(wchar_t))))
>       return NULL;
> 
>     wtext = malloc(wsize * sizeof(wchar_t));
>     if (wtext)
>       if (!MultiByteToWideChar(CP_ACP, 0, text, strlen(text) + 1, wtext, 
> wsize))
>         return NULL;
> 
>     return wtext;
> }
> 
> char *
> evil_wchar_to_char(const wchar_t *text)
> {
>     char * atext;
>     int    size;
>     int    asize;
> 
>     size = wcslen(text) + 1;
> 
>     asize = WideCharToMultiByte(CP_ACP, 0, text, size, NULL, 0, NULL, 
> NULL);
>     if (asize == 0)
>       return NULL;
> 
>     atext = (char*)malloc((asize + 1) * sizeof(char));
> 
>     if (atext)
>       if (!WideCharToMultiByte(CP_ACP, 0, text, size, atext, asize, NULL, 
> NULL))
>         return NULL;
>     atext[asize] = '\0';
> 
>     return atext;
> }
> 
> char *
> evil_last_error_get(void)
> {
>     char   str[206];
>     LPTSTR str1;
>     char  *str2;
>     DWORD  err;
> 
>     err = GetLastError();
>     FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER | 
> FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS,
>                   NULL,
>                   err,
>                   0, // Default language
>                   (LPTSTR)&str1,
>                   0,
>                   NULL);
> 
> #ifdef UNICODE
>     str2 = evil_wchar_to_char(str1);
>     LocalFree(str1);
> #else
>     str2 = str1;
> #endif /* ! UNICODE */
> 
>     snprintf(str, 206, "(%ld) %s", err, str2);
>     LocalFree(str2);
> 
>     return strdup(str);
> }
> 
> 
> int main (int argc, char *argv[])
> {
>     FILE *fp;
>     int fd;
>     HANDLE handle;
>     HANDLE fm;
>     wchar_t *file;
> 
>     fp = fopen (argv[1], "rb");
>     if (!fp) {
>       printf (" * fopen failed\n");
>       return -1;
>     }
> 
>     fd = fileno (fp);
>     printf (" %p   %d\n", fp, fd);
>     handle = (HANDLE)get_osfhandle (fd);
>     if (handle == INVALID_HANDLE_VALUE) {
>       printf(" * get_osfhandle failed\n");
>       return -1;
>     }
> 
>     fm = CreateFileMapping(handle,
>                            NULL,
>                            PAGE_READONLY,
>                            0,
>                            0,
>                            NULL);
>     if (!fm) {
>       printf (" * CreateFileMapping failed %s\n", evil_last_error_get());
>       return -1;
>     }
> 
>     printf ("success\n");
> 
>     return 0;
> }
> 
> 
> 
> CreateFileMapping returns the error 6 "The handle is invalid." If I use 
> PAGE_READWRITE, I get the error 5 "Access is denied."
> 
> So I tried to create the handle with CreateFileForMapping(), using that 
> code:
> 
> 
>     file = evil_char_to_wchar(argv[1]);
>     handle = CreateFileForMapping(file, GENERIC_READ, 0, NULL, OPEN_EXISTING, 
> 0, 0);
> 
> 
> but then, the error is 87 "The parameter is incorrect."
> 
> Does someone know if CreateFileMapping() is working with cegcc ? And if 
> it is working, what is wrong in my code ?
> 
> thank you
> 
> Vincent Torri
> 
> 
> 
> -------------------------------------------------------------------------
> Check out the new SourceForge.net Marketplace.
> It's the best place to buy or sell services for
> just about anything Open Source.
> http://sourceforge.net/services/buy/index.php
> _______________________________________________
> Cegcc-devel mailing list
> Cegcc-devel@lists.sourceforge.net
> https://lists.sourceforge.net/lists/listinfo/cegcc-devel
> 
-- 
Danny Backx ; danny.backx - at - scarlet.be ; http://danny.backx.info


-------------------------------------------------------------------------
Check out the new SourceForge.net Marketplace.
It's the best place to buy or sell services for
just about anything Open Source.
http://sourceforge.net/services/buy/index.php
_______________________________________________
Cegcc-devel mailing list
Cegcc-devel@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/cegcc-devel

Reply via email to