首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何将程序转换为.dll文件并使用rundll32.exe在cmd中运行?

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

Stack Overflow用户
提问于 2019-11-03 16:50:01
回答 1查看 576关注 0票数 0

我有一个程序,可以创建多个线程,并在循环中打印一些字符串。我的任务是将这个程序转换成一个.dll,并使用rundll32.exe运行它,但我不知道如何将.dll作为可执行文件运行。

代码语言:javascript
复制
 #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;
}

有人知道如何将这些代码放入dll并使用命令行运行吗?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2019-11-03 17:05:20

在编译程序时,可以在Windows中创建名为便携式可执行[PE]的内容。共享该家族的文件包括.exe.dll.scr,您可以通过在文本编辑器(例如记事本)中打开这些文件来识别它们,并查看文件是否以MZ开头,MZ是Mark的签名。

简而言之,*.dll*.exe没有太大的区别,除了一些小块在版本上的删除。因此,简而言之,在编译它时,您正在创建一个"dll“。但是,如果希望将程序准确地编译为dll,这取决于编译器:

  1. 如果您在Visual 中工作,那么Microsoft有一些这方面的教程
  2. 对于MinGW,您有在代码教程中
  3. 对于CygWin,你有编译器的命令行参数
  4. 对于Clang,我建议使用这个问题

但是我会小心部署这样的文件,因为@Richard很好地指出了RunDll32是不推荐的,但是它仍然用于一些编程语言库的齿轮中。因此,如果您正在构建一些用于自我测试的内容,我将根据编译器的不同,推荐这4种选项。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/58682426

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档