CSDN首页 空间 新闻 论坛 Blog 下载 读书 网摘 搜索 .NET Java 视频 接项目 求职 在线学习 买书 程序员 通知
花落谁家,你作主! 盛大widget设计大赛英雄榜
CSDN社区
搜索 收藏 打印 关闭
CSDN社区 >  C++ Builder >  Windows SDK/API

关于两个API在不同操作系统下的使用

楼主Cryes(yes)2005-06-02 20:13:28 在 C++ Builder / Windows SDK/API 提问

我写了个程序,其中用到两API,分为NT和非NT两种情况。但是为什么在WIN95下就运行不了?或有什么解决方法?  
   
  if(IsWinNT())  
    {  
          ::CreateProcessWithLogonW(...);  
              ....  
    }  
  else  
    {      
        CreateProcess(.....);          
          .....  
    }    
  问题点数:50、回复次数:10Top

1 楼jishiping(JSP 季世平)回复于 2005-06-02 20:33:59 得分 0

为什么在WIN95下就运行不了?  
  ---------------------------------  
  你又不贴代码,别人怎么知道?光写个   CreateProcess(.....);   别人就知道是什么错了?Top

2 楼Cryes(yes)回复于 2005-06-02 20:45:00 得分 0

哦,原来是季老大,不好意思.我把部分代码贴出来吧.希望能帮我解决问题.  
  在win95上运行的时候,提示少了advapi32.dll,就是CreateProcessWithLogonW()函数有问题.但在WINNT下就没问题.  
   
  BOOL   IsWinNT(void)  
  {  
          OSVERSIONINFO   OSVersionInfo;  
          OSVersionInfo.dwOSVersionInfoSize   =   sizeof   (OSVERSIONINFO);  
          if(GetVersionEx(&OSVersionInfo))  
                  return   (OSVersionInfo.dwPlatformId   ==   VER_PLATFORM_WIN32_NT);  
          else  
                  return   (FALSE);  
  }  
  //--------------------------------------------------------------------------  
  void   __fastcall   TfMain::FormCreate(TObject   *Sender)  
  {  
            if(IsWinNT())  
            {  
                  if(!IsAdmin())  
                    {  
   
                        Res   =   ::CreateProcessWithLogonW(L"User",L"DOAMIN",L"Password",LOGON_WITH_PROFILE,  
                                                            L"Setup.exe",L"   /S   /v/qn",CREATE_DEFAULT_ERROR_MODE,NULL,NULL,&si,&pi);  
                        if(Res)     {   Timer1->Enabled   =   true;}  
                        else           {   Panel1->Visible   =   false;}  
   
                    }  
                    else  
                        {  
                             
                              CreateProcess("setup.exe",   "   /S   /v/qn",   NULL,   NULL,   FALSE,   0,     NULL,   NULL,&si1,&pi);  
                               
                        }  
   
   
   
              }  
              else  
              {  
                 
                CreateProcess("setup.exe",   "   /S   /v/qn",   NULL,   NULL,   FALSE,   0,     NULL,   NULL,&si1,&pi);  
                   
                            }  
   
   
  }Top

3 楼jishiping(JSP 季世平)回复于 2005-06-02 21:02:26 得分 0

你静态调用了只有NT系统上才有的API函数,当然不行了。必须动态加载CreateProcessWithLogonW这个函数。你要用MSDN查一下,看看在哪个DLL中,然后用LoadLibrary   以及   GetProcAddress   动态加载这个函数才可以。Top

4 楼Cryes(yes)回复于 2005-06-02 21:03:52 得分 0

可以写出来呢?我是新手啊,不胜感谢!Top

5 楼jishiping(JSP 季世平)回复于 2005-06-02 21:22:56 得分 0

typedef   BOOL   WINAPI   (_CreateProcessWithLogonW)(     \  
              LPCWSTR   lpUsername,                                                 \  
              LPCWSTR   lpDomain,                                                     \  
              LPCWSTR   lpPassword,                                                 \  
              DWORD       dwLogonFlags,                                             \  
              LPCWSTR   lpApplicationName,                                   \  
              LPWSTR   lpCommandLine,                                             \  
              DWORD   dwCreationFlags,                                           \  
              LPVOID   lpEnvironment,                                             \  
              LPCWSTR   lpCurrentDirectory,                                 \  
              LPSTARTUPINFOW   lpStartupInfo,                             \  
              LPPROCESS_INFORMATION   lpProcessInformation   \  
              );  
   
  HINSTANCE   hInstance   =   LoadLibrary("advapi32.dll");  
  (FARPROC)_CreateProcessWithLogonW   =   hInstance!=NULL   ?    
          GetProcAddress(hInstance,"CreateProcessWithLogonW")   :   NULL;  
  if   (_CreateProcessWithLogonW)  
  {  
           
          _CreateProcessWithLogonW(...);  
          //....  
  }  
  else  
  {  
          CreateProcess(.....);  
          //.....  
  }  
  if   (hInstance!=NULL)   FreeLibrary(hInstance);Top

6 楼Cryes(yes)回复于 2005-06-03 08:19:48 得分 0

季老大,好像有3处报错的,请帮忙解决一下好吗?  
   
  //---------------------------------------------------------------------------  
   
  #include   <vcl.h>  
  #pragma   hdrstop  
   
  #include   "Unit1.h"  
  #pragma   comment   (lib,"Advapi32.lib")  
  //---------------------------------------------------------------------------  
  #pragma   package(smart_init)  
  #pragma   resource   "*.dfm"  
  TForm1   *Form1;  
   
    typedef   BOOL   WINAPI   (_CreateProcessWithLogonW)(     \  
              LPCWSTR   lpUsername,                                                 \  
              LPCWSTR   lpDomain,                                                     \  
              LPCWSTR   lpPassword,                                                 \  
              DWORD       dwLogonFlags,                                             \  
              LPCWSTR   lpApplicationName,                                   \  
              LPWSTR   lpCommandLine,                                             \  
              DWORD   dwCreationFlags,                                           \  
              LPVOID   lpEnvironment,                                             \  
              LPCWSTR   lpCurrentDirectory,                                 \  
              LPSTARTUPINFOW   lpStartupInfo,                             \  
              LPPROCESS_INFORMATION   lpProcessInformation   \  
              );  
   
  STARTUPINFOW   si={sizeof(si)};  
  STARTUPINFO   si1={sizeof(si1)};  
  PROCESS_INFORMATION   pi;  
  //---------------------------------------------------------------------------  
  __fastcall   TForm1::TForm1(TComponent*   Owner)  
                  :   TForm(Owner)  
  {  
  }  
  //---------------------------------------------------------------------------  
   
  void   __fastcall   TForm1::Button1Click(TObject   *Sender)  
  {  
          HINSTANCE   hInstance   =   LoadLibrary("advapi32.dll");  
        (FARPROC)_CreateProcessWithLogonW   =   hInstance!=NULL   ?  
          GetProcAddress(hInstance,"CreateProcessWithLogonW")   :   NULL;  
        if   (_CreateProcessWithLogonW)  
          {  
   
          _CreateProcessWithLogonW(L"user",L"Domain",L"password",LOGON_WITH_PROFILE,  
                                                            L"Setup.exe",L"   /S   /v/qn",CREATE_DEFAULT_ERROR_MODE,NULL,NULL,&si,&pi);  
   
  }  
  else  
  {  
          CreateProcess("setup.exe",   "   /S   /v/qn",   NULL,   NULL,   FALSE,   0,     NULL,   NULL,&si1,&pi);  
          //.....  
  }  
  if   (hInstance!=NULL)   FreeLibrary(hInstance);  
   
  }  
  //---------------------------------------------------------------------------  
   
   
   
  Build  
      [C++   Error]   Unit1.cpp(41):   E2108   Improper   use   of   typedef   '__stdcall   _CreateProcessWithLogonW(const   wchar_t   *,const   wchar_t   *,const   wchar_t   *,unsigned   long,const   wchar_t   *,wchar_t   *,unsigned   long,void   *,const   wchar_t   *,_STARTUPINFOW   *,_PROCESS_INFORMATION   *)'  
      [C++   Error]   Unit1.cpp(43):   E2108   Improper   use   of   typedef   '__stdcall   _CreateProcessWithLogonW(const   wchar_t   *,const   wchar_t   *,const   wchar_t   *,unsigned   long,const   wchar_t   *,wchar_t   *,unsigned   long,void   *,const   wchar_t   *,_STARTUPINFOW   *,_PROCESS_INFORMATION   *)'  
      [C++   Error]   Unit1.cpp(47):   E2031   Cannot   cast   from   '_PROCESS_INFORMATION   *'   to   'int(const   wchar_t   *,const   wchar_t   *,const   wchar_t   *,unsigned   long,const   wchar_t   *,wchar_t   *,unsigned   long,void   *,const   wchar_t   *,_STARTUPINFOW   *,_PROCESS_INFORMATION   *)'  
  Top

7 楼Cryes(yes)回复于 2005-06-03 18:06:48 得分 0

我自己写了个函数,能编译过去,但还是用不了啊.请帮我看看好吗?  
   
   
  bool   LogonWithNT(String   strUser,String   strDomain,String   strPWD)  
  {  
              typedef   BOOL   (WINAPI   *pCreateProcessWithLogonW)(  
              LPCWSTR   lpUsername,  
              LPCWSTR   lpDomain,  
              LPCWSTR   lpPassword,  
              DWORD       dwLogonFlags,  
              LPCWSTR   lpApplicationName,  
              LPWSTR   lpCommandLine,  
              DWORD   dwCreationFlags,  
              LPVOID   lpEnvironment,  
              LPCWSTR   lpCurrentDirectory,  
              LPSTARTUPINFOW   lpStartupInfo,  
              LPPROCESS_INFORMATION   lpProcessInformation  
              );  
        bool   Res   =   false;  
        HINSTANCE   hInstance   =   LoadLibrary("advapi32.dll");  
        if(hInstance!=NULL)  
          {  
              pCreateProcessWithLogonW   pCreate   =   NULL;  
   
            (FARPROC   )   pCreate   =   GetProcAddress(hInstance,"CreateProcessWithLogonW");  
            if   (pCreate)  
              {  
   
                  Res   =   pCreate(WideString(strUser).c_bstr(),WideString(strDomain).c_bstr(),WideString(strPWD).c_bstr(),LOGON_WITH_PROFILE,  
                                                            L"Setup.exe",NULL,CREATE_DEFAULT_ERROR_MODE,NULL,NULL,&si,&pi);  
   
              }  
   
          FreeLibrary(hInstance);  
          }  
          else   {ShowMessage("Load   advapi32.dll   fail!");   Application->Terminate();}  
        return   Res;  
   
  }Top

8 楼jishiping(JSP 季世平)回复于 2005-06-03 21:44:20 得分 50

#pragma   comment   (lib,"Advapi32.lib")   这一行要去掉,否则在Win95下可能还是不能运行。  
   
  typedef   BOOL   WINAPI   (*_CreateProcessWithLogonW)(   \  
          LPCWSTR   lpUsername,   \  
          LPCWSTR   lpDomain,   \  
          LPCWSTR   lpPassword,   \  
          DWORD   dwLogonFlags,   \  
          LPCWSTR   lpApplicationName,   \  
          LPWSTR   lpCommandLine,   \  
          DWORD   dwCreationFlags,   \  
          LPVOID   lpEnvironment,   \  
          LPCWSTR   lpCurrentDirectory,   \  
          LPSTARTUPINFOW   lpStartupInfo,   \  
          LPPROCESS_INFORMATION   lpProcessInformation   \  
  );  
   
  void   __fastcall   TForm1::Button1Click(TObject   *Sender)  
  {  
          STARTUPINFO   si;  
          STARTUPINFOW   siw;  
          PROCESS_INFORMATION   pi;  
   
          HINSTANCE   hInstance   =   LoadLibrary("advapi32.dll");  
          _CreateProcessWithLogonW   CreateProcessWithLogonW   =  
          hInstance!=NULL   ?   (_CreateProcessWithLogonW)  
          GetProcAddress(hInstance,"CreateProcessWithLogonW")  
          :   NULL;  
          if   (CreateProcessWithLogonW)  
          {  
                  memset(siw,   0,   sizeof(siw));  
                  siw.cb   =   sizeof(STARTUPINFOW);  
                  CreateProcessWithLogonW(L"user",   L"Domain",   L"password",  
                          LOGON_WITH_PROFILE,   L"Setup.exe",   L"   /S   /v/qn",  
                          CREATE_DEFAULT_ERROR_MODE,   NULL,   NULL,   &siw,   &pi);  
                  //......  
          }  
          else  
          {  
                  memset(si,   0,   sizeof(si));  
                  si.cb   =   sizeof(STARTUPINFO);  
                  CreateProcess("setup.exe",   "   /S   /v/qn",   NULL,   NULL,  
                          FALSE,   0,   NULL,   NULL,   &si,   &pi);  
                  //.....  
          }  
          if   (hInstance!=NULL)   FreeLibrary(hInstance);  
  }Top

9 楼Cryes(yes)回复于 2005-06-05 14:28:36 得分 0

谢谢季老大了,问题已经解决了.不过现在还有一个问题:如何判断某个域用户是本地管理员组成员?    
  http://community.csdn.net/Expert/TopicView1.asp?id=4058874  
  请帮帮忙吧!!谢谢!!  
  Top

10 楼jishiping(JSP 季世平)回复于 2005-06-07 18:10:54 得分 0

这个没有做过,不知道。Top

相关问题

  • 应用软件在不同操作系统下的使用问题
  • vb在不同操作系统下的转换使用,高手过来看看.
  • 应该如何使用操作系统?
  • 2000操作系统的使用
  • JSP不同数据库 不同操作系统乱码问题
  • 在线等待:如何得知用户使用的操作系统是何种操作系统?
  • win2000操作系统使用问题~!!!请大虾们帮忙
  • 中国的导弹上使用的是什么操作系统?
  • 不同操作系统出来到界面不一样??
  • 32位的操作系统与64位的有什么不同?

关键词

  • c++
  • win95
  • 函数
  • createprocesswithlogonw
  • lpcwstr
  • siw
  • hinstance
  • si
  • osversioninfo
  • dwlogonflags

得分解答快速导航

  • 帖主:Cryes
  • jishiping

相关链接

  • CSDN Blog
  • 技术文档
  • 代码下载
  • 第二书店
  • 读书频道

广告也精彩

反馈

请通过下述方式给我们反馈
反馈
提问
网站简介|广告服务|VIP资费标准|银行汇款帐号|网站地图|帮助|联系方式|诚聘英才|English|问题报告
北京创新乐知广告有限公司 版权所有, 京 ICP 证 070598 号
世纪乐知(北京)网络技术有限公司 提供技术支持
Copyright © 2000-2008, CSDN.NET, All Rights Reserved
GongshangLogo