Jump to content

Recommended Posts

Posted (edited)

hi guys  i want  read a memory mapped file , but  i want  use dll, , i know  in autoit  you can read also without dll , but i use autoit  like test if, dll work ,( because the dll i will use in an other language)

i have created  with VS2019 C++ in wind 10 64  bit , the example string  memory mapped file (by msdn )

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

#define BUF_SIZE 4096
TCHAR szName[] = TEXT("dllmemfilemap");
TCHAR szMsg[] = TEXT("Message from first process.");

int _tmain()
{
    HANDLE hMapFile;
    LPCTSTR pBuf;

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

    if (hMapFile == NULL)
    {
        _tprintf(TEXT("Could not create file mapping object (%d).\n"),
            GetLastError());
        return 1;
    }
    pBuf = (LPTSTR)MapViewOfFile(hMapFile,   // handle to map object
        FILE_MAP_ALL_ACCESS, // read/write permission
        0,
        0,
        BUF_SIZE);

    if (pBuf == NULL)
    {
        _tprintf(TEXT("Could not map view of file (%d).\n"),
            GetLastError());

        CloseHandle(hMapFile);

        return 1;
    }


    CopyMemory((PVOID)pBuf, szMsg, (_tcslen(szMsg) * sizeof(TCHAR)));
    _getch();

    UnmapViewOfFile(pBuf);

    CloseHandle(hMapFile);

    return 0;
}

for this project i started of this dll ,

https://www.autoitscript.com/forum/applications/core/interface/file/attachment.php?id=16378

 

the  first problem i meet  is  when i push GetSharedMem  return me only one  M also if i  increase the dimention (in file test1.au3)

$ret = DllCall($dll,"none","GetSharedMem","str",$read_string,"int",50)

TO

            $ret = DllCall($dll,"none","GetSharedMem","str",$read_string,"int",500)

some one have  a idea ??

thankz

or if exist some opensource dll , just good and complete , is welcome , thankz

 

P.S.

the test1.au3 is

#include <GUIConstants.au3>
#include <Array.au3>

Dim $dll
Dim $ret
Dim $read_string = ""

$dll = DllOpen("SharedMemory.dll")
If @error Then
    MsgBox(0,"Testing","Error opening dll")
EndIf

$Form1 = GUICreate("Test1", 360, 321, 280, 230)
$Button1 = GUICtrlCreateButton("SetSharedMem", 40, 40, 81, 25, 0)
$Button2 = GUICtrlCreateButton("GetSharedMem", 43, 70, 81, 25, 0)
$Input1 = GUICtrlCreateInput("", 144, 40, 153, 21)
$Input2 = GUICtrlCreateInput("", 144, 72, 153, 21)
GUISetState(@SW_SHOW)

While 1
    $nMsg = GUIGetMsg()
    Switch $nMsg
        Case $Button1
            DllCall($dll,"none","SetSharedMem","str",GUICtrlRead($Input1))
            If @error Then
                MsgBox(0,"Testing","Error calling SetSharedMem")
                Exit
            EndIf
        Case $Button2
            $ret = DllCall($dll,"none","GetSharedMem","str",$read_string,"int",50)
            If @error Then
                MsgBox(0,"Testing","Error calling GetSharedMem")
                Exit
            Else
                GUICtrlSetData($Input2,$ret[1])
                _ArrayDisplay($ret)
            EndIf
        Case $GUI_EVENT_CLOSE
            Exit
    EndSwitch
WEnd

DllClose($dll)
If @error Then
    MsgBox(0,"Testing","Error closing dll")
EndIf

code of DLL

#include <windows.h>
           #include <memory.h>
           #include <string.h>
           
           #define SHMEMSIZE 4096 
           
           static LPVOID lpvMem = NULL;   // pointer to shared memory
           static HANDLE hMapObject = NULL;  // handle to file mapping
           
           // The DLL entry-point function sets up shared memory using a 
           // named file-mapping object. 
           
           BOOL WINAPI DllMain(HINSTANCE hinstDLL,  // DLL module handle
               DWORD fdwReason,           // reason called 
               LPVOID lpvReserved)         // reserved 
           { 
               BOOL fInit, fIgnore;           
               switch (fdwReason) 
               { 
                   // DLL load due to process initialization or LoadLibrary
                   case DLL_PROCESS_ATTACH: 
                       // Create a named file mapping object
                       hMapObject = CreateFileMapping( 
                           INVALID_HANDLE_VALUE,   // use paging file
                           NULL,                   // default security attributes
                           PAGE_READWRITE,       // read/write access
                           0,                     // size: high 32-bits
                           SHMEMSIZE,             // size: low 32-bits
                           TEXT("dllmemfilemap")); // name of map object
                       if (hMapObject == NULL) 
                           return FALSE; 
                       // The first process to attach initializes memory
                       fInit = (GetLastError() != ERROR_ALREADY_EXISTS); 
                       
                       // Get a pointer to the file-mapped shared memory
                       lpvMem = MapViewOfFile( 
                           hMapObject,   // object to map view of
                           FILE_MAP_WRITE, // read/write access
                           0,             // high offset:  map from
                           0,             // low offset:   beginning
                           0);           // default: map entire file
                       if(lpvMem == NULL) return FALSE;
                       // Initialize memory if this is the first process
                       if(fInit) memset(lpvMem, '', SHMEMSIZE); 
                       break; 
                   // The attached process creates a new thread
                   case DLL_THREAD_ATTACH: 
                       break; 
                   // The thread of the attached process terminates
                   case DLL_THREAD_DETACH: 
                       break; 
                   // DLL unload due to process termination or FreeLibrary
                   case DLL_PROCESS_DETACH: 
                       // Unmap shared memory from the process's address space
                       fIgnore = UnmapViewOfFile(lpvMem); 
                       // Close the process's handle to the file-mapping object
                       fIgnore = CloseHandle(hMapObject); 
                       break; 
                   default: 
                       break; 
                } 
               return TRUE; 
               UNREFERENCED_PARAMETER(hinstDLL); 
               UNREFERENCED_PARAMETER(lpvReserved); 
           }
            
           // SetSharedMem sets the contents of the shared memory 
           VOID _stdcall SetSharedMem(char *str) 
           {
               char *lpszTmp;
           
               // Get the address of the shared memory block
               lpszTmp = (char *) lpvMem;
               strncpy(lpszTmp,str,SHMEMSIZE);
           } 
            
           // GetSharedMem gets the contents of the shared memory
           VOID _stdcall GetSharedMem(char *str, int size) 
           {
               char *lpszTmp;
           
               // Get the address of the shared memory block
               lpszTmp = (char *) lpvMem;
               strncpy(str,lpszTmp,size);
           }

 

 

 

Edited by faustf
Posted (edited)

 thankz  but  i dont have choice i must use memory mapped file , and  i use autoit only for test the dll, because the dll i must use in other language , therfore is necessary for me use dll 

but i look your work for new project in future  , thabkz so much

 

Edited by faustf

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...