简体   繁体   中英

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.

 #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?

When you compile your program you make something called Portable Executable [PE] in Windows. 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.

In short there isn't much difference in *.dll or *.exe except some minor blocks depedning on version. So in short you are making an "dll" when you compile it. But if you wish to compile your program as dll exactly, that depends on your compiler:

  1. If you are working in Visual Studio , Microsoft has some tutorials for that
  2. For MinGW you have in code tutorial
  3. And for CygWin you have command line arguments for compiler
  4. And for Clang I would suggest this question

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. So if you are building something for self testing purposes I would recommend those 4 options depending on your compiler.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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