Shared pain
Steven Schveighoffer
schveiguy at yahoo.com
Thu Nov 18 05:33:12 PST 2010
On Thu, 18 Nov 2010 06:26:39 -0500, Steve Teale
<steve.teale at britseyeview.com> wrote:
> I had D code that provided a basis for creation of Windows services,
> which I have just tried to get working with the latest D2.
>
> No dice.
>
> The point of failure was in this method
>
> static void StartService()
>
> {
>
> if (!StartServiceCtrlDispatcherA(cast(SERVICE_TABLE_ENTRY *)
> &_sta[0]))
>
> {
> ...
>
> }
>
> }
>
> Where _sta is an array of SERVICE_TABLE_ENTRY structs. The bits of code
> that install and remove the service worked fine, but the service control
> dispatcher was clearly not happy about these structs being in TLS, and
> the service crashed immediately when I tried to start it.
I think this has something to do with how services start. From this page:
http://msdn.microsoft.com/en-us/library/ms685990(v=VS.85).aspx
I see that StartServiceCtrlDispatcher creates its own thread to run
ServiceMain, which most likely does not call the D thread initialization
routines (not sure). TLS may not be properly set up inside your thread
that is running ServiceMain, and probably the static this() module
functions have not been called.
>
> The class also has a couple of methods with signatures like:
>
> extern (Windows) static export void service_ctrl(uint dwCtrlCode) {...}
>
> That's probably not relevant, but I think it contributed to a host of
> errors that finally made be take a big step back.
>
> I re-implemented the thing in the style used for OOP in C.
>
> struct ServiceBase
> {
> SERVICE_TABLE_ENTRY[2] _sta;
> ...
> }
>
> __gshared ServiceBase __sb;
>
> void initialize(ServiceBase* sb) { ... }
> void whatever(ServiceBase* sb ...) { ... }
>
> and used the __sb global within the functions with the Windows signature,
> which just became
>
> extern (Windows) export void service_ctrl(uint dwCtrlCode) {...}
>
> Now it's working again, but I want to change it into idiomatically sound
> D code.
>
> Style suggestions please!
I think using a class in D is a good idea. But what you may need is
global functions which forward to your class methods. This is how I would
probably do it.
-Steve
More information about the Digitalmars-d
mailing list