简体   繁体   English

批量翻译成 C++ 的等效代码 %SystemDrive%

[英]The equivelant code %SystemDrive% in batch translated into C++

To anyone that can help Please, (My operating system is Windows XP) I have looked on the this forum but have not found a similair answer that I could use or adapt to suite this particular situation.对于任何可以提供帮助的人,(我的操作系统是 Windows XP)我已经看过这个论坛,但没有找到类似的答案,我可以使用或适应这种特殊情况。 I will try to explain (I apologise in advance if my question seems confusing) I am constructing a batch file that will call a C++ program (.exe) The C++ program is hard coded to the C: drive.我将尝试解释(如果我的问题看起来令人困惑,我提前道歉)我正在构建一个批处理文件,该文件将调用 C++ 程序 (.exe) C++ 程序被硬编码到 C: 驱动器。 By the way I did not write the C++ program as I am incapable of writing in C++ but would like to exchange the C: in C++ for what would be in batch %SystemDrive%.顺便说一下,我没有编写 C++ 程序,因为我无法用 C++ 编写,但想将 C++ 中的 C: 换成批处理 %SystemDrive% 中的内容。 The line of code in C++ reads as follows: C++中的代码行如下:

        SetSfcFileException(0, L"c:\\windows\\system32\\calc.exe",-1);
    // Now we can modify the system file in a complete stealth.
}

The bit of code I would like to alter in the above code is C: or "C" to change it to %systemDrive% but in C++ code language, in effect change the hard coded part of the C++ program to read a System path variable within XP.我想在上面的代码中更改的代码位是 C: 或“C”以将其更改为 %systemDrive% 但在 C++ 代码语言中,实际上更改了 C++ 程序的硬编码部分以读取系统路径变量在 XP 中。

I have also looked elsewhere on the net but have not found a suitable answer as I do Not want to break the C++ code you see.我还查看了网上的其他地方,但没有找到合适的答案,因为我不想破坏您看到的 C++ 代码。

The C++ code was obtained from the folowing website written by Abdellatif_El_Khlifi: C++ 代码来自 Abdellatif_El_Khlifi 编写的以下网站:

https://www.codeproject.com/Articles/14933/A-simple-way-to-hack-Windows-File-Protection-WFP-u https://www.codeproject.com/Articles/14933/A-simple-way-to-hack-Windows-File-Protection-WFP-u

Many Thanks for any help given,非常感谢您提供的任何帮助,

David大卫

The search term you should be looking for is Known Folders .您应该寻找的搜索词是已知文件夹

Specifically, calling SHGetKnownFolderPath() with the FOLDERID_System identifier, one of the many IDs found here .具体来说,使用FOLDERID_System标识符调用SHGetKnownFolderPath() ,这是在此处找到的众多 ID 之一。

That's for Vista or better.那是为 Vista 或更好的。 For earlier than that (such as XP), you have to use CSIDL values, CSIDL_SYSTEM (see here for list) passed into SHGetFolderPath() .对于更早的版本(例如 XP),您必须使用传递给SHGetFolderPath() CSIDL 值, CSIDL_SYSTEM (参见此处的列表SHGetFolderPath()

You can still use the pre-Vista ones but I think they're just thin wrappers around the newer ones.您仍然可以使用Vista 之前的版本,但我认为它们只是新版本的薄包装。


This is the simplest console application I could come up with that shows this in action (Visual Studio 2019):这是我能想出的最简单的控制台应用程序,它显示了这一点(Visual Studio 2019):

#include <iostream>
#include <shlobj_core.h>
#include <comutil.h>

int main()
{
    PWSTR path = NULL;
    HRESULT hr = SHGetKnownFolderPath(FOLDERID_System, 0, NULL, &path);
    _bstr_t bstrPath(path);
    std::string strPath((char*)bstrPath);
    std::cout << "Path is '" << strPath << "'\n";
}

and the output on my system is:我系统上的输出是:

Path is 'C:\WINDOWS\system32'

This is not really answering my own question, well it is but in a alternative manner, many ways to skin a cat so to speak!这并没有真正回答我自己的问题,好吧,它只是以另一种方式,可以说有很多方法可以给猫剥皮!

Here is one encouraging bit of news though I have stumbled across the very thing I need called WFPReplacer, it is a commandline windows utility that pretty well does what I want & generally in the same manner.这是一个令人鼓舞的消息,尽管我偶然发现了我需要的名为 WFPReplacer 的东西,它是一个命令行窗口实用程序,可以很好地完成我想要的并且通常以相同的方式完成。 it disables WFP for both singular files & can be used for wholesale switching off of WFP if the right file is replaced.它对单一文件禁用 WFP,并且如果替换了正确的文件,则可用于批量关闭 WFP。 All I need to do is write a batch file as a front end to back up the system files I want to disable use WFPReplacer.exe.我需要做的就是编写一个批处理文件作为前端来备份我想禁用使用 WFPReplacer.exe 的系统文件。 So if in the event of the proceedings the routine gets stuffed I can revert back to the backed up files.因此,如果在诉讼过程中例程被塞满,我可以恢复到备份文件。 I think this program uses the same type of embedded coding but is written in Delphi/pascal, it is called Remko Weijnen's Blog (Remko's Blog) "replacing Wfp protected files".我认为这个程序使用了相同类型的嵌入式编码但是是用 Delphi/pascal 编写的,它被称为 Remko Weijnen 的博客(Remko's Blog)“替换 Wfp 保护文件”。

I generally like to leave whatever I am doing on a positive note.我通常喜欢以积极的态度离开我正在做的任何事情。 So just in case someone else lands on this forum & is trying to accomplish a similair exercise here is the code that one can compile (This is not my code it belongs to Remko Weijnen's Blog (Remko's Blog)) Please be advised it is NOT C++ it is a commandline exe Delhi/Pascal found at this link, so all credits belong to him.所以以防万一其他人登陆这个论坛并试图完成一个类似的练习,这里是一个可以编译的代码(这不是我的代码,它属于 Remko Weijnen 的博客(Remko 的博客))请注意它不是C++这是在此链接中找到的命令行 exe 德里/帕斯卡,因此所有学分都属于他。 The link is:链接是:

https://www.remkoweijnen.nl/blog/2012/12/05/replacing-wfp-protected-files/ https://www.remkoweijnen.nl/blog/2012/12/05/replacing-wfp-protected-files/

DWORD __stdcall SfcFileException(RPC_BINDING_HANDLE hServer, LPCWSTR lpSrc, int Unknown)
{
  RPC_BINDING_HANDLE hServerVar; // eax@2
  int nts; // eax@6
  __int32 dwResult; // eax@7
  DWORD dwResultVar; // esi@9
  int v8; // [sp+8h] [bp-8h]@1
  int v9; // [sp+Ch] [bp-4h]@1

  LOWORD(v8) = 0;
  *(int *)((char *)&v8 + 2) = 0;
  HIWORD(v9) = 0;
  if ( !hServer )
  {
    hServerVar = _pRpcHandle;
    if ( !_pRpcHandle )
    {
      hServerVar = SfcConnectToServer(0);
      _pRpcHandle = hServerVar;
      if ( !hServerVar )
        return 0x6BA;                           // RPC_S_SERVER_UNAVAILABLE
    }
    hServer = hServerVar;
  }
  nts = SfcRedirectPath(lpSrc, (int)&v8);
  if ( nts >= 0 )
    dwResult = SfcCli_FileException((int)hServer, v9, Unknown).Simple;
  else
    dwResult = RtlNtStatusToDosError(nts);
  dwResultVar = dwResult;
  MemFree(v9);
  return dwResultVar;
}

Also as one further warning (Unless you know what you are doing!!!) do not attempt to use this program, ALWAYS ALWAYS ALWAYS backup your system files before deletion or alteration.另外作为进一步的警告(除非您知道自己在做什么!!!)不要尝试使用该程序,在删除或更改之前始终备份您的系统文件。 What this program will do is disarm WFP for 60 seconds whilst you intercange or amend your files.该程序将做的是在您交换或修改文件的同时解除 WFP 的武装 60 秒。 Example usage for example is: WfpReplacer.exe c:\\windows\\Notepad.exe (Errorlevel true or false will be produced on execution).示例用法例如:WfpReplacer.exe c:\\windows\\Notepad.exe(执行时将产生错误级别 true 或 false)。

Best Regards David最好的问候大卫

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM