CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 6 of 6
  1. #1
    Join Date
    Nov 2002
    Location
    Sofia, Bulgaria
    Posts
    661

    Unique identifiers

    Hey.
    Is there a way to generate unique identifiers for a program. For example here's how it can be done with Visual C++ in a file.

    Code:
    #define UNIQ(s) s##__COUNTER__
    then

    Code:
    int UNIQ(a) = 5; //a0
    int UNIQ(a) = 6; //a1
    etc...

    Unfortunately the counter is restarted per file. Is there a way to make something global for the program.

    Thanks in advance for any help.
    (Visual C++ specific solutions are ok)
    It's only when you look at an ant through a magnifying glass on a sunny day that you realise how often they burst into flames

  2. #2
    Join Date
    Sep 2005
    Location
    London
    Posts
    208

    Re: Unique identifiers

    Quote Originally Posted by SeventhStar
    Is there a way to generate unique identifiers for a program?
    You can use global object Mutexes to set a unique identifiers.
    To create an identifier for a single process, simply pass a NULL to lpName.
    You can create a mutex that refers to the same mutex in MORE than one process, by providing lpName.

    HANDLE CreateMutex(LPSECURITY_ATTRIBUTES lpMutexAttributes,
    BOOL bInitialOwner,
    LPCWSTR lpName);

    Consider the following code:

    Code:
    	HANDLE hMutex;
    	hMutex=CreateMutex(NULL,true, NULL);
    	if (hMutex==NULL)
    	{
                     std::cout<<"Failed to create mutex";
                     return 0;
    	}
    
    	if (getLastError() == ERROR_ALREADY_EXISTS)
    	{
    		// ... Do Something ...
    	}
    EDIT: The code provided will only run for a single process, if you have more than one process that shares the same resource, you should provide the function a unique lpName.

    Regards
    Doron Moraz
    Last edited by Doron Moraz; February 16th, 2008 at 06:23 AM.

  3. #3

    Re: Unique identifiers

    Why do they need to be unique between files? - putting them into a no-name namespace or marking them static will prevent the linker treating them as the same.

  4. #4
    Join Date
    Apr 2005
    Location
    Norway
    Posts
    3,934

    Re: Unique identifiers

    Do you need the unique identifier at compile-time or at runtime?

    At runtime you can create and use LUIDs (Locally Unique Identifiers) that are guaranteed to be unique across the OS (until you restart the system).

    - petter

  5. #5
    Join Date
    Aug 2005
    Location
    LI, NY
    Posts
    576

    Re: Unique identifiers

    Quote Originally Posted by SeventhStar
    Code:
    int UNIQ(a) = 5; //a0
    int UNIQ(a) = 6; //a1
    This looks awful to me. How would it be useful?

    1) The point of named variables is that you can (and should) give a meaningful name to a piece of data. If you have a group of related variables where it would make sense to give them all the same name, in addition to a unique number, guess what... that's an array. For example:

    Code:
    vector<int> a;
    a.push_back(5);
    a.push_back(6);
    
    // or
    
    int b[] = { 5, 6 /*,  etc */ };
    
    // or, if you must
    
    unsigned int counter = 0;
    int c[SOME_SUFFICIENT_NUMBER];
    c[counter++] = 5;
    c[counter++] = 6;
    2) Say you're debugging your code, and the variable a## looks suspicious. Where is this variable declared? Is it UNIQ(a), UNIQ(a), or UNIQ(a), or ...? Also, how do you refer to these variables in your code anyway?
    - Alon

  6. #6
    Join Date
    Apr 2005
    Location
    Norway
    Posts
    3,934

    Re: Unique identifiers

    Quote Originally Posted by Hermit
    This looks awful to me. How would it be useful?
    Ahh. Now I realized what the question was all about. Hermit is correct. It's awful and never any reason for 'randomly' naming variables.

    - petter

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured