2016-07-09 27 views
-1

Während meines alten 32-Bit-Code zu 64-Bit-Portierung, erhalte ichMapFileMemory (dwAllocSize: DWord); 'Haken DLL', 'map Datei kann nicht'

'Haken DLL', 'kann nicht Map-Datei'

Was soll ich tun falsch?

var 
    hObjHandle: THandle; //Variable for the file mapping object 
    lpHookRec: PHookRec; //Pointer to our hook record 

procedure MapFileMemory(dwAllocSize: DWord); 
begin //MapFileMemory 
    //Create a process wide memory mapped variable 
    hObjHandle := CreateFileMapping($FFFFFFFF, nil, PAGE_READWRITE, 0, dwAllocSize, 'HookRecMemBlock'); 
    if (hObjHandle = 0) then 
    begin 
     MessageBox(0, 'Hook DLL', 'Could not create file map object', mb_Ok); 
     exit 
    end;// (hObjHandle = 0) 

    //Get a pointer to our process wide memory mapped variable 
    lpHookRec := MapViewOfFile(hObjHandle, FILE_MAP_WRITE, 0, 0, dwAllocSize); 
    if (lpHookRec = nil) then 
    begin 
     CloseHandle(hObjHandle); 
     MessageBox(0, 'Hook DLL', 'Could not map file', mb_Ok); 
     exit 
    end //lpHookRec = Nil) 
end; //MapFileMemory 

procedure UnMapFileMemory; 
begin //UnMapFileMemory 
    //Delete our process wide memory mapped variable 
    if (lpHookRec <> nil) then 
    begin 
     UnMapViewOfFile(lpHookRec); 
     lpHookRec := nil 
    end; // (lpHookRec <> Nil) 
    if (hObjHandle > 0) then 
    begin 
     CloseHandle(hObjHandle); 
     hObjHandle := 0 
    end //(hObjHandle > 0) 
end; // UnMapFileMemory 


procedure DllEntryPoint(dwReason: DWord); 
begin { DllEntryPoint } 
    case dwReason of 
    Dll_Process_Attach: 
     begin 
     {if we are getting mapped into a process, then get} 
     {a pointer to our process wide memory mapped variable} 
     hObjHandle := 0; 
     lpHookRec := nil; 
     MapFileMemory(sizeof(lpHookRec^)) 
     end; 
    Dll_Process_Detach: 
     begin 
     {if we are getting unmapped from a process then, remove} 
     {the pointer to our process wide memory mapped variable} 
     UnMapFileMemory 
     end; 
    end { case dwReason } 
end; { DllEntryPoint } 
+0

Dies ist Code aus dem Internet. –

+1

Lesen Sie die Dokumentation. Ich wette, dass bei 64 Bit der erste Arg in CreateFileMapping nicht $ ffffffff ist. Übergeben Sie INVALID_HANDLE_VALUE. Versuchen Sie, Ihren Code zu verstehen, anstatt ihn blind zu kopieren –

+0

Warum möchten Sie Ihr Projekt auf 64 Bits ändern? Ein 32-Bit-Programm wird auch von x64-Prozessoren unterstützt. :-) –

Antwort

0

Sie müssen die entsprechenden Änderungen vornehmen für die Tatsache zu berücksichtigen, dass Sie sich auf 64-Bit laufen.

Oh und lesen Sie auf Raymond Chens essential writings on DllMain und the followup.
Sie sollten wirklich nicht die Zuordnung der Speicherabbilddatei in DllMain tun.
Stattdessen sollten Sie warten, bis Ihr DLL-Client beginnt, eine der Arbeitsroutinen in Ihrer DLL aufrufen und erst dann das Mapping.

Darüber hinaus ist es immer eine gute Idee zu include the actual error, wenn Sie debuggen.

var 
    hObjHandle: THandle; //Variable for the file mapping object 
    lpHookRec: PHookRec; //Pointer to our hook record 

procedure MapFileMemory(dwAllocSize: NativeUInt); 
begin 
    if Assigned(lpHookRec) then exit; 
    hObjHandle := CreateFileMapping(INVALID_HANDLE_VALUE, nil, PAGE_READWRITE, 0, dwAllocSize, 'HookRecMemBlock'); 
    if (hObjHandle = 0) then begin 
    MessageBox(0, 'Hook DLL', 'Could not create file map object '#10#13+ 
       SysErrorMessage(GetLastError), mb_Ok); 
    exit; 
    end; 

    //Get a pointer to our process wide memory mapped variable 
    lpHookRec := MapViewOfFile(hObjHandle, FILE_MAP_WRITE, 0, 0, dwAllocSize); 
    if (lpHookRec = nil) then begin 
    CloseHandle(hObjHandle); 
    MessageBox(0, 'Hook DLL', 'Could not map file '#10#13+ 
       SysErrorMessage(GetLastError), mb_Ok); 
    exit; 
    end; 
end; 

procedure UnMapFileMemory; 
begin 
    //Delete our process wide memory mapped variable 
    if Assigned(lpHookRec) then begin 
    UnMapViewOfFile(lpHookRec); 
    lpHookRec := nil; 
    end; 
    if (hObjHandle <> 0) then begin 
    CloseHandle(hObjHandle); 
    hObjHandle := 0; 
    end; 
end; 

var 
    CS: TRTLCriticalSection;  

procedure DllEntryPoint(dwReason: DWord); 
begin { DllEntryPoint } 
    case dwReason of 
    Dll_Process_Attach: begin 
     {if we are getting mapped into a process, then get} 
     {a pointer to our process wide memory mapped variable} 
     hObjHandle := 0; 
     lpHookRec := nil; 
     //do not call MapMemFile here, do it when your work-routine is called for the first time. 
    end; 
    Dll_Process_Detach: begin 
     {if we are getting unmapped from a process then, remove} 
     {the pointer to our process wide memory mapped variable} 
     EnterCriticalSection(CS); 
     if Assigned(lpHookRec) then UnMapFileMemory; 
     LeaveCriticalSection(CS); 
    end; 
    end; { case dwReason } 
end; { DllEntryPoint } 
+2

Außer den Aufrufen von 'MessageBox' war alles andere sicher von' DllMain'. Dies wird unter [Best Practices für die Dynamic Link Library] (https://msdn.microsoft.com/en-us/library/windows/desktop/dn633971.aspx) explizit erläutert. – IInspectable

+0

Wow, das war jedoch 1) Ich kann nicht SysErrorMessage (GetLastError) Funktion zu wissen, die tatsächlichen Fehler hinzufügen (sein kann, brauche ich einige Einheit enthalten, raten Sie bitte) 2) Wenn ich das nicht enthalten MapFileMemory (sizeof (lpHookRec ^)) in DllMain, wie ermittle ich die "First Use" und führe diese Funktion aus? – bhattji