1

如果它是一个 win32 控制台应用程序,我有以下代码运行良好

int APIENTRY _tWinMain(HINSTANCE hInstance,
                     HINSTANCE hPrevInstance,
                     LPTSTR    lpCmdLine,
                     int       nCmdShow)
{
    UNREFERENCED_PARAMETER(hPrevInstance);
    UNREFERENCED_PARAMETER(lpCmdLine);

    // TODO: Place code here.
    MSG msg;

    if( !InitBoard() )
    {
        return 1;
    }

    HWND hDialog = 0;

    hDialog = CreateDialog (hInst, 
        MAKEINTRESOURCE (IDD_MAIN), 
        0, 
        DialogProc);
    if (!hDialog)
    {
        char buf [100];
        wsprintf (buf, "Error x%x", GetLastError ());
        MessageBox (0, buf, "CreateDialog", MB_ICONEXCLAMATION | MB_OK);
        return 1;
    }

#if !defined (_WRITE_MPEG_ )
    InitBufferHandling();
    StartRTPProcess();

    // give vlc some time to get started before shoving buffers at it...
#endif

    Sleep(250);
    if( !StartAcquisition() )
        return 1;

    g_hWnd = hDialog;
    // Main message loop:
    while (GetMessage(&msg, NULL, 0, 0))
    {
        if (!IsDialogMessage(hDialog, &msg))
        {
            TranslateMessage(&msg);
            DispatchMessage(&msg);
        } 
    }

    return (int) msg.wParam;
}

BOOL CALLBACK DialogProc(HWND hwnd, UINT uMessage, WPARAM wParam, LPARAM lParam)
{
  switch (uMessage)
  {
    case WM_CLOSE:
      PostQuitMessage(0);
      return FALSE;
    case WM_COMMAND:
      switch(wParam)
      {
        case IDOK:
          ShutDownAcquisition();
          PostQuitMessage(1);
          return FALSE;

        case IDC_ABOUT:
          DialogBox(hInst, MAKEINTRESOURCE(IDD_ABOUTBOX), hwnd, About);
          return TRUE;

        default:
          break;

      }
    default:
      return FALSE;
  }
}

// Message handler for about box.
INT_PTR CALLBACK About(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)
{
    UNREFERENCED_PARAMETER(lParam);
    switch (message)
    {
    case WM_INITDIALOG:
        return (INT_PTR)TRUE;

    case WM_COMMAND:
        if (LOWORD(wParam) == IDOK || LOWORD(wParam) == IDCANCEL)
        {
            EndDialog(hDlg, LOWORD(wParam));
            return (INT_PTR)TRUE;
        }
        break;
    }
    return (INT_PTR)FALSE;
}


int SelectBoard()
{
  DialogBox(hInst, MAKEINTRESOURCE(IDD_SELECT_BOARD), g_hWnd, SelectBoardProc);
  return g_SelectedBoard;
}

BOOL CALLBACK SelectBoardProc(HWND hDlg, UINT uMessage, WPARAM wParam, LPARAM lParam)
{
  switch (uMessage)
  {
    case WM_INITDIALOG:
      {
        int nBoards = GetBoardCount();
        SelBoard BoardInfo;
        char ButtonCaption[64];
        int  nButtonHeight = 17;

        for( int i = 0; i < nBoards; i++ )
        {
          GetBoardSelection( i, &BoardInfo );
          wsprintf( ButtonCaption, "%s %s %s", BoardInfo.szBoardName, BoardInfo.szSerialNumber, 
              BoardInfo.bClaimed ? "(Busy)" : " " );

          CreateWindowEx(  0, TEXT("BUTTON"), ButtonCaption, WS_CHILD | WS_VISIBLE | BS_AUTORADIOBUTTON,
                         7, 7 + i*nButtonHeight, 282, nButtonHeight, hDlg, (HMENU)(BOARD_RADIO_1 + i),  hInst, 0 );
        }
        g_SelectedBoard = 0;
      }
          return (INT_PTR)TRUE;

    case WM_COMMAND:
      if (HIWORD(wParam) == BN_CLICKED) 
      {
           switch (LOWORD(wParam)) 
         {
          case BOARD_RADIO_1:
          case BOARD_RADIO_1 + 1:
          case BOARD_RADIO_1 + 2:
          case BOARD_RADIO_1 + 3:
          case BOARD_RADIO_1 + 4:
          case BOARD_RADIO_1 + 5:
            g_SelectedBoard = LOWORD(wParam) - BOARD_RADIO_1;
            return TRUE;
            break;

          case IDOK:
            EndDialog(hDlg, LOWORD(wParam));
            return FALSE;

         }
      }

  }

  return FALSE;
}

我想将上面的代码转换为窗口服务,所以当我这样做时:-

void main() 
{ 
   SERVICE_TABLE_ENTRY ServiceTable[2];
   ServiceTable[0].lpServiceName = "MemoryStatus";
   ServiceTable[0].lpServiceProc = (LPSERVICE_MAIN_FUNCTION)ServiceMain;

   ServiceTable[1].lpServiceName = NULL;
   ServiceTable[1].lpServiceProc = NULL;
   // Start the control dispatcher thread for our service
   StartServiceCtrlDispatcher(ServiceTable);  
}

// Service initialization
int InitService() 
{ 
   int result;
   result = WriteToLog("Monitoring started.");
   return(result); 
}

void ServiceMain(int argc, char** argv) 
{ 
   int error; 
   MEMORYSTATUS memory;
   char buffer[16];
   int result;

   ServiceStatus.dwServiceType = SERVICE_WIN32; 
   ServiceStatus.dwCurrentState = SERVICE_START_PENDING; 
   ServiceStatus.dwControlsAccepted   =  SERVICE_ACCEPT_STOP | SERVICE_ACCEPT_SHUTDOWN;
   ServiceStatus.dwWin32ExitCode = 0; 
   ServiceStatus.dwServiceSpecificExitCode = 0; 
   ServiceStatus.dwCheckPoint = 0; 
   ServiceStatus.dwWaitHint = 0; 

   hStatus = RegisterServiceCtrlHandler(
      "MemoryStatus", 
      (LPHANDLER_FUNCTION)ControlHandler); 
   if (hStatus == (SERVICE_STATUS_HANDLE)0) 
   { 
      // Registering Control Handler failed
      return; 
   }  
   // Initialize Service 
   error = InitService(); 
   if (error) 
   {
      // Initialization failed
      ServiceStatus.dwCurrentState = SERVICE_STOPPED; 
      ServiceStatus.dwWin32ExitCode = -1; 
      SetServiceStatus(hStatus, &ServiceStatus); 
      return; 
   } 

   // We report the running status to SCM. 
   ServiceStatus.dwCurrentState = SERVICE_RUNNING; 
   SetServiceStatus (hStatus, &ServiceStatus);

   // The worker loop of a service
   while (ServiceStatus.dwCurrentState == SERVICE_RUNNING)
   {void myfunc(HINSTANCE hInstance,
                     HINSTANCE hPrevInstance,
                     LPTSTR    lpCmdLine,
                     int       nCmdShow)
{
    UNREFERENCED_PARAMETER(hPrevInstance);
    UNREFERENCED_PARAMETER(lpCmdLine);

    // TODO: Place code here.
    MSG msg;

    if( !InitBoard() )
    {
        return 1;
    }

    HWND hDialog = 0;

    hDialog = CreateDialog (hInst, 
        MAKEINTRESOURCE (IDD_MAIN), 
        0, 
        DialogProc);
    if (!hDialog)
    {
        char buf [100];
        wsprintf (buf, "Error x%x", GetLastError ());
        MessageBox (0, buf, "CreateDialog", MB_ICONEXCLAMATION | MB_OK);
        return 1;
    }

#if !defined (_WRITE_MPEG_ )
    InitBufferHandling();
    StartRTPProcess();

    // give vlc some time to get started before shoving buffers at it...
#endif

    Sleep(250);
    if( !StartAcquisition() )
        return 1;

    g_hWnd = hDialog;
    // Main message loop:
    while (GetMessage(&msg, NULL, 0, 0))
    {
        if (!IsDialogMessage(hDialog, &msg))
        {
            TranslateMessage(&msg);
            DispatchMessage(&msg);
        } 
    }

    //return (int) msg.wParam;
    if (eventchecking)
      {
         ServiceStatus.dwCurrentState = SERVICE_STOPPED; 
         ServiceStatus.dwWin32ExitCode      = -1; 
         SetServiceStatus(hStatus, &ServiceStatus);
         return;
      }
      Sleep(SLEEP_TIME);
   }
  // return; 
}
   return;
}

它给了我一个错误 C2601: 'myfunc' : 本地函数定义是非法的

4

2 回答 2

1

从您的第二个代码块:

// The worker loop of a service
   while (ServiceStatus.dwCurrentState == SERVICE_RUNNING)
   {void myfunc(HINSTANCE hInstance,
                     HINSTANCE hPrevInstance,
                     LPTSTR    lpCmdLine,
                     int       nCmdShow)
{
    UNREFERENCED_PARAMETER(hPrevInstance);
    UNREFERENCED_PARAMETER(lpCmdLine);

您似乎遇到了严重的剪切和粘贴错误,将 的定义粘贴myfuncServiceMain.

还要看看代码的末尾:

      Sleep(SLEEP_TIME);
   }
  // return; 
}
   return;
}

缩进是错误的标志。

于 2012-05-28T08:38:34.100 回答
0

从 while 循环中注释掉以下行 -

void myfunc(HINSTANCE hInstance,
                 HINSTANCE hPrevInstance,
                 LPTSTR    lpCmdLine,
                 int       nCmdShow)
于 2012-05-28T08:37:56.973 回答