'myfunc':本地函数定义是非法的

时间:2012-05-28 08:24:38

标签: visual-studio-2010 visual-c++ service programming-languages

如果是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':本地函数定义是非法的

2 个答案:

答案 0 :(得分: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);

你似乎遭受了重大影响。粘贴错误,将myfunc的定义粘贴到ServiceMain的中间。

同时查看代码的结尾:

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

这种缩进是一种错误的迹象。

答案 1 :(得分:0)

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

void myfunc(HINSTANCE hInstance,
                 HINSTANCE hPrevInstance,
                 LPTSTR    lpCmdLine,
                 int       nCmdShow)