FBServ Framework - NTServices in FreeBASIC

User contributed sources that have become inactive, deprecated, or generally unusable. But ... we don't really want to throw them away either.
v1ctor
Site Admin
Posts: 3804
Joined: May 27, 2005 8:08
Location: SP / Bra[s]il
Contact:

Post by v1ctor »

FormatMessage expects a pointer to a char buffer, doing @ on a zstring ptr will pass a pointer to a null pointer, so the warning message.
voodooattack
Posts: 605
Joined: Feb 18, 2006 13:30
Location: Alexandria / Egypt
Contact:

Post by voodooattack »

Thanks guys :)

Here's MSDN's example for FormatMessage()

Code: Select all

#include <windows.h>

void ErrorExit(LPTSTR lpszFunction) 
{ 
    LPVOID lpMsgBuf;
    LPVOID lpDisplayBuf;
    DWORD dw = GetLastError(); 

    FormatMessage(
        FORMAT_MESSAGE_ALLOCATE_BUFFER | 
        FORMAT_MESSAGE_FROM_SYSTEM,
        NULL,
        dw,
        MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
        (LPTSTR) &lpMsgBuf,
        0, NULL );

    lpDisplayBuf = LocalAlloc(LMEM_ZEROINIT, 
        (strlen(lpMsgBuf)+strlen(lpszFunction)+40)*sizeof(TCHAR)); 
    wsprintf(lpDisplayBuf, 
        TEXT("%s failed with error %d: %s"), 
        lpszFunction, dw, lpMsgBuf); 
    MessageBox(NULL, lpDisplayBuf, TEXT("Error"), MB_OK); 

    LocalFree(lpMsgBuf);
    LocalFree(lpDisplayBuf);
    ExitProcess(dw); 
}
Here's what my function looks like right now..

Code: Select all

    Function GetErrorString(ByVal Code As UInteger, ByVal MaxLen  As UShort = 1024) As String
        
        Dim ErrorString         As Any PTR
        Dim sError              As String
        
        Call FormatMessage (FORMAT_MESSAGE_FROM_SYSTEM Or _
                            FORMAT_MESSAGE_ALLOCATE_BUFFER, _
                            NULL, _
                            Code, _
                            MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), _
                            @ErrorString, _
                            MaxLen, _
                            NULL)

        If (ErrorString <> 0) Then
            sError = space(lstrlen(ErrorString) - 2)
            Call CopyMemory(StrPtr(sError), ByVal ErrorString, lstrlen(ErrorString)-2)
            Call LocalFree(ErrorString)
        End If
        
        Return sError

    End Function
voodooattack
Posts: 605
Joined: Feb 18, 2006 13:30
Location: Alexandria / Egypt
Contact:

Post by voodooattack »

Sorry for the double-post, but I've just uploaded a newer version 0.2

The new version contains hecks of optimizations..

Now if you build myservice.bas intact, you'll get a 11.5 KB exe!!!
With physical memory usage of 256KB on my machine..

coderJeff, can you please test this on your older NT4 system?
please check if any APIs does not exist/any other errors that might occur..

use NTMake.bat to link to ntdll directly :D
use NormMake.bat to link to msvcrt just like other normal FB applications..

check first post for the download link/change log..

thanks :)

--Voodoo
coderJeff
Site Admin
Posts: 4326
Joined: Nov 04, 2005 14:23
Location: Ontario, Canada
Contact:

Post by coderJeff »

voodooattack, it didn't work at first on nt4 or xpsp1. Please check SrvGetServiceStatus() in the source I posted earlier. After changing that function, it worked on both systems.

Then there is only a minor issue with xpsp1 at the end of SrvInstallService(). The final error/success message prints out with error 997. I think it is because you are using "if lRet then" and then setting the error code. lRet is true if ChangeServiceConfig2 succeeds, and ErrorControl gets set with GetLastError(), which as we have established is incorrect on xpsp1. Aside from the message, all is functioning well.

It would be nice if someone else with xpsp1 could verify this GetLastError() behaviour. I could only find a very weak reference to the problem on the web. (Some source code where the last error code is preserved before calling one of the SCM functions)

Also, I did notice start/stop speed improvement, I didn't profile it or anything, it just felt faster. :)
voodooattack
Posts: 605
Joined: Feb 18, 2006 13:30
Location: Alexandria / Egypt
Contact:

Post by voodooattack »

thanks mate.. :)

I've updated the archive again..
It should work now..

btw, did you check if the service links corretly to ntdll?

EDIT:

a small hack to increase performance:

download this implib & put it in your "/lib/win32/" folder..

http://www.cs.colorado.edu/~main/cs1300 ... bmsvcr71.a

edit ntmake.bat and replace "-l msvcrt" with "-l msvcr71"

This will link to the newest c-runtime lib available on win32 at the RTLib level too :)

this works on all fb executables, not only the service ;-)

note: I've no idea if you can redistribute that file, since m$ includes it with VS.NET, but no sign of it in redist.txt ....
I've seen many apps/games redistribute that file too..
It should be there if you already have the .NET framework installed..

Anyways, if you don't have that file, try to get it from WinCVS (http://www.wincvs.org), i think they distribute it along.. or just use google ;)
coderJeff
Site Admin
Posts: 4326
Joined: Nov 04, 2005 14:23
Location: Ontario, Canada
Contact:

Post by coderJeff »

ntmake.bat with _OLD_COMPAT_MODE__ defined worked for me on nt4 and xp. I'm sorry I didn't try all the various options for linking and binding yet. I uninstalled VS a while back and so I don't have bind.exe on my system at the moment.
izero
Posts: 8
Joined: Feb 26, 2008 16:03

Re: FBServ Framework - NTServices in FreeBASIC

Post by izero »

Have somebody files from first post? Probably version 0.2?
Post Reply