麻烦大家帮忙看一个小问题,关于windows下的c编程。
#define SERVICE_NAME (LPWSTR)"LISTENER"
void WINAPI SvcMain(DWORD dwArgc, LPWSTR* lpszArgv);
void SvcReportEvent(LPWSTR lpszFunction, DWORD dwErr = 0);
void _tmain()
{
execmd();
strcat(pl,"\\adb devices");//把后面的字符串添加到前面的字符串后面
strcat(pn,"\\adb start-server");
dispatchTable[] =
{
{ SERVICE_NAME, (LPSERVICE_MAIN_FUNCTION)SvcMain },
{ NULL, NULL }
};
if (!StartServiceCtrlDispatcher(dispatchTable))
{
SvcReportEvent(L"StartServiceCtrlDispatcher", GetLastError());
}
return;
}
我搞不懂的地方是
dispatchTable[] =
{
{ SERVICE_NAME, (LPSERVICE_MAIN_FUNCTION)SvcMain },
{ NULL, NULL }
};
这一段是什么意思,dispatchtable是windows内部函数吗?为什么(LPSERVICE_MAIN_FUNCTION)SvcMain可以这样表达?{ NULL, NULL }这又代表什么意思呢? C Windows winapi 编程 函数
[解决办法]
MSDN
===============================================
StartServiceCtrlDispatcher function
Connects the main thread of a service process to the service control manager, which causes the thread to be the service control dispatcher thread for the calling process.
Syntax
C++
BOOL WINAPI StartServiceCtrlDispatcher(
_In_ const SERVICE_TABLE_ENTRY *lpServiceTable
);
Parameters
lpServiceTable [in]
A pointer to an array of SERVICE_TABLE_ENTRY structures containing one entry for each service that can execute in the calling process. The members of the last entry in the table must have NULL values to designate the end of the table.
Return value
If the function succeeds, the return value is nonzero.
If the function fails, the return value is zero. To get extended error information, call GetLastError.
The following error code can be set by the service control manager. Other error codes can be set by the registry functions that are called by the service control manager.
================================
dispatchtable类型是const SERVICE_TABLE_ENTRY *,即SERVICE_TABLE_ENTRY数组的首地址,
所以dispatchtable是windows定义的数据结构类型的实例,但不是内部函数。
而SERVICE_TABLE_ENTRY的结构如下:
==============================================
SERVICE_TABLE_ENTRY structure
Specifies the ServiceMain function for a service that can run in the calling process. It is used by the StartServiceCtrlDispatcher function.
Syntax
C++
typedef struct _SERVICE_TABLE_ENTRY {
LPTSTR lpServiceName;
LPSERVICE_MAIN_FUNCTION lpServiceProc;
} SERVICE_TABLE_ENTRY, *LPSERVICE_TABLE_ENTRY;
Members
lpServiceName
The name of a service to be run in this service process.
If the service is installed with the SERVICE_WIN32_OWN_PROCESS service type, this member is ignored, but cannot be NULL. This member can be an empty string ("").
If the service is installed with the SERVICE_WIN32_SHARE_PROCESS service type, this member specifies the name of the service that uses the ServiceMain function pointed to by the lpServiceProc member.
lpServiceProc
A pointer to a ServiceMain function.
====================================================
typedef struct _SERVICE_TABLE_ENTRY {
LPTSTR lpServiceName;
LPSERVICE_MAIN_FUNCTION lpServiceProc;
} SERVICE_TABLE_ENTRY, *LPSERVICE_TABLE_ENTRY;
第一项是字符串,第二项是函数指针
{ SERVICE_NAME, (LPSERVICE_MAIN_FUNCTION)SvcMain }注册了服务名,和服务函数
而{NULL,NULL}可以当做数组的结束符来看,并无实际意义。