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



我有一个程序可以创建多个线程并在循环中打印一些字符串。我的任务是将这个程序变成一个.dll并使用rundll32.exe运行它,但我不知道如何将.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;
}

有谁知道如何将此代码放入 dll 并使用命令行运行它?

当你编译你的程序时,你会在Windows中制作一个叫做可移植可执行文件[PE]的东西。共享该系列的文件包括.exe.dll.scr,您可以通过在文本编辑器(如记事本)中打开它们并查看文件是否以 Mark Zbikowski 签名的MZ开头来识别它们。

简而言之,除了一些小块在版本上降级之外,*.dll*.exe没有太大区别。简而言之,您在编译它时正在制作一个"dll"。但是,如果您希望完全dll地编译程序,这取决于您的编译器:

  1. 如果你在Visual Studio中工作,Microsoft有一些教程。
  2. 对于MinGW,您可以在代码教程中拥有
  3. 对于CygWin,你有编译器的命令行参数
  4. 对于Clang,我会建议这个问题

但是我会小心部署这样的文件,因为@Richard很好地指出RunDll32已被弃用,但它仍然用于某些编程语言库的齿轮中。因此,如果您正在构建用于自测试目的的东西,我会根据您的编译器推荐这 4 个选项。

最新更新