Login  Register

Re: How to CreateMutex in Dolphin?

Posted by Blair McGlashan-3 on Sep 22, 2004; 9:52am
URL: https://forum.world.st/How-to-CreateMutex-in-Dolphin-tp3371535p3371545.html

"Sean Malloy" <[hidden email]> wrote in message
news:4151154a$[hidden email]...

> Mark,
>
>> Do I maybe have to import/declare this Win32 API function myself or?
>
> Yep.
>
> Biggest help I have found, is having a copy of the Win32 ref (a .hlp
> file).
> Makes for importing api calls much easier.
> ...

We use our own modified version of the Bruce McKinney´s "Hardcore Visual
Basic" Type Library Win32 API type library, and this allows one to generate
functions and structs automagically that typically require little, if any,
modification. This has a number of advantages; it typically saves time, it
is less tedious and error prone, the results are consistent and
reproduceable. You can download it from:

http://object-arts.com/Lib/Downloads/Misc/win32.zip

Its not complete, and because it was designed for VB5/6 (which do not
support unsigned integers) it may have some translations in it of the Win32
SDK header files that are not strictly correct. Our rebuilt version defines
a preprocessor flag that builds a sign aware version of the type-library and
contains some other modifications and extensions, but I can't guarantee it
will be correct in all cases so you should check the generated result
against the documentation on MSDN. Anyway if you want to extend it I have
also made available the IDL source and a VC6 project for building it. For
those that don't have VC6 it would be possible to build it using the MIDL
compiler in the freely downloadable Microsoft SDK.

http://object-arts.com/Lib/Downloads/Misc/win32lib.zip

In the type-library the Win32 API functions are grouped into "modules" that
correspond to the different Win32 DLLs in which their implementations
reside. These map onto different ExternalLibrary subclasses in Dolphin, e.g.
KernelLibrary for Kernel32.dll. The Active-X Component Wizard allows you to
generate a complete module definition, but normally this is more than one
wants so it is best to use a bit of script, e.g.

tlb := AXTypeLibraryAnalyzer open: 'win32.tlb'.
tlb prefix: ''.
module := (tlb at: 'Kernel').
funcs := LookupTable new addAll: (module functions collect: [:each | each
name -> each]); yourself.
#('CreateMutex' 'ReleaseMutex') do: [:each | module generateMethodWrappers:
(funcs at: each)].

I've pasted below the code that was generated from this, which as you will
see is virtually identical to that you've hand-written, with the exception
of the boolean parameter to CreateMutex (better your way).

Hope this is useful for the future,

Blair
-------------------------
!KernelLibrary methodsFor!

createMutex: lpMutexAttributes bInitialOwner: bInitialOwner lpName: lpName
 "Invoke the CreateMutex() function of the module wrapped by the receiver.
 Helpstring: Creates a named or unnamed mutex object

  HANDLE __stdcall CreateMutex(
   [in]void* lpMutexAttributes,
   long bInitialOwner,
   LPSTR lpName);"

 <stdcall: handle CreateMutexA  void* sdword lpstr>
 ^self invalidCall!

releaseMutex: hMutex
 "Invoke the ReleaseMutex() function of the module wrapped by the receiver.
 Helpstring: Releases the Mutex once

  long __stdcall ReleaseMutex(
   HANDLE hMutex);"

 <stdcall: sdword ReleaseMutex  handle>
 ^self invalidCall! !
!KernelLibrary categoriesFor: #createMutex:bInitialOwner:lpName:!**auto
generated**!public! !
!KernelLibrary categoriesFor: #releaseMutex:!**auto generated**!public! !



> Define the required loose methods, and maybe even create a class called
> NamedMutex or something...
>
> Heres the Kernel call..
>
> KernelLibrary>>createMutex: aSECURITYATTRIBUTES bInitialOwner: aBoolOwner
> lpName: aStringName
> "Answer a new Win32 Handle"
> "HANDLE CreateMutex(
>
>    LPSECURITY_ATTRIBUTES lpMutexAttributes, // pointer to security
> attributes
>    BOOL bInitialOwner, // flag for initial ownership
>    LPCTSTR lpName  // pointer to mutex-object name
>   );
>
> "
> <stdcall: handle CreateMutex lpvoid bool lpstr>
> ^self invalidCall
>
> make sure you implement ReleaseMutex as well
>
> KernelLibrary>>releaseMutex: aHandle
> "Cleans up an existing mutex"
> "BOOL ReleaseMutex(
>    HANDLE hMutex // handle of mutex object
>   );
> "
> <stdcall: bool ReleaseMutex handle>
> ^self invalidCall
>
> I've uploaded the win32 api ref here if you don't already have it:
>
> http://www.arcturus.com.au/win32.zip
>
>