Semaphores question

Im creating a semaphore with a certain name.
Then Im trying to create it again with the same parameters.
CreateSemaphore creates two semaphores, while it should return me the previous semaphore handle. Why is this happening?
I am supposed to get a last error of ERROR_ALREADY_EXISTS, but Im not...

These are my declarations:
--------------------------
Private Declare Function CreateSemaphore Lib "kernel32" Alias "CreateSemaphoreA" (ByRef lpSemaphoreAttributes As SECURITY_ATTRIBUTES, ByVal lInitialCount As Long, ByVal lMaximumCount As Long, ByVal lpName As String) As Long
Private Type SECURITY_ATTRIBUTES
        nLength As Long
        lpSecurityDescriptor As Long
        bInheritHandle As Long
End Type

This is the subroutine:
-----------------------
 Dim sec As SECURITY_ATTRIBUTES
 sec.bInheritHandle = 1&
 sec.nLength = Len(sec)
 Dim Hsem As Long
 Hsem = CreateSemaphore(sec, 0, 1, "Hello")
 msgbox Hsem
 Hsem = CreateSemaphore(sec, 0, 1, "Hello")
 msgbox Hsem
----------------

Help please! :-)
ViniTAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

danlevansCommented:
When you CreateSemaphore on an already open handle of the same name, you get both a valid handle return and the error ERROR_ALREADY_EXISTS . Check your error codes even if you get a valid handle back. In this case it should be the same handle as you got on the first call.

Have fun

Dan
0
ViniTAuthor Commented:
Im sorry, but GetError always returns me Operation succeeded!
0
ViniTAuthor Commented:
Nothing works!
Im using the same parameters in a sequent call to CreateSemaphore and it returns me two different handles!!! why is this happening?!
0
The Ultimate Tool Kit for Technolgy Solution Provi

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy for valuable how-to assets including sample agreements, checklists, flowcharts, and more!

corvanderlindenCommented:
I tried your code, and I also got two different values for hsem.
BUT
err.LastDllError gives me number 183 (already exists) on the second call

If you call GetLastError() it is possible that it returns 0 because under the hood VB is making more calls to the WinAPI, so be sure to use err.LastDllError
0
ViniTAuthor Commented:
Still, why is it returning the a new number for the semaphore?
0
corvanderlindenCommented:
The documentation says:

If the function succeeds, the return value is a handle to the semaphore object. If the named semaphore object existed before the function call, the function returns a handle to the existing object and GetLastError returns ERROR_ALREADY_EXISTS.

It does not say it has to be the same handle, you just get a handle to the same semaphore. Also if you call OpenSemaphore() you get a different handle to the same object. Do you want the same handle call DuplicateHandle.

(With API calls you must always check the return code or GetLastError)

0
corvanderlindenCommented:
I forgot to mention that if you create two processes of which each creates the semaphore, you get of course in each process the same handle

Doing a create twice in the same process gives different handles because (in my opinion) you have to have different handles in order to be able close each handle.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
ViniTAuthor Commented:
Thanks.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic Classic

From novice to tech pro — start learning today.