2

I created a dll(in c++) with one method called "changeSize"(in kb) which has the argument "size". So the method basically sets a new size of a file (the file that is stated in the method). Now I want to execute the method of this dll in command prompt.

I've tried to execute the method with the help of "rundll32.exe", so i executed that command in command prompt, but it does not work:

rundll32.exe ChangeSize.dll,changeSize 1024

Question 1: Is it possible to execute a method of a dll with "rundll32.exe" if it needs arguments, or is there a simple way to do it with command prompt or if necessary powershell?

EDIT Now I know that the dll needs to have a special signature, that it is able to be executed with "rundll32.exe".

I have come accross this: https://stackoverflow.com/a/11913860/16104144, which is a short documentation on how to create a dll that can be run with "rundll32.exe".

I have the following code, and when i set the parameter "size" OF MY CODE to any value and build the dll and run it in cmd: rundll32.exe ChangeSize.dll,EntryPoint, then it works, but I want to be able to define the size just when i execute the dll with "rundll32.exe" and i don't want to have to define a size before building the dll.

Question 2: Where do i have to add the variable size in my code that "rundll32.exe" knows that the dll needs that argument. The parameter "size" should be "unsigned long int".

So this is my code in the main file(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;
    }

}

Adding the variable to the function and its declaration did not work:

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

As it says here https://learn.microsoft.com/en-us/windows/win32/learnwin32/winmain--the-application-entry-point, the arguments that are passed with this command: rundll32.exe ChangeSize,changeSize 1024

are saved inside the variable

LPSTR lpszCmdLine

And because it is just one argument I can just replace the variable "size" with (unsigned long int)lpszCmdLine:

#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.
  • 1
    https://learn.microsoft.com/en-us/windows-server/administration/windows-commands/rundll32. Not every *.dll* can be invoked this way. – CristiFati Jun 02 '21 at 19:15
  • [Create a DLL that can be run](https://stackoverflow.com/a/11917097/11954025) – Daniel Jun 02 '21 at 19:37
  • Thanks for the answers, that explains a lot. – Donniekovski Jun 02 '21 at 20:41
  • I've rolled back your last two edits. Please do not change a title to include "SOLVED", a solved question is one which has an officially marked as accepted answer. Please do not include a solution within the question body. If you have found a solution, please write an answer in the appropriate location. Should you write up your own answer, and it proves to be your favored one, you can after sufficient time has elapsed mark your own answer as accepted. – Compo Jun 02 '21 at 23:19

1 Answers1

-1

Question 1: First of all, not every .dll can be executed with rundll32.exe, because the .dlls need a special signature, so a special structure. For example is an EntryPoint necessary.

Here it says how the structure looks like: https://stackoverflow.com/a/11913860/16104144


Question 2: The arguments are stored in LPSTR lpszCmdLine and what the other variables save that are in the declaration of the method:

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

can be seen here:

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

Just use the variable lpszCmdLine as the variable that you need. But as soon as you have more than one argument so e.g. rundll32.exe ChangeSize,changeSize 1 9 you need to split it.