tags:

views:

109

answers:

1

Hey i want to create a mutex with the kernel function NtCreateMutant. I did it like this:

Handle hMutex;
NTSTATUS ntMutex = NtOpenMutant(&hMutex,MUTEX_ALL_ACCESS,false);

the NTSTATUS value that is returned: C0000024 STATUS_OBJECT_TYPE_MISMATCH

hope someone can help me with calling NtOpenMutant the right way.

With the windows API OpenMutex(..) its working just fine..

    HANDLE hMutex;
hMutex = OpenMutex(MUTEX_ALL_ACCESS, FALSE, "Name");

Hope someone can explain me how to use the nativ function :)

so the exactly thing i do is this just with native functions:

HANDLE hMutex;
hMutex = OpenMutex(MUTEX_ALL_ACCESS, FALSE, "NameOfMyMutex");
if(hMutex == NULL)
{
    hMutex = CreateMutex(NULL, FALSE, "NameOfMyMutex");
}
else
{
    return FALSE;
}
+1  A: 

Could you please post more code? It's not entirely clear what's going on here just yet, but here are a few thoughts:

1) You start by saying you create your mutex with NtCreateMutant, but the code you posted is using NtOpenMutant. Please clarify exactly what you're actually doing here, preferably with a larger code snippet.

2) NtCreateMutant doesn't take 3 parameters, and NtOpenMutant does not take a boolean 3rd parameter:

+NTSTATUS SERVICECALL
+NtCreateMutant(OUT PHANDLE MutantHandle,
+               IN ACCESS_MASK DesiredAccess,
+               IN POBJECT_ATTRIBUTES ObjectAttributes  OPTIONAL,
+               IN BOOLEAN InitialOwner);
+
+NTSTATUS SERVICECALL
+NtOpenMutant(OUT PHANDLE       MutantHandle,
+             IN  ACCESS_MASK       DesiredAccess,
+             IN  POBJECT_ATTRIBUTES    ObjectAttributes);

It's not clear which you intend to be using, but it would appear regardless of which you meant to use, you may be using it incorrectly.

If you really mean to use NtOpenMutant, it would seem that your third parameter needs to be an OBJECT_ATTRIBUTES structure, defined HERE to be:

typedef struct _OBJECT_ATTRIBUTES {
  ULONG           Length;
  HANDLE          RootDirectory;
  PUNICODE_STRING ObjectName;
  ULONG           Attributes;
  PVOID           SecurityDescriptor;
  PVOID           SecurityQualityOfService;
}  OBJECT_ATTRIBUTES, *POBJECT_ATTRIBUTES;

Keep in mind that the Nt* functions are not exact mirrors of the public and documented Windows API. This seems to be where you're experiencing your issues.

KevenK
Hey thanks i want to avoid the more times execution of my exe file.atm i am using OpenMutex to check if the mutex is already create -> termiante process else i create my mutex with CreateMutex API. So now i want to use the native functions of these APIs.Thats why i am asking if someone could give me a direction to setup the native call right.http://undocumented.ntinternals.net/UserMode/Undocumented%20Functions/NT%20Objects/Mutant/NtOpenMutant.htmlI have my information from this website; there are 3 params written.
Simon
So i have to fill the struct with the right stuff... could someone show me the right direction? or do i have to reverse the call to OpenMutex
Simon
and thanks for this :) its explaining how to fill the struct :) http://msdn.microsoft.com/en-us/library/ff557749%28VS.85%29.aspx
Simon
@Simon: Yes, the link for `NtOpenMutex` takes 3 parameters, as I stated, but it wasn't clear whether you were intending to use `NtOpenMutex` or `NtCreateMutex` (which takes 4 params). But as your link shows (as well as the interface I posted), the third parameter is an `_OBJECT_ATTRIBUTES` struct and not a boolean. Your input of `false` is incorrect.
KevenK