2012-06-09 1 views
0

Je travaille sur une application dans Delphi 7 qui va exécuter et afficher le journal créé par FastMM4.pas.Delphi 7, utilisez FastMM4 pour obtenir le chemin d'application et l'application ouverte

L'application sera installée n'importe où sur le système. Je l'ai modifié le FastMM4.pas afin qu'il CreateProcess (inshort exécuter ma demande) Le code de mon previous Question et Sertac Akyuz's answer

Le leakTracker.exe prendra dans le fichier journal du fastmm4 comme paramètre et ouvrez le fichier et l'affichage. Le fastMM4.pas modifié sera utilisé dans toute autre application.

Procedure OpenTheLeakReader 
begin 
CmdLine := 'C:\Program Files\leakTracker\leakTracker.exe "'; 
lstrcat(CmdLine,CTheArGuements); 
ZeroMemory(@SInfo, SizeOf(SInfo)); 
SInfo.cb := SizeOf(SInfo); 
CreateProcess(nil, CmdLine, nil, nil, False, NORMAL_PRIORITY_CLASS, nil, nil, sInfo, pInfo); 
end; 

Cela fonctionne bien mais j'ai hardcoded le path parce que pour obtenir mon chemin d'application ..

    [FastMM4] -cannot use SysUtils.pas  //*1 
          -cannot use Registry.pas  //*2 
          -cannot use ParamStr(0)  //*3 
          -cannot use unitWithSysRegis //*4 

       [someAplicationWhichWillUseFastMM4] -Uses FastMM4.pas 

dans le FAstMM4.pasfinalization i cette

  if ifLeakedMemory then OpenTheLeakReader; 

depuis que je ne peux pas avoir

*1 - SysUtils.pas - à FastMM4.pass car cela désinstaller fastmmm4

*2 - Registry.pas - pour rechercher leakTracker chemin d'installation, mais désinstallerez fastmm4

*3 - paramstr(0) - il donne une erreur à la fin de l'application.

*4 - unitWithSysRegis - avec SysUtils, le registre n'est pas non plus possible dans la clause d'utilisation de Fastm4.

Donc je suis coincé comme comment obtenir le chemin de leakTracker.exe et envoyer le chemin du fichier journal à `leakTracker.exe 'via CreateProcess.

+1

Vous pouvez importer les [fonctions de registre] (Windows http://msdn.microsoft.com/en-us/library/windows/desktop/ms724875 .aspx) vous-même? – Blorgbeard

+0

@Blorgbeard je n'ai pas comment faire cela .. tous les exemples? – PresleyDias

+0

En fait, pouvez-vous utiliser l'unité 'windows'? Il a 'RegOpenKeyEx',' RegGetValueEx' etc. Sinon, vous pouvez copier les définitions à partir de là. – Blorgbeard

Répondre

2

(Pour une première explication (concernant la question liée dans la question), cette question ne concerne pas seulement l'impossibilité d'utiliser des unités (ayant des sections d'initialisation nécessitant une allocation de mémoire) dans FastMM4.pas OP pense que son code courir après FastMM finalise le gestionnaire de mémoire. FastMM soulève une exception si la mémoire est allouée après cela, si l'allocation de mémoire via RTL est interdite.)


Utilisez les fonctions de registre de api ainsi qu'il est indiqué dans la question précédente ou Blorgbeard commentait à cette question. Incorporé avec le code précédent, il deviendrait quelque chose comme ceci:

var 
    hReg: HKEY; 
    Ret: Longint; 
    RegDataType, RegDataSize: DWORD; 
    CmdLine: array [0..560] of Char; // increase as needed 
    Len: Integer; 
    SInfo: TStartupInfo; 
    PInfo: TProcessInformation; 


initialization 
{$ifndef BCB} 
    // fastmm code 
{$endif} 

finalization 
{$ifndef PatchBCBTerminate} 
    FinalizeMemoryManager; // fastmm code 



    Ret := windows.RegOpenKeyEx(HKEY_LOCAL_MACHINE, 
        'SOFTWARE\[YourProgram]', // registry key to your program's path 
        0, KEY_READ, hReg); 

    RegDataType := REG_SZ; 
    RegDataSize := 260; 
    Ret := windows.RegQueryValueEx(hReg, 
        'path',  // registry value containing your program's path 
        nil, @RegDataType, @CmdLine, @RegDataSize); 
    RegCloseKey(hReg); 

    CmdLine[RegDataSize - 1] := ' '; 
    CmdLine[RegDataSize] := '"';  // open doublequote in case spaces in path 
    Len := windows.GetModuleFileName(0, 
      PChar(@CmdLine[RegDataSize + 1]), 260) + RegDataSize; 

    while CmdLine[Len] <> '.' do  // assumes executable has an extension 
    Dec(Len); 
    CmdLine[Len] := #0; 
    lstrcat(CmdLine, '_MemoryManager_EventLog.txt"'#0); // closing doublequote 

    ZeroMemory(@SInfo, SizeOf(SInfo)); 
    SInfo.cb := SizeOf(SInfo); 
    CreateProcess(nil, CmdLine, nil, nil, False, 
       NORMAL_PRIORITY_CLASS, nil, nil, sInfo, pInfo); 

{$endif} 
end.