開發環境:WIN7 X64 SP1 + VS2010 專業版 MFC表單程式
BOOL Proc::ConfigNetDriver( void )
{
/*
PrintMsg(_T("API 卸載映射盤。"));
res = WNetCancelConnection2(L"x:", CONNECT_UPDATE_PROFILE, TRUE);
DWORD res;
NETRESOURCE nr = {0};
nr.dwType = RESOURCETYPE_ANY;
nr.lpLocalName = L"x:";
nr.lpRemoteName = L"\\\\172.16.1.1\\share:";
PrintMsg(_T("API 映射驅動盤,稍等。。。%s"), p->NetDrv_SharePath);
res = WNetAddConnection2(&nr, L"guest", L"guest", CONNECT_UPDATE_PROFILE);
Sleep(1000);
if(res != NO_ERROR)
{
PrintMsg(L"ConfigNetDriverAPI Error: %ld", res);
return FALSE;
}
*/
system(“net use * /del /y & ping localhost -n 2 > nul”);
system(“net use x: \"\\\\172.16.1.1\\share\" guest /user:guest /persistent:yes & ping localhost -n 2 > nul”);
DWORD dwResult = 0;
SendMessageTimeout(HWND_BROADCAST, WM_DEVICECHANGE, NULL, NULL, SMTO_NORMAL, 1000, &dwResult);
SHChangeNotify(SHCNE_ALLEVENTS, SHCNF_IDLIST, NULL, NULL);
if( (int)GetFileAttributes(L"x:\1.txt") == -1 )
{
PrintMsg(L"重啟Explorer中(%s 訪問失敗)", L"x:\1.txt");
RestartWinExplorer();
}
}
BOOL Proc::RestartWinExplorer()
{
char strShell[1024];
SHELLEXECUTEINFOA shellExeInfo = {0};
shellExeInfo.cbSize = sizeof(SHELLEXECUTEINFOA);
shellExeInfo.fMask = SEE_MASK_NOCLOSEPROCESS;
shellExeInfo.nShow = SW_HIDE;
shellExeInfo.lpVerb = "open";
GetSystemDirectoryA(strShell,1024);
::PathAppendA(strShell,"taskkill.exe");
shellExeInfo.lpFile = strShell;
shellExeInfo.lpParameters = "/F /IM explorer.exe";
ShellExecuteExA(&shellExeInfo);
WaitForSingleObject(shellExeInfo.hProcess,INFINITE);
GetWindowsDirectoryA(strShell,1024);
::PathAppendA(strShell,"explorer.exe");
WinExec(strShell,SW_SHOW);
return TRUE;
}
生成程式后 以管理員權限在 WIN8、WIN10下運行。
通過system函式 或者WNetAddConnection2的API方式映射盤符成功后程式是可以打開 x:\1.txt 的檔案,但是在我的電腦下卻看不到映射的x盤,甚至強行運行RestartWinExplorer后也不行。
我的問題:
1、為什么程式映射后 程式自己可以訪問映射盤,而用戶桌面卻不行?有什么辦法可以解決
2、當前我知道用system賬號運行程式后,用戶桌面可以看到x盤,但是有沒有辦法用代碼實作自身以system賬號運行?
uj5u.com熱心網友回復:
C:\>runas /?RUNAS 用法:
RUNAS [ [/noprofile | /profile] [/env] [/savecred | /netonly] ]
/user:<UserName> program
RUNAS [ [/noprofile | /profile] [/env] [/savecred] ]
/smartcard [/user:<UserName>] program
/noprofile 指定不應該加載用戶的組態檔。
這會加速應用程式加載,但
可能會造成一些應用程式運行不正常。
/profile 指定應該加載用戶的組態檔。
這是默認值。
/env 要使用當前環境,而不是用戶的環境。
/netonly 只在指定的憑據限于遠程訪問的情況下才使用。
/savecred 用用戶以前保存的憑據。
Windows XP Home Edition 上沒有這個選項。
該選項會被忽略。
/smartcard 如果憑據是智能卡提供的,則使用這個選項。
/user <UserName> 應使用 USER@DOMAIN 或 DOMAIN\USER 形式
program EXE 的命令列。請參閱下面的例子
例如:
> runas /noprofile /user:mymachine\administrator cmd
> runas /profile /env /user:mydomain\admin "mmc %windir%\system32\dsa.msc"
> runas /env /user:[email protected] "notepad \"my file.txt\""
注意: 只在得到提示時才輸入用戶的密碼。
注意: USER@DOMAIN 跟 /netonly 不兼容。
注意: /profile 跟 /netonly 不兼容。
注意: /savecred 跟 /smartcard 不兼容。
uj5u.com熱心網友回復:
你用administrator權限運行你的程式uj5u.com熱心網友回復:
前提是用 右鍵管理員運行程式的,還是不行。uj5u.com熱心網友回復:
你可以把你的程式改成服務, 啟動這個服務就是用system賬號運行uj5u.com熱心網友回復:
恩,但是如何在代碼級實作 提升到system賬號,或者自身用system運行(不考慮注冊成系統服務的形式)uj5u.com熱心網友回復:
搜“Session0穿透”轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/103868.html
標籤:基礎類
