Thursday 3 January 2013

Win API- Memory management

1.Main memory.


2.Virtual memory.
a). Memory mapping File : shared memory between processes
 #include <windows.h>
#include <stdio.h>
#include <iostream>
using namespace std;
#define BUF_SIZE 256
TCHAR szName[]=TEXT("SharedObject");
TCHAR szMsg[]=TEXT("Message from first process.");
int main()
{
    HANDLE hFile =CreateFile(L"share.txt",
                             GENERIC_READ | GENERIC_WRITE,
                             FILE_SHARE_READ,
                             NULL,
                             OPEN_ALWAYS,
                             FILE_ATTRIBUTE_NORMAL,
                             NULL);
    if(hFile==INVALID_HANDLE_VALUE)
    {
        cout<<" the function fails"<<endl;
        return false;
    }

    HANDLE hMapFile=CreateFileMapping(hFile,
                                      NULL,
                                      PAGE_READWRITE,
                                      0,
                                      BUF_SIZE,
                                      szName
                                     );
    if(hMapFile==NULL)
    {
        cout<<" the function fails"<<endl;
        return false;
    }
    LPVOID lpMapAddress =MapViewOfFile(hMapFile,
                                       FILE_MAP_ALL_ACCESS,
                                       0,
                                       0,
                                       BUF_SIZE
                                      );
    if(hMapFile==NULL)
    {
        cout<<" the function fails"<<endl;
        CloseHandle(hMapFile);
        return false;
    }
    CopyMemory(lpMapAddress, szMsg, wcslen(szMsg) * sizeof(TCHAR) );
     cout<<" end"<<endl;
    while(true);
     UnmapViewOfFile(lpMapAddress);
     CloseHandle(hMapFile);
    return 0;
}

#include <windows.h>
#include <stdio.h>
#include <iostream>
using namespace std;
#pragma comment(lib, "user32.lib")
#define BUF_SIZE 256
TCHAR szName[]=TEXT("SharedObject");
#define BUF_SIZE 256
int main()
{
    HANDLE hMapFile =OpenFileMapping(FILE_MAP_ALL_ACCESS,
                                 FALSE,
                                 szName);
    if(hMapFile==NULL)
    {
        cout<<" OpenFileMapping fails"<<endl;
        return false;
    }
     LPVOID lpMapAddress =MapViewOfFile(hMapFile,
                                      FILE_MAP_ALL_ACCESS,
                                       0,
                                       0,
                                       BUF_SIZE
                                      );

   if(lpMapAddress==NULL)
    {
        cout<<" MapViewOfFile fails"<<endl;
        CloseHandle(hMapFile);
        return false;
    } 
    TCHAR *Data;
    Data =(TCHAR *)lpMapAddress;
    for(int i = 0 ; ;i++)
    {
         if (Data[i] == NULL)
         {
             break;
         }
        printf("%c", Data[i]);
    }
    printf("\n");
    UnmapViewOfFile(lpMapAddress);
    CloseHandle(hMapFile);
    return 0;
}

3. Pointer to a function in WinAPI
declare:
typedef LRESULT (__stdcall * WindowProcedure)(HWND, UINT, WPARAM, LPARAM);
use:

WindowProcedure ptr = NULL;


#include <windows.h>
#include <iostream>
using namespace std;
typedef int (*Total)(int a , int b);
int  add(int a , int b)
{
    return a+b;
}
int main()
{
    Total a = NULL;
    cout<<"adress : "<<&a<<endl;
    a = add;
     cout<<a(3,5)<<endl;
     cout<<add<<endl;
    return 0 ;
}

No comments:

Post a Comment