ExitThread API fails

  • Thread starter Thread starter Stephen Martin
  • Start date Start date
S

Stephen Martin

You might want to check the documentation for ExitThread. Any call to
ExitThread never returns since the thread making the call exits.
 
Here is the code.

Dim process, regLib, succeed As Integer
Dim xc, h1, id As Integer
Dim p As String

Select Case func
Case regxEnum.REGISTER
p = "DllRegisterServer"
Case regxEnum.UNREGISTER
p = "DllUnregisterServer"
Case Else
Return resultEnum.INVALID
End Select

regLib = LoadLibraryRegister(Path)

If regLib = 0 Then
Return resultEnum.NOTFOUND
End If

'get the address for either the Register or Unregister
process = GetProcAddressRegister(regLib, p)

If process = 0 Then
FreeLibraryRegister(regLib)
Return resultEnum.NOTACTX
Else
h1 = CreateThreadForRegister(0, 0, process, 0, 0, id)

'unable to create a thread for register or unregister
If h1 = 0 Then
FreeLibraryRegister(regLib)
Return resultEnum.NOTHREAD
Else
succeed = (WaitForSingleObject(h1, 2000) = 0)

If succeed Then
CloseHandle(h1)
FreeLibraryRegister(regLib)
Return resultEnum.SUCCESS
Else

GetExitCodeThread(h1, xc)
ExitThread(xc)

FreeLibraryRegister(regLib)

Return resultEnum.FAILURE
End If
End If
End If

Here is the problem:

I have a program running this code segment on lots of different
machines. However, on only 3 machines this code segment fails. I
have been able to track it down to when ExitThread(xc) is called, it
never returns. The value of xc when this API is called is 259. I am
a very green newbie with threading and am thoroughly confused by this.
I thought, since xc = 259, that means the thread is still running.
Shouldn't ExitThread just kill the thread? Am I missing something
important here? I would love to have some help in understanging this.

As always, thank you very much for your help.
Kalvin
 
Thank you very much for your reply. I like the methods but I still have
a problem.

A little more info, that I thought was not important, but maybe it is.
I have a COM component on lots of users machines. I have an .exe app
that checks the time stamp for the dll on the local machine against one
on the network, if the one on the network is newer, then the application
UnRegisters the local dll, copies the file from the network and
Registers it. When I use the method you suggest, after the dll is
Unregistered, and I attempt to copy the new file over the old file,
replacing it, I get an error that the file is in use. I have tried to
use a call:

Private Declare Function UnRegFile Lib "MyFile.dll" Alias _
"DllUnregisterServer" () As Long

to do unregister it, and a similar one to register it, but get the same
error.

Thank you again.
Kalvin
 
Kalvin,

Can't you run the app twice (perhaps from a batch file), once to
unregister and once to replace the file and re-register?



Mattias
 
Back
Top