关于两个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




