可视化 C++ 中的模拟
Impersonation in visual c++
我需要在我的 c++ 应用程序中模拟不同的用户。我正在为此使用以下代码。
try {
IntPtr tokenHandle = IntPtr(0);
bool returnValue = LogonUser(username, domain, password, LOGON32_LOGON_INTERACTIVE, LOGON32_PROVIDER_DEFAULT, &tokenHandle);
if (false == returnValue) {
int ret = Marshal::GetLastWin32Error();
throw gcnew System::ComponentModel::Win32Exception(ret);
}
WindowsIdentity^ newId = gcnew WindowsIdentity(tokenHandle);
WindowsImpersonationContext^ impersonatedUser = newId->Impersonate();
//TODO access file with impersonated user rights
impersonatedUser->Undo(); // Stop impersonating the user.
if (tokenHandle != IntPtr::Zero) CloseHandle(tokenHandle); // Free the tokens.
}
catch(Exception^ ex){
}
登录用户函数为 c++ 控制台应用程序返回 true,但为 visual c++ 应用程序返回 false。 这两个项目都使用公共语言运行库支持。这两个项目具有相同的包含和引用。
问题是Visual c ++项目是win32项目。它已经包含登录功能。所以我不需要 .net 模拟函数。以下代码修复了我的 isue。
HANDLE tokenHandle = INVALID_HANDLE_VALUE;
bool returnValue = LogonUser(L"username", L"domain", L"password", LOGON32_LOGON_INTERACTIVE, LOGON32_PROVIDER_DEFAULT, &tokenHandle);
if (false == returnValue) {
int ret = GetLastError();
throw gcnew System::ComponentModel::Win32Exception(ret);
}
bool res = ImpersonateLoggedOnUser(tokenHandle);
//Access file here
CloseHandle(tokenHandle);
相关文章:
- 如何使用Google Mock来模拟gettimeofday()
- G锁定铸造到基础上会释放模拟行为
- 有什么好的方法可以让系统调用代理允许在单元测试中进行模拟
- 落砂模拟碰撞检测C++和SFML
- 在gtest.中使用fff.h模拟系统API
- 谷歌模拟和覆盖关键字
- 用C#中的并集模拟C++嵌套结构
- 在同一模拟中使用静脉和静脉_ inet内容时出现运行时错误
- 在模拟器中使用并集来模拟CPU寄存器有多合适
- 我写了一个C++程序来模拟Enigma机器.我没有得到输出
- 如何模拟不同边数的骰子滚动?
- 模拟持久按键
- 使用SIR模型的疾病爆发模拟
- 在 c++ 中模拟输入并在 JAVA 中读取它?
- 转发变量参数列表以模拟 std::thread
- 如何在谷歌模拟中匹配 C 样式数组
- 如何使用兰德随机化模拟点击
- 模拟GPS数据,以便使用Qt与Traccar一起使用
- QKeyPress - 在Qt中模拟按键
- 如何使用不同的谷歌模拟运行相同的谷歌测试用例?