VC模仿Rundll32.exe

来源:互联网 发布:上汽进出口 知乎 编辑:程序博客网 时间:2024/05/22 23:42
/*   * FileName: my_Rundll32.cpp  * Creator: 冷却  * Date: 2009年10月7日  * Comment: 模仿Windows里的一个名为Rundll32.exe的小工具  */    #include <windows.h>   #pragma comment(linker, "/subsystem:windows")     typedef void (*EntryPoint)(                             HWND hwnd,        // handle to owner window                              HINSTANCE hinst,  // instance handle for the DLL                              LPTSTR lpCmdLine, // string the DLL will parse                              int nCmdShow      // show state                              );    int WINAPI WinMain(                     HINSTANCE hInstance,      // handle to current instance                      HINSTANCE hPrevInstance,  // handle to previous instance                      LPSTR lpCmdLine,          // command line                      int nCmdShow              // show state                      )  {      if ( strlen(lpCmdLine)==0 )      {          return -1;      }            char* buf=lpCmdLine;      char* tmp=NULL;      bool haveArg=true;            int n,n1;      int len = strlen(buf);            tmp = strchr(buf, ',');      if ( tmp == NULL )      {          return -1;      }      int commaPos = (tmp-buf);            int spacePos;      tmp = strchr(buf, ' ');      if ( tmp == NULL )      {          spacePos = len;          haveArg = false;      }      else      {          spacePos = (tmp-buf);      }            char* dllName=NULL;      char* funName=NULL;      char* arguments=NULL;            dllName = new char[commaPos+1];      funName = new char[spacePos-commaPos];            if ( haveArg )      {          arguments = new char[len-spacePos];      }      else      {          arguments = new char[1];          strcpy(arguments, "");      }            for ( n=0; n<commaPos; n++ )      {          dllName[n] = buf[n];      }      dllName[commaPos]=0;            for ( n1=0,n=(commaPos+1); n<spacePos; n1++,n++ )      {          funName[n1] = buf[n];      }      funName[spacePos-commaPos-1]=0;            if ( haveArg )      {          for ( n1=0,n=spacePos+1; n<len; n1++,n++)          {              arguments[n1] = buf[n];          }          arguments[len-spacePos-1]=0;      }            //这部分为核心部分,其余的均是为了解析参数       HINSTANCE dll_handle = LoadLibrary(dllName);      if ( dll_handle != NULL )      {          EntryPoint my_fun = (EntryPoint)GetProcAddress(dll_handle, funName);          if ( my_fun != NULL )          {              my_fun((HWND)hInstance, dll_handle, arguments, nCmdShow);          }          else          {              MessageBox(0,"丢失条目!",0,0);          }      }      else      {          MessageBox(0,"找不到指定的模块!",0,0);      }      FreeLibrary(dll_handle);      //             delete[] dllName;      delete[] funName;            if ( haveArg )      {          delete[] arguments;      }            return 0;  }  

原创粉丝点击