Can I create a unique file name based on ProcessID and ThreadID?

I have a delphi web application (Win32) that can run as a CGI, ISAPI or Apache DLL application. I want to be able to generate a unique file name prefix (unique to all current requests at the moment) and consider that the best way to do this is to use processID (to handle CGI mode) as well as threadID (to handle dll).

How do I get a unique process id and thread id in Delphi?

Will they be unique in multi-core / multi-processor situations (on a single server)?

Edit: note that I was advised against this approach, and therefore the accepted answer uses a different method to create temporary file names

+3
source share
7 answers

You have a lot of good ideas presented here.

Does it create an empty file to “get a lock” for the name?

no

no; I believe that we rely on Windows to ensure that the same temp file name is never given twice on the same computer from the time it boots.

Is there any chance of a collision if there is a split second delay between generating the name and creating the file (if I need to create the file myself).

no

no; it will be very bad.

here is the routine I used to get the temporary file.

function GetTemporaryFileName:string;
var
  Path, FileName: array[0..MAX_PATH] of Char;
begin
  Win32Check(GetTempPath(MAX_PATH, Path) <> 0);
  Win32Check(GetTempFileName(Path, '~EX', 0, FileName) <> 0);
  Result:=String(Filename);
end;

instead, you can use FileGetTempName () from JclFileUtils.pas in the JCL.

+5
source

Windows . :

Delphi :

function CreateTempFileName(aPrefix: string): string;
var
  Buf: array[0..MAX_PATH] of Char;
  Temp: array[0..MAX_PATH] of Char;
begin
  GetTempPath(MAX_PATH, Buf);
  if GetTempFilename(Buf, PChar(aPrefix), 0, Temp) = 0 then
  begin
    raise Exception.CreateFmt(sWin32Error, [GetLastError, SysErrorMessage(GetLastError)]);
  end;
  Result := string(Temp);
end;
+5

GUID?

: ,

CreateGuid
GuidToString
+4

. , , , , . ThreadID. , - tmpfile tmpnam ( C, , Delphi ).

, GUID .

+3

1) ThreadID Delphi:

:
. ""
. 32- 0 4294967295

implementation
uses Windows;

procedure MySolution();
var
  myThreadID:Cardinal;  
  myProcessID:Cardinal;
begin
  myThreadID := windows.GetCurrentThreadID;
  myProcessID := windows.GetCurrentProcessId;
end;

2) / ( -)?
: .

, . ( )

, . ( , , )

+3

, , _ tempnam. , . , _tempnam, . , , - . .

_tempnam , . , , tempfile_s. , ... , temp, . tempfile_s , .

EDIT: downvote, C. C, delphi. msvcrt.dll .

function _tempnam(const Dir: PChar, const Prefix: PChar): PChar; cdecl;
  external 'msvcrt.dll' name '_tempnam';
+2

Others gave you good and reasonable ideas, but still - if you use files for temporary storage and if these files will always be created first (it doesn’t matter if there is a residual file with the same name already on the disk, since you will overwrite it anyway) , then the processid_threadid method is perfectly valid.

Use GetCurrentProcessID and GetCurrentThreadID Win32 calls access to these two identifiers.

+1
source

Source: https://habr.com/ru/post/1699288/


All Articles