FBServ Framework - NTServices in FreeBASIC
-
- Posts: 605
- Joined: Feb 18, 2006 13:30
- Location: Alexandria / Egypt
- Contact:
Thanks guys :)
Here's MSDN's example for FormatMessage()
Here's what my function looks like right now..
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);
}
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
-
- Posts: 605
- Joined: Feb 18, 2006 13:30
- Location: Alexandria / Egypt
- Contact:
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
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
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. :)
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. :)
-
- Posts: 605
- Joined: Feb 18, 2006 13:30
- Location: Alexandria / Egypt
- Contact:
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 ;)
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 ;)
Re: FBServ Framework - NTServices in FreeBASIC
Have somebody files from first post? Probably version 0.2?