简体   繁体   English

如何将我的程序转换为 .dll 文件并使用 rundll32.exe 在 cmd 中运行它?

[英]How can I turn my program into a .dll file and run it in cmd using rundll32.exe?

I have a program that creates multiple threads and prints some strings in a loop.我有一个程序可以创建多个线程并在循环中打印一些字符串。 My task is to turn this program into a.dll and run it using rundll32.exe, but I've got no idea how can I run a.dll as an executable file.我的任务是将这个程序变成 a.dll 并使用 rundll32.exe 运行它,但我不知道如何将 a.dll 作为可执行文件运行。

 #define _CRT_SECURE_NO_WARNINGS
#include<windows.h>
#include <stdlib.h>
#include<process.h>
#include<stdio.h>
#include<string>
#include<ctime>
#include<vector>
#include<iostream>

typedef struct {
    std::string info;
    unsigned int m_number;
    int m_stop_thread;
    int m_priority_thread;
    unsigned m_cycles;
    unsigned m_currentThread;
}data;
HANDLE tmp;
unsigned int __stdcall Func(void* d) {
    data* real = (data*)d;
    std::cout << "\nCurrent thread ID: " << GetCurrentThreadId() << std::endl;
    if (real->m_currentThread == real->m_priority_thread)
        SetThreadPriority(tmp, 2);
        std::cout << "Thread priority: " << GetThreadPriority(tmp) << std::endl;
    for (int j = (real->m_currentThread - 1) * real->m_cycles / real->m_number;j < real->m_currentThread * real->m_cycles / real->m_number;j++) {
        for (int i = 0;i < real->info.size();++i)
            std::cout << real->info[i];
        std::cout << std::endl;
    }
    return 0;
}

int main(int argc, char* argv[]) {
    int threadsNumber, priority, stop;
    std::string str;
    std::cout << "Enter the info about a student:\n";
    std::getline(std::cin, str);
    std::cout << "Enter the number of threads:\n";
    std::cin >> threadsNumber;
    int cycles;
    std::cout << "Enter the number of cycles:\n";
    std::cin >> cycles;
    std::cout << "Which thread priority do you want to change? ";
    std::cin >> priority;
    std::cout << "Which thread do you want to stop? ";
    std::cin >> stop;
    std::vector<HANDLE> threads;

    data* args = new data;
    args->info = str;
    args->m_number = threadsNumber;
    args->m_cycles = cycles;
    args->m_priority_thread = priority;
    args->m_stop_thread = stop;
    clock_t time = clock();
    for (int i = 1;i <= threadsNumber;++i) {
        args->m_currentThread = i;
        tmp = (HANDLE)_beginthreadex(0, 0, &Func, args, 0, 0);

        threads.push_back(tmp);
    }
    WaitForMultipleObjects(threads.size(), &threads.front(), TRUE, INFINITE);
    time = clock() - time;
    std::cout << "time: " << (double)time / CLOCKS_PER_SEC << "s" << std::endl << std::endl;
    getchar();
    return 0;
}

Does anyone know how can I put this code into a dll and run it using command line?有谁知道如何将此代码放入 dll 并使用命令行运行它?

When you compile your program you make something called Portable Executable [PE] in Windows.当你编译你的程序时,你在 Windows 中创建了一个叫做Portable Executable [PE]的东西。 Among files that share that family are .exe , .dll , .scr and you can recognize them by opening them in text editor (such as notepad) and looking if file starts with MZ which are signature for Mark Zbikowski.共享该家族的文件包括.exe.dll.scr ,您可以通过在文本编辑器(例如记事本)中打开它们并查看文件是否以MZ开头(这是 Mark Zbikowski 的签名)来识别它们。

In short there isn't much difference in *.dll or *.exe except some minor blocks depedning on version.简而言之, *.dll*.exe没有太大区别,除了一些取决于版本的小块。 So in short you are making an "dll" when you compile it.所以简而言之,当你编译它时,你正在制作一个“dll”。 But if you wish to compile your program as dll exactly, that depends on your compiler:但是,如果您希望将程序编译为dll ,这取决于您的编译器:

  1. If you are working in Visual Studio , Microsoft has some tutorials for that如果你在Visual Studio中工作,微软有一些教程
  2. For MinGW you have in code tutorial对于MinGW ,您在代码教程中
  3. And for CygWin you have command line arguments for compiler对于CygWin ,您有用于编译器的命令行 arguments
  4. And for Clang I would suggest this question对于Clang我会建议这个问题

But I would be careful with deployment of such file, since @Richard nicely pointed it out that RunDll32 is deprecated, but it is still used in gears of some programming language libraries.但我会小心部署此类文件,因为@Richard 很好地指出RunDll32已被弃用,但它仍用于某些编程语言库的齿轮中。 So if you are building something for self testing purposes I would recommend those 4 options depending on your compiler.因此,如果您正在构建用于自测目的的东西,我会根据您的编译器推荐这 4 个选项。

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

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