如何在命令提示符下执行带参数的 dll (C++) 方法

How to execute a method of a dll (C++) with parameters with command prompt

我创建了一个 dll(在 c++ 中),其中一个方法名为“changeSize”(以 kb 为单位),该方法具有参数“size”。因此,该方法基本上设置了文件的新大小(方法中指定的文件)。 现在我想在命令提示符下执行这个 dll 的方法。

我尝试在“rundll32.exe”的帮助下执行该方法,所以我在命令提示符下执行了该命令,但它不起作用:

rundll32.exe ChangeSize.dll,changeSize 1024

问题 1: 如果 dll 需要参数,是否可以执行带有“rundll32.exe”的方法,或者是否有一种简单的方法可以通过命令提示符或必要时的 powershell 来完成?

编辑 现在我知道 dll 需要有一个特殊的签名,它可以用“rundll32.exe”执行。

我遇到了这个:,这是一个关于如何创建 dll 的简短文档,它可以 运行 与“rundll32.exe".

我有以下代码,当我将我的代码的参数“大小”设置为任何值并在 cmd 中构建 dll 和 运行 时: rundll32.exe ChangeSize.dll,EntryPoint,然后它可以工作,但我希望能够在我使用“rundll32.exe”执行 dll 时定义大小,我不想在构建 dll.

问题 2: 我必须在我的代码中的什么位置添加变量大小,“rundll32.exe”知道 dll 需要那个论点。参数“size”应该是“unsigned long int”。

所以这是我在主文件中的代码(dllmain.cpp):

#include "pch.h"

namespace fs = std::filesystem;


__declspec(dllexport) void CALLBACK EntryPoint(
    HWND hwnd,
    HINSTANCE hinst,
    LPSTR lpszCmdLine,
    int nCmdShow
    );

void CALLBACK EntryPoint(HWND hwnd, HINSTANCE hinst, LPSTR lpszCmdLine, int nCmdShow)
{
    fs::path p = "C:\Users\myname\AppData\Local\Temp\1304.txt";
    fs::resize_file(p, size);

    int msgboxID = MessageBox(
        NULL,
        L"Hello World from Run32dll",
        L"HelloWorld",
        MB_ICONWARNING | MB_CANCELTRYCONTINUE | MB_DEFBUTTON2 
    );

    switch (msgboxID)
    {
    case IDCANCEL:
        break;
    case IDTRYAGAIN:
        break;
    case IDCONTINUE: 
        break;
    }

}

将变量添加到函数及其声明无效:

void CALLBACK EntryPoint(HWND hwnd, HINSTANCE hinst, LPSTR lpszCmdLine, int nCmdShow, unsigned long int size`, unsigned long int size);

如此处 https://docs.microsoft.com/en-us/windows/win32/learnwin32/winmain--the-application-entry-point 所述,使用此命令传递的参数: rundll32.exe ChangeSize,changeSize 1024

保存在变量里面

LPSTR lpszCmdLine

因为它只是一个参数,所以我可以用 (unsigned long int)lpszCmdLine:

替换变量“size”
#include "pch.h"

namespace fs = std::filesystem;


__declspec(dllexport) void CALLBACK EntryPoint(
    HWND hwnd,
    HINSTANCE hinst,
    LPSTR lpszCmdLine,
    int nCmdShow
    );

void CALLBACK EntryPoint(HWND hwnd, HINSTANCE hinst, LPSTR lpszCmdLine, int nCmdShow)
{
    fs::path p = "C:\Users\myname\AppData\Local\Temp\1304.txt";
    fs::resize_file(p, (unsigned long int)argument;

    /*int msgboxID = MessageBox(
        NULL,
        L"Hello World from Run32dll",
        L"HelloWorld",
        MB_ICONWARNING | MB_CANCELTRYCONTINUE | MB_DEFBUTTON2 
    );

    switch (msgboxID)
    {
    case IDCANCEL:
        break;
    case IDTRYAGAIN:
        break;
    case IDCONTINUE: 
        break;
    }/*
     
}


___________________________________________________________________________
I have to admit that it's pretty new to me to create *dlls*, but i can not find anything about creating *dlls* with parameters that can be run with "rundll32.exe" and I am very thankful for you even reading this.

问题一:首先,并不是每个.dll都可以用rundll32.exe执行,因为.dlls需要特殊的签名,所以结构特殊。例如,是否需要一个 EntryPoint。

这里说的是结构的样子:


问题2:参数存储在LPSTR lpszCmdLine中,其他变量保存在方法声明中的内容:

void CALLBACK EntryPoint(HWND hwnd, HINSTANCE hinst, LPSTR lpszCmdLine, int nCmdShow)

可以在这里看到:

https://docs.microsoft.com/en-us/windows/win32/learnwin32/winmain--the-application-entry-point

只需使用变量lpszCmdLine作为您需要的变量即可。但是,一旦您有多个论点,例如rundll32.exe ChangeSize,changeSize 1 9你需要拆分它。